How to automatically build a high-quality dynamic PDF report?

I want to create a pdf data report programmatically so that it leads to a report in the style of this (it is in German but it shows the design I want to achieve):

I searched for some time in the web but couldn't find any solution yet. I would think to use a HTML-based approach with d3.js for the charts, but I have no idea how I can put this into a multi-page and still nice looking PDF. Some texts will be static and some will be dynamic depending on the data and, thus, the length of the texts and position of the charts may vary.

Does anybody has any suggestion or experience in providing a dynamic data report PDF in production that would look similar to the above? Perhaps also with other technologies like Python or LaTeX?

One interesting solution is to use plotly because it can generate good charts, and it can also write formulas with latex. Used with a HTML library like xhtml2pdf, you can generate any report.

In addition to that, it is a python library, so you can collect data from anywhere (database, excel file, etc.)

To install it:

pip install plotly

pip install xhtml2pdf


Example of pdf generation report from HTML:

import plotly
import xhtml2pdf

from xhtml2pdf import pisa             # import python module

graph_block = (''
'<a href="{graph_url}" target="_blank">' # Open the interactive graph when you click on the image
'<img style="height: 400px;" src="{graph_url}.png">'
'</a>')
report_block = ('' +
graph_block +
'{caption}' + # Optional caption to include below the graph
'<br>'      + # Line break
'<a href="{graph_url}" style="color: rgb(190,190,190); text-decoration: none; font-weight: 200;" target="_blank">'+
'Click to comment and see the interactive graph' + # Direct readers to Plotly for commenting, interactive graph
'</a>' +
'<br>' +
'<hr>') # horizontal line

# Utility function
def convert_html_to_pdf(source_html, output_filename):
# open output file for writing (truncated binary)
result_file = open(output_filename, "w+b")

# convert HTML to PDF
pisa_status = pisa.CreatePDF(
source_html,                # the HTML to convert
dest=result_file)           # file handle to recieve result

# close output file
result_file.close()                 # close output file

# return True on success and False on errors
return pisa_status.err

convert_html_to_pdf(report_block , 'report.pdf')


Example of text in Latex:

import plotly.express as px

fig = px.line(x=[1, 2, 3, 4], y=[1, 4, 9, 16], title=r'$$\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}$$')
fig.update_layout(
xaxis_title=r'$$\sqrt{(n_\text{c}(t|{T_\text{early}}))}$$',
yaxis_title=r'$$d, r \text{ (solar radius)}$$'
)
fig.show()


Sources:

https://plotly.com/python/v3/pdf-reports/

https://plotly.com/python/LaTeX/

• Hi Nicolas. Thanks for that! It is definitely helpful. However, it seems that if I would use plotly, only images of graphs can be included into the report. This would lead to the problem that fonts are of different sizes depending on the image size and content of the graphs in the report or they are squeezed in one direction. I think the only way to achieve a really well looking report is when it is vector-based which would lead me back to pure html -based solutions Aug 8 at 10:00
• How would you manage data in pure html-based solutions? At least you can manage data efficiently with python libraries, as well as using vector-based functions to achieve nice results. The advantage of Python, is that you can programmatically define sizes, content and use cases to build any HTML reports, in addition to latex and collecting data. In addition to that, you can even build dynamic reports that can be exported to pdf, for instance: tdenzl-bulian-bulian-ifeiih.streamlitapp.com and streamlit-example-app-pdf-report-streamlit-app-rolxw3.streamlit… Aug 8 at 10:16