Skip to main content

A small Python 3 library that uses Selenium and Google Chrome to convert HTML into a PDF.

Project description

ChromePDF

Overview

ChromePDF is a small Python 3 library that uses Selenium and Google Chrome to convert HTML into a PDF. This is accomplished by using Chrome's Page.printToPDF DevTools command.

ChromePDF provides a function that accepts an html string, plus a dict of page parameters, and other settings, and returns the bytes containing a PDF:

pdf_bytes = generate_pdf(html_string, pdf_kwargs, **kwargs)

Because it renders via Chrome, it supports a wide range of CSS and HTML tags that should display the same as if you used Chrome to view the HTML itself.

ChromePDF can take advantage of Django settings for configuration, but Django is not a required dependency.

Official ChromePDF releases are available on PyPI.

Installation

1. Install ChromePDF via pip.

The latest version can be installed via PyPI. This will also install Selenium, the only direct dependency (Selenium versions 3 and 4 are supported; 3 will be used if already present, otherwise will install 4).

You may view the Changelog for a list of all ChromePDF version changes. ChromePDF uses semantic versioning for its release numbering. To ensure backwards compatibility and receive bug fixes, you are encouraged to pin your requirements to a Major.Minor version like so:

pip install django-chromepdf~=1.3.2

2. Set the location of your Chrome executable. This can be done in one of two ways:

  • In your Django settings, set CHROMEPDF['CHROME_PATH'] to the full path of the executable (E.G., r'C:\Program Files (x86)\Google\...\chrome.exe')
  • OR, pass chrome_path as a keyword argument to the generate_pdf() function.

About Chromedrivers

A chromedriver executable is necessary to interface between Selenium and Chrome. ChromePDF will automatically check the version of your Chrome binary and download the required chromedriver from the Chrome website if it doesn't exist, into your site-packages/chromepdf/chromedrivers/ folder. If the Chrome binary ever gets upgraded, ChromePDF will realize this and download the required driver for it.

You may disable these automatic downloads in the following way:

  • In your Django settings, set CHROMEPDF['CHROMEDRIVER_DOWNLOADS'] to False
  • OR, pass a chromedriver_downloads=False argument to generate_pdf()

You may also specify a chromedriver path manually. This is recommended if you disable downloads:

  • In your Django settings, set CHROMEPDF['CHROMEDRIVER_PATH'] to the full path of the executable (E.G., r'C:\Users\myuser\...\chromedriver_win32\chromedriver.exe')
  • OR, pass a chromedriver_path argument to generate_pdf() containing the path.
  • OR, if both of the above are not set, and you've disabled downloads, and if your chromedriver is in your PATH environment variable, then Selenium should be able to find it automatically.

Example: generate_pdf()

Note: generate_pdf() cannot include external files including CSS. You must include all your CSS within <style> tags or as inline styles.

# NOTE: This example assumes that you've set Django's settings.CHROMEPDF['CHROME_PATH'] = '(path to your Chrome instance)'
from chromepdf import generate_pdf 

with open('myfile.html','r') as f:
    html = f.read()

pdf_kwargs = {
    'paperFormat': 'A4',
    'marginTop': '2.5cm',
    'marginLeft': '2cm',
    'marginRight': '2cm',
    'marginBottom': '3.5cm',
    'displayHeaderFooter': True,
    'headerTemplate': '',
    'footerTemplate': '''
        <div style='font-size: 12px; width: 100%; padding: 0; padding-left: 2cm; padding-bottom: 1cm; margin: 0; '>
            Page <span class='pageNumber'></span> of <span class='totalPages'></span>
        </div>
    ''',
}
pdfbytes = generate_pdf(html, pdf_kwargs)

with open('myfile.pdf', 'wb') as file:
    file.write(pdfbytes)

Django Settings

You can specify default settings in your Django settings file, if desired, via a CHROMEPDF settings. Anything passed via the pdf_kwargs argument will override the PDF_KWARGS settings.

# settings.__init__.py

CHROMEPDF = {
    'CHROME_PATH': r'C:\Program Files (x86)\Google\...\chrome.exe',
    'CHROME_ARGS': [], # Optional list of command-line argument strings to pass to Chrome when rendering a PDF.
    'CHROMEDRIVER_PATH': None, # will rely on downloads instead
    'CHROMEDRIVER_DOWNLOADS': True, # automatically download the correct chromedriver for the chrome path
    'PDF_KWARGS': {
        'paperFormat': 'A4',
        'marginTop': '2.5cm',
        'marginLeft': '2cm',
        'marginRight': '2cm',
        'marginBottom': '3.5cm',
    }
}

PDF_KWARGS Options

The pdf_kwargs argument to generate_pdf() lets you specify all the arguments for Chrome's Page.printToPDF API. Its API can be viewed here:

Page.printToPDF API (url is funky. If you get a 404, try reloading/refreshing)

Some shortcut parameters are provided by generate_pdf() for convenience. Here is a list of all the options:

Layout:

  • scale: Scale of the PDF. Default 1.
  • landscape: True to use landscape mode. Default False.

Page Dimensions:

  • paperWidth: Width of the paper, in inches. Can also use some CSS string values, like '30cm'. Default: 8.5
  • paperHeight: Height of the paper, in inches. Can also use some CSS string values, like '30cm'. Default: 11
  • paperFormat: A string indicating a paper size format, such as 'letter' or 'A4'. Case-insensitive. This will override paperWidth and paperHeight. Not part of Page.printToPDF API. Provided for convenience.

Content:

  • displayHeaderFooter: True to display header and footer. Default False.
  • headerTemplate: HTML containing the header for all pages. Default is an empty string. You may pass html tags with specific classes in order to insert values. For example, <span class='title'></span> would insert the the title.
    • date: formatted print date
    • title: document title
    • url: document location
    • pageNumber: current page number
    • totalPages: total pages in the document
  • footerTemplate: HTML containing the footer for all pages. Default is an empty string. You may pass html tags with specific classes in order to insert values (same as above)
  • printBackground: True to print background graphics. Default False.

Margins:

  • margin: Shortcut used to set all four margin values at once. Not part of Page.printToPDF API. Provided for convenience.
  • marginTop: Top margin. Default: '1cm'
  • marginBottom: Bottom margin. Default: '1cm'
  • marginLeft: Left margin. Default: '1cm'
  • marginRight: Right margin. Default: '1cm'

Page Ranges:

  • pageRanges: String indicating page ranges to use. Example: '1-5, 8, 11-13'
  • ignoreInvalidPageRanges: If True, will silently ignore invalid 'pageRanges' values. Default False.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-chromepdf-1.3.2.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_chromepdf-1.3.2-py3-none-any.whl (35.9 kB view details)

Uploaded Python 3

File details

Details for the file django-chromepdf-1.3.2.tar.gz.

File metadata

  • Download URL: django-chromepdf-1.3.2.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/28.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.51.0 importlib-metadata/4.8.3 keyring/21.4.0 rfc3986/1.4.0 colorama/0.4.4 CPython/3.6.8

File hashes

Hashes for django-chromepdf-1.3.2.tar.gz
Algorithm Hash digest
SHA256 072d7f5fa9adff95b34eb9b1e018b5814be9deaa76d4df0dc45737f1e73deac9
MD5 dd998d9dc089c68e19a5d6e90ce10784
BLAKE2b-256 664a375ec423ac329606dae73359698597445096a571ff53245ac5b8919e2c64

See more details on using hashes here.

File details

Details for the file django_chromepdf-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: django_chromepdf-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 35.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/28.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.51.0 importlib-metadata/4.8.3 keyring/21.4.0 rfc3986/1.4.0 colorama/0.4.4 CPython/3.6.8

File hashes

Hashes for django_chromepdf-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4ba9cdb3e2ff79117b93977d2fe59e87b0c1ebabd9770f95f04a1862018ebb3b
MD5 a5e09392175859db85b1e2cca9de1685
BLAKE2b-256 9698238a9223df633ee4afbe937e96396929c1204aa2073e3da4d7852b3cfbda

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page