6

I'm trying to convert some HTML files stored locally on my computer to PDF format through a Python script, and I've tried xhtml2pdf but I ran into countless errors and decided to stop using it.

I heard that wkhtmltopdf was a better alternative and I found a Python package that integrated well into it. Unfortunately, this package requires xvfb which cannot be installed for Windows. Is there any other way to install wkhtmltopdf for Python on Windows?

Thanks for your help!

3 Answers 3

2

Here's wkhtmltopdf download list, windows installer included

2
  • install wkhtmltopdf for Windows(http://wkhtmltopdf.org/downloads.html)
  • Add the bin folder to the "Path" env variable (eg: C:\Program Files (x86)\wkhtmltopdf\bin)
  • Install pdfkit for python bindings (pip install pdfkit)

Documentation about pdfKit can be found here: https://pypi.python.org/pypi/pdfkit

0

This is for windows & linux

  • wkhtmltopdf [download list][1], for windows installer
  • Add the binary "Path" to env variable

    def html_to_pdf(<html>):
       with tempfile.NamedTemporaryFile(suffix='.html', delete=False) as temp:
           temp.write(html.encode('windows-1252'))
           temp.seek(0)
           pdfkit.from_file(temp.name, 'out.pdf') 
           temp.close()
           pdf = open('out.pdf', 'r+b')
           response = HttpResponse(pdf.read(), content_type='application/pdf')
           response['Content-Disposition'] = 'attachment; filename=out.pdf'
           pdf.close()
           # remove the locally created pdf file.
           os.remove(temp.name)
           return response
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.