Skip to main content

Pyppeteer-based async python wrapper to convert html to pdf

Project description

PDFGen-Python: HTML to PDF wrapper

Build Status PyPI version PyPI pyversions

Python 3.6.1+ async wrapper for Pyppeteer to convert HTML to PDF.

NOTE: All the public API functions are adapted to async coroutines, so use them with await!


This is adapted version of python-PDFKit library, so big thanks to them!


Installation

  • Install pdfgen:
$ pip install pdfgen
  • To download Chromium beforehand, run $ pdfgen-setup. Otherwise, it'll be downloaded on the first run of library.

Usage

For simple async tasks:

import pdfgen

async def f():
    await pdfgen.from_url('http://google.com', 'out.pdf')
    await pdfgen.from_file('test.html', 'out.pdf')
    await pdfgen.from_string('Hello!', 'out.pdf')

Sync API is also provided at pdfgen.sync for all the above-mentioned functions:

import pdfgen

pdfgen.sync.from_url('http://google.com', 'out.pdf')
pdfgen.sync.from_file('test.html', 'out.pdf')
pdfgen.sync.from_string('Hello!', 'out.pdf')

You can pass a list with multiple URLs or files:

pdfgen.sync.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfgen.sync.from_file(['file1.html', 'file2.html'], 'out.pdf')

Also you can pass an opened file:

with open('file.html') as f:
    pdfgen.sync.pdfgen(f, 'out.pdf')

If you wish to further process generated PDF, you can read it to a variable:

# Ignore output_path parameter to save pdf to a variable
pdf = pdfgen.sync.from_url('http://google.com')

You can specify all Pyppeteer options used for saving PDF as shown below:

options = {
    'scale': 2.0,
    'format': 'Letter',
    'margin': {
        'top': '0.75in',
        'right': '0.75in',
        'bottom': '0.75in',
        'left': '0.75in',
    },
    'pageRanges': '1-5,8',
}

pdfgen.sync.from_url('http://google.com', 'out.pdf', options=options)

You can also pass any options through meta tags in your HTML:

body = """
    <html>
      <head>
        <meta name="pdfgen-format" content="Legal"/>
        <meta name="pdfgen-landscape" content="False"/>
      </head>
      Hello World!
      </html>
    """

pdfgen.sync.from_string(body, 'out.pdf')

Configuration

Each API call takes an optional options parameter to configure print PDF behavior. However, to reduce redundancy, one can certainly set default configuration to be used for all API calls. It takes the configuration options as initial paramaters. The available options are:

  • options - the dict used by default for pyppeteer page.pdf(options) call. options passed as argument to API call will take precedence over the default options.
  • meta_tag_prefix - the prefix for pdfgen specific meta tags - by default this is pdfgen-.
  • environ - the dict used to provide env variables to pyppeteer headless browser.
import pdfgen

pdfgen.configuration(options={'format': 'A4'})

async def f():
    # The resultant PDF at 'output_file' will be in A4 size and 2.0 scale.
    await pdfgen.from_string(html_string, output_file, options={'scale': 2.0})

Troubleshooting

  • InvalidSourceError: Provided HTML source is invalid (maybe wrong URL or non-existing file)

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

pdfgen-1.0.5.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

pdfgen-1.0.5-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file pdfgen-1.0.5.tar.gz.

File metadata

  • Download URL: pdfgen-1.0.5.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.5

File hashes

Hashes for pdfgen-1.0.5.tar.gz
Algorithm Hash digest
SHA256 c11fd69714a4d6463675e28473d6e5a43467fa1ea8769bfc1213f6a07fccf9d2
MD5 78aadaabc3350d6f104ca7553ce46e84
BLAKE2b-256 12664a644dbb574b1cf3f553d3b542f626e5e23012585654a678ff9034033b55

See more details on using hashes here.

File details

Details for the file pdfgen-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: pdfgen-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.5

File hashes

Hashes for pdfgen-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d8952011c8fa59e560b8037d01d80ced7284085139e9d7b88f33d42a546388c9
MD5 cae759dd84eeed039b2df1164d64a1e7
BLAKE2b-256 97f400413c171a4cbd145717a7ac564d5b6379808036e935db36e2b3ce814d59

See more details on using hashes here.

Supported by

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