We’ve had many little example projects to teach our tools in our repo for years; but with the advent of `pip install`, people ceased downloading the source distribution and generally haven’t noticed them.
We have therefore modernised our examples and collected them into a single repository which you can clone and run. You can always find this from the “Samples” button on our docs page..
View our samples page here, or clone the repo1 right away…
hg clone https://hg.reportlab.com/hg-public/rlextra-examples
Most of the examples use our preppy 2 preprocessor together with Report Markup Language. We usually work with files as input, so you can see and run these in seconds; in real life, you will probably be fetching data from databases or other sources.
These are some of the example demos we have available
Invoice - a simple structured document with tables and fields
Fundfacts - a full fund fact sheet with charts
Graphic card - how to make a simple custom graphic
Long document - tables of contents, chapter headings and so on.
Product catalogue
retrofitzero - a real example of a home environmental report built for a customer, with custom graphics to show progress towards net zero emissions
RML Tests - all the tests we run to illustrate tags and features in RML
Error Handling - some scripts which throw the different kinds of exceptions you might encounter in real life
Generating PDFs with dynamic content
With less than 10 lines of python code needed for the actual python to pdf generation it couldn’t be simpler! A minimal example is shown below,
import preppy
from rlextra.rml2pdf import rml2pdf
def main():
namespace = {'test_variable': True}
template = preppy.getModule('my_template.prep', savePyc=False, importModule=False)
rml = template.getOutput(namespace)
rml2pdf.go(rml.encode(), outputFileName="output.pdf", saveRml="latest.rml")
if __name__ == '__main__':
main()
The variables in the namespace dictionary can be accessed using {{test_variable}}
within the prep file. It’s as simple as that. A prep file is basically just an rml file that lets you use python.
By adding the saveRml variable to rml2pdf.go it’ll also output a file named latest.rml which will contain the rml between the prep file and the pdf to aid in debugging.
Example - Long document
To demonstrate potentially loading up large text documents into pdfs, we pulled some publicly available documents from the Internet Classics Archive available here: http://classics.mit.edu/. Below you can see a pdf generated using this text file.
In this example you can generate any of the documents used as examples from the Internet classics archive by passing the generation script different filenames like so:
python generate_long_document.py odyssey.mb.txt
Feel free to try it out with the different ones included and maybe even find another one from the archive you’re interested in and try generating it.
If there are more examples you would like to see, or you have built something you’d like to show people, let us know!
Yes, we use Mercurial. When the first distributed version conrol systems came out, Git was an insane mess, and mercurial was well documented and designed. Python moved to Mercurial so we did too.
preppy is a preprocessor for python. We wrote it in 2001 and still think it’s the simplest, best performing and most versatile templating system out there.