How to Convert HTML to a PDF in Python

The xhtml2pdf Python package simplifies the process used to generate PDF documents from HTML, XHTML and CSS in Python 2.3 and 2.5. Because it's written in pure Python, the package is platform independent and can be easily integrated into your programs or used as a stand-alone command line tool for quick conversions. The package supports multiple font styles and recognizes many formatting options in HTML documents.

Things You'll Need

  • Python 2.3 or 2.5
  • ReportLab Toolkit
  • Html5lib
Show More

Instructions

    • 1

      Launch a command line or terminal window. Run the "easy_install pisa" command to install the xhtml2pdf package. Optionally, you can download the source code from the developer's website. Install it with "python setup.py install" instead.

    • 2

      Open the Python application. Add "import ho.pisa as pisa" to the top to include pisa.

    • 3

      Use the syntax "pisa.CreatePDF(src,dst)" to create a PDF file from HTML. The "src" should be an HTML file; the destination is the PDF file you will be creating.

    • 4

      Use "pisa.startViewer(dst)" to launch the user's PDF viewer with the newly created PDF file open. The "dst" is the name of the PDF file.

Tips & Warnings

  • Use the pdf.err property to check for errors before opening the PDF files.

Related Searches:

References

Comments

Related Ads

Featured