1
$\begingroup$

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):

https://www.destatis.de/DE/Service/Statistik-Campus/Datenreport/Downloads/datenreport-2021.pdf;jsessionid=15EE55DD14818244B5EA17198EAA6B04.live721?__blob=publicationFile

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?

$\endgroup$
1
  • $\begingroup$ Did you look into PyFPDF? It can handle images (plots), and even templates. I am currently doing something similar. We use Tableau, but it is lacking in some areas, and have a pretty unique and non-reusable way of doing things. The report that you are linking was done with Adobe InDesign, at least according to the file's metadata. $\endgroup$ Aug 22 at 11:17

1 Answer 1

3
$\begingroup$

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/

$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – JoeBe
    Aug 8, 2022 at 10:00
  • $\begingroup$ 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… $\endgroup$ Aug 8, 2022 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.