Is it possible to install the wkhtmltopdf Python package on Windows?
Asked Answered
L

3

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!

Lye answered 4/2, 2012 at 21:50 Comment(0)
K
2

Here's wkhtmltopdf download list, windows installer included

Keare answered 1/8, 2012 at 12:58 Comment(0)
M
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

Maag answered 5/1, 2018 at 14:39 Comment(0)
C
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
    
Cay answered 27/3, 2020 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.