How download a pdf in my Dash & Plotly application

Hello @Matemunioz and welcome to the forum!

Print to PDF is one of the most delicate taks in Dash. Personally I implemented this solution from mentioned thread:

If you are no expert in javascript like me it is relatively simple to understand (it contains one clientside callback and one javascript script you have to place in assets folder and downloading htm2pdf). When it is set up it works relatively nice (i.e. you can use classes to give htm2pdf commands how it should see the layout (i.e. placing html.Div(className=“html2pdf__page-break”) into layout makes a page divider in a layout and you can control the behaviour of new page breaker).

Problem is that I encountered numerous bugs I was not able to solve (i.e. the plotly charts were always a little bit wider than in the browser), only working solution was to construct the layout for pdf in some special way so it looked fine in pdf (I solved mentioned issue by setting the width of the charts about 3/4 of the width of the parent container and in PDF it occupies whole container). There are different problems with different components so it is possible using another components in another libraries would not cause this issues.

2 Likes