Black Lives Matter. Please consider donating to Black Girls Code today.

Creating a PDF report with pdfkit


I tried to create a PDF report from a Plotly graph using pdfkit and wkhtmltopdf with the following code:

import plotly.offline as offline
import plotly.graph_objs as go
import pdfkit    

data = [go.Bar(x=[2, 4, 6], y=[10, 12, 15])]
layout = go.Layout(title='A Simple Plot', width=800, height=640)

fig = offline.plot({'data': data,
                    'layout': layout},
                    output_type='div', include_plotlyjs=True)

opt = {'javascript-delay': 1000,
       'no-stop-slow-scripts': None,
       'debug-javascript': None}

pdfkit.from_string(fig, 'test.pdf', options=opt)

when I use Linux, everything works like a charm. But on windows, the generated PDF is always empty and wkhtmltopdf throws a Javascript Error:

Loading pages (1/6)
Warning: undefined:0 TypeError: 'undefined' is not a function     
Warning: undefined:0 ReferenceError: Can't find variable: Plotly  
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)

Any ideas why? Or is there another solution for pdf report generation with plotly (offline)?

Problem solved. It’s a bug inside of wkhtmltopdf.
Just use the bleeding edge version (0.13), based on Qt 5.4.2. The problem seems to be in Qt WebEngine.

Question - how come when I try this it just generates a pdf with the html address file path and not the chart itself?

I don’t really know anything about pdfkit, but I wanted to mention that the plotly orca project (see is a (relatively) new cross-platform command line program supports PDF export of plotly figures.

And, integration is on the way: see