JSON To PDF - Fund Report Tutorial
Register and install an evaluation copy of ReportLab PLUS
This tutorial is aimed at showing the use of Report Markup Language (RML), which is a component of our commercial tool-kit. Here you can get a basic introduction to a common application architecture and start experimenting with some fundamental ReportLab technologies. If you have any problems or questions, please email email@example.com.
All you need to do to download a full evaluation copy is sign in or register on our site; then, follow the installation instructions to get yourself set up. Once these are completed, you're ready to go.
To check that all the dependencies are in place, start Python up (inside your virtual environment, if you are using one) and check these imports all work:
>>> import reportlab >>> import rlextra
Download and install tutorial pack
You have two options, either download the partial tutorial pack, which means you will need to finish to the code changes listed below or download the completed tutorial pack, which means you will not need to make the changes listed below.
Take a look at the directory structure:
data/ output/ rml/ gen_factsheet.py
data/will hold our JSON data
rml/will hold the fonts, images, and templates for constructing the documents
output/will hold the generated PDFs
gen_factsheet.pywill be the script which ties it all together
Making your first document in Report Markup Language
python gen_factsheet.py data/factsheet.json
You should now see a file output/factsheet.pdf, which should look like:
It is not much to look at in its current state, but we will make some improvements along the way.
Code Walk Though
The PDF template is rml/factsheet.prep, open that file in an editor. We define the frame(sections) on the page in the pagetemplate tag. During development, setting showBoundary can help be a visual guide to getting frames in the right place. Change the value from 0 to 1. Now generate the PDF again & you should see the the outline of the frames. Change showBoundary back to 0.
Lets add some coloured rectangles to the page. We can add the colours to sit behind the content, to give it some colour. We use the rect tag to draw rectangles on the page & set the colour accordingly. Add the following lines below:
We can also add colour to the Keyfacts table that appears on the right of the page.
Now regenerate the PDF.
We are now going to add two charts to the prep file.
The code for fundindexlineplot.py was generated by our our graphics library.
The code for assets_pie.py was also generated by our our graphics library.
Finaly, look in data/factsheet.json and change the "status" to "approved":
Now save data.json and re-run the script and check the output. It should look like this:
Further examples of the huge range of chart types available with ReportLab can be found in our chart gallery which included details of the main parameters of each style. Go and experiment!