Question

I'm trying to generate a very simple pdf using wkhtmltopdf tool and python:

# html string (works fine in all browsers)
html = generate_html()
f = open("/tmp/sudoku.html", 'w')
f.write(html)
system('wkhtmltopdf /tmp/sudoku.html sudoku.pdf')
f.close()

The resulting pdf is blank, but if i call wkhtmltopdf directly from the command line, it works:

wkhtmltopdf /tmp/sudoku.html sudoku.pdf

Why is that? Thanks.

Was it helpful?

Solution

Unless /tmp/sudoku.html already contains content, you need to close the file object first before calling wkhtmltopdf to generate a PDF from it. Unless you flush what you have already written to the file, nothing will be output to /tmp/sudoku.html until you close the file. Try:

html = generate_html()
f = open("/tmp/sudoku.html", 'w+')
f.write(html)
f.close() # close the file first
system('wkhtmltopdf /tmp/sudoku.html sudoku.pdf')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top