Monday, December 22, 2014

Need to create a nice PDF from Python?

pdfkit is a package that can help. This package can cook you some PDFs from HTML. It's a wrapper around wkhtmltopdf, so make sure you install that. On Debian/Ubuntu I wouldn't apt-get it from the standard repository - those wkhtmltopdf QT patches sure provide nice functionality.

Installing pdfkit is as simple as
$ pip install pdfkit
Then you can do this:
import pdfkit

pdfkit.from_url('http://google.com', 'out1.pdf')
pdfkit.from_file('test.html', 'out2.pdf')  # Provided you have test.html in your current folder
pdfkit.from_string('Hello!', 'out3.pdf')
pdfkit.from_string('<html><table><tr><th>Header</th></tr><tr><td>Row 1<td></tr><tr><td>Row 2<td></tr></table></html>', 'out4.pdf')

No comments:

Post a Comment