HTML-to-PDF with Python


Code Examples


pip install --upgrade docraptor


easy_install --upgrade docraptor

If you are on a system with easy_install but not pip, you can use easy_install instead. If you're not using virtualenv, you may have to prefix those commands with sudo.


Below is a barebones example, more robust examples with file output and error handling, asynchronous generation, hosted documents, or asynchronous hosted documents are also available.

import docraptor

doc_api = docraptor.DocApi()
doc_api.api_client.configuration.username = 'YOUR_API_KEY_HERE'
# doc_api.api_client.configuration.debug = True

response = doc_api.create_doc({
  "test": True,                                                   # test documents are free but watermarked
  "document_content": "<html><body>Hello World</body></html>",    # supply content directly
  # "document_url": "", # or use a url
  "name": "docraptor-python.pdf",                                 # help you find a document later
  "document_type": "pdf",                                         # pdf or xls or xlsx
  # "javascript": True,                                           # enable JavaScript processing
  # "prince_options": {
  #   "media": "screen",                                          # use screen styles instead of print styles
  #   "baseurl": "",                              # pretend URL when using document_content
  # },

Documents created synchronously like above are limited to 60 seconds of generation time, the asynchronous method allows up to 10 minutes.

Our styling documentation and knowledge base contain tips and guides on creating headers, footers, page numbers, table of contents, and much more.

With DocRaptor, it's fast and painless to convert HTML, CSS, and JavaScript into PDF and XLS documents with Python. Below are working Python examples for creating documents. Our API reference lists all the generation options and our style and formatting guide will help make it look perfect.