Skip to main content

Pyppeteer-based async python wrapper to convert html to pdf

Project description

PyPI PyPI version Code style: black

PDFMate

Async / sync wrapper for Pyppeteer

Install

pip install pdfmate

Usage

For simple async tasks:

import pdfmate

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

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

import pdfmate

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

You can pass a list with multiple URLs or files:

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

Also you can pass an opened file:

with open('file.html') as f:
    pdfmate.sync.pdfmate(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 = pdfmate.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',
}

pdfmate.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="pdfmate-format" content="Legal"/>
        <meta name="pdfmate-landscape" content="False"/>
      </head>
      Hello World!
      </html>
    """

pdfmate.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 pdfmate specific meta tags - by default this is pdfmate-.
  • environ - the dict used to provide env variables to pyppeteer headless browser.
import pdfmate

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

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

Setup for development

poetry install -v --no-root

Run tests

poetry run pytest tests/

Enable git-hooks with lint-staged

npx mrm lint-staged
npx husky install

Credits

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

Other projects

Is it any good?

Yes.

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

pdfmate-0.4.2.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

pdfmate-0.4.2-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file pdfmate-0.4.2.tar.gz.

File metadata

  • Download URL: pdfmate-0.4.2.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.3 Darwin/19.3.0

File hashes

Hashes for pdfmate-0.4.2.tar.gz
Algorithm Hash digest
SHA256 5c01511ac9de2cc10ad81955788a174194fae299bce32456ab8cc2de190a21d3
MD5 56d195714fa6322a213ee881c6beb3fb
BLAKE2b-256 792e1e1928a810e679ce0d8cf2b32b2c8cf0249ee53da1ed8e6258b37690b0a5

See more details on using hashes here.

File details

Details for the file pdfmate-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: pdfmate-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.3 Darwin/19.3.0

File hashes

Hashes for pdfmate-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 be7a89cb241a31ab7ed4cb1c0015beab5894dbab677c6c46d51f48573c49151f
MD5 3bc30e6e73b8ab3504385694e227ba6c
BLAKE2b-256 c0847c4ed8953ab90720e43d3fbaa1e38cd8e9155e890e94b5584f4d06b328ed

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