How to format and print Dash dashboard to PDF?

旧街凉风 提交于 2020-01-14 03:38:21

问题


I've just created a dashboard in Dash, which fits automatically to my computer monitor and looks half decent. I now have the problem that I have to produce a way to print to PDF.

I've seen the Vanguard example which uses external CSS to format into an A4-size, and then a print button (using external javascript?). Is there a more 'Dash-native' way of doing this that doesn't require me to learn another programming language (I only know Python)?

There doesn't seem to be anything on the Dash User Guide https://dash.plot.ly/, all I've found is this https://plot.ly/python/pdf-reports/ which describes how to print individual plotly figures to pdf.

Ideally, it would format as it is now online (online layout) without losing space down the sides, and then I could use another layout (pdf layout) which could be printed out.

Any links or suggestions on how to proceed with this would be very much appreciated!


回答1:


You can get pdf from plotly if save plot as png image (the most left option on image):

And then using that code convert png to pdf:

from PIL import Image
# Save your plot as png file (option "Download as png" in upper-left corner)
PNG_FILE = 'horizontal-bar.png'
PDF_FILE = 'horizontal-bar.pdf'

rgba = Image.open(PNG_FILE)
rgb = Image.new('RGB', rgba.size, (255, 255, 255))  # white background
rgb.paste(rgba, mask=rgba.split()[3])               # paste using alpha channel as mask
rgb.save(PDF_FILE, 'PDF', resolution=100.0)

If you are a lazy one, you can use library webkit2png check here how. This allowing you not clicking on Download as png button and just convert from html to png and then to pdf.



来源:https://stackoverflow.com/questions/55494924/how-to-format-and-print-dash-dashboard-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!