Skip to main content

Pyppeteer PDF. Print html sites and files to pdf via pyppeteer (uses patched pyppeteer that by default downloads updated Chromium revision via https with certifi).

Project description

pyppdf: Pyppeteer PDF

Build Status

Prints html sites and files to pdf via pyppeteer (uses patched pyppeteer that by default downloads updated Chromium revision via https with certifi). I use it with Pandoctools/Knitty.

Pyppeteer is a Python port of the Puppeteer. pyppdf command line interface is built with the help of litereval and click.

At the moment recommended settings when reading from stdin are following:

echo "# Header
Text \$f(x)=x^2\$" |
pandoc -f markdown -t html --standalone --self-contained |
pyppdf -o doc.pdf --goto temp

Contents:

Install

Needs python 3.6+

conda install:

conda install -c defaults -c conda-forge pyppdf

pip install:

pip install pyppdf

Linux

If on Ubuntu additionally install (Chrome headless doesn't launch on Unix):

sudo apt-get update
sudo apt-get install -y ca-certificates fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils

CLI

pyppdf

Command line interface:

Usage: pyppdf [OPTIONS] [PAGE]

  Reads html document, converts it to pdf via pyppeteer and writes to disk
  (or writes base64 encoded pdf to stdout).

  PAGE is an URL or a common file path, pyppdf reads from stdin if PAGE is
  not set.

  -a, --args defaults:

  {launch={args=['--font-render-hinting=none']},goto={waitUntil='networkidle0',
  timeout=100000}, pdf={width='8.27in', printBackground=True, margin={top='1in',
  right='1in', bottom='1in', left='1in'},}}

  They affect the following pyppeteer methods (only the last name should be
  used):  pyppeteer.launch, page.goto, page.emulateMedia,
  page.waitForNavigation, page.waitFor, page.pdf. See:

  https://pyppeteer.github.io/pyppeteer/reference.html#pyppeteer.page.Page.pdf

Options:
  -a, --args TEXT  Python code str that would be evaluated to the dictionary
                   that is a pyppeteer functions options. Has predefined
                   defaults.
  -u, --upd TEXT   Same as --args dict but --upd dict is recursively merged
                   into --args.
  -o, --out TEXT   Output file path. If not set then pyppdf writes base64
                   encoded pdf to stdout.
  -d, --dir TEXT   Directory for '--goto temp' mode. Has priority over dir of
                   the --out

  -g, --goto [url|setContent|temp|data-text-html]
                   Choose page.goto behaviour. By default pyppdf tries 'url'
                   mode then 'setContent' mode. 'url' works only if url (PAGE)
                   arg was provided or {goto={url=<...>}} was set in the merged
                   args. 'setContent' (works without page.goto), 'temp' (temp
                   file) and 'data-text-html' work only with stdin input.
                   'setContent' and 'data-text-html' presumably do not support
                   some remote content. I have bugs with the last one
                   when: page.goto(f'data:text/html,{html}')
  --help           Show this message and exit.

See Pyppeteer methods.

pyppdf-install

Simply downloads and installs Chromium beforehead (uses fresher revision from pyppdf):

pyppdf-install

Python API

def save_pdf(output_file: str=None, url: str=None, html: str=None,
             args_dict: Union[str, dict]=None,
             args_upd: Union[str, dict]=None,
             goto: str=None, dir_: str=None) -> bytes:
    """
    Converts html document to pdf via pyppeteer
    and writes to disk if asked. Also returns bytes of pdf.

    ``args_dict`` affect the following methods that are used during
    conversion (only the last name should be used):
    ``pyppeteer.launch``, ``page.goto``, ``page.emulateMedia``,
    ``page.waitForNavigation``, ``page.waitFor``, ``page.pdf``. See:
     https://pyppeteer.github.io/pyppeteer/reference.html#pyppeteer.page.Page.pdf

    ``args_dict`` default value:

    >>> # {launch={args=['--font-render-hinting=none']},
    >>> #  goto={waitUntil='networkidle0', timeout=100000},
    >>> #  pdf={width='8.27in', printBackground=True,
    >>> #       margin={top='1in', right='1in',
    >>> #               bottom='1in', left='1in'},}}
    >>> #

    ``args_upd`` examples that won't overwrite other options:

    * ``"{launch={args=['--no-sandbox', '--disable-setuid-sandbox']}}``
    *  ``"{emulateMedia="screen", waitFor=1000}"``

    Formats for **values** of the ``args_dict``:
    ``*args`` and ``**kwargs`` for functions:

    * ``{(): (arg1, arg2), kwarg1=val1, kwarg2=val2}``
      Special key for positional args,
    * ``[arg1, arg2]`` or ``(arg1, arg2)`` Positional only,
    * If value in the **root**  ``args_dict`` is None
      (like ``{foo=None}``) it later means that ``'foo'`` key is not used
      (same as if it was absent in the **root** dict).

    Parameters
    ----------
    output_file :
        Path to write pdf to.
    url :
        Page URL address or html document file path
        (url has priority over html).
    html :
        html document file source
        (url has priority over html).
    args_dict :
        Options that govern conversion.
        dict with pyppeteer kwargs or Python code str that would
        be "litereval" evaluated to the dictionary.
        If None then default values are used.
        Supports extended dict syntax: {foo=100, bar='yes'}.
    args_upd :
        dict with *additional* pyppeteer kwargs or Python code str
        that would be "litereval" evaluated to the dictionary.
        This dict would be recursively merged into args_dict.
    goto :
        Same as in 'main' function.
    dir_ :
        Directory for goto temp mode.
    """
async def main(args: dict, url: str=None, html: str=None, output_file: str=None,
               goto: str=None, dir_: str=None) -> bytes:
    """
    Returns bytes of pdf.

    Parameters
    ----------
    args :
        Pyppeteer options that govern conversion.
        dict with keys dedicated for pyppeteer functions used.
        See save_pdf for more details.
    url :
        Site address or html document file path (url - that by the
        way can also be set in args - has priority over html).
    html :
        html document file source
    output_file :
        Path to save pdf
    goto :
        One of:
        >>> # ('url', 'setContent', 'temp', 'data-text-html')
        >>> #
        >>> # Choose page.goto behaviour. By default pyppdf tries 'url' mode
        >>> # then 'setContent' mode. 'url' works only if url (PAGE) arg was
        >>> # provided or {goto={url=<...>}} was set in the merged args.
        >>> # 'setContent' (works without page.goto), 'temp' (temp file) and
        >>> # 'data-text-html' work only with stdin input. 'setContent' and
        >>> # 'data-text-html' presumably do not support some remote
        >>> # content. I have bugs with the last one when:
        >>> # page.goto(f'data:text/html,{html}')
        >>> #
    dir_ :
        Directory for goto temp mode.
    """

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

pyppdf-0.1.2.tar.gz (26.8 kB view details)

Uploaded Source

File details

Details for the file pyppdf-0.1.2.tar.gz.

File metadata

  • Download URL: pyppdf-0.1.2.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for pyppdf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 beb94e8ce9e8893f5dc17d4b73176019b7e88d8f3e38eb72782a3a6c0bebffeb
MD5 c460a5feaf9667016a1a7c4099549585
BLAKE2b-256 3cedfb7ab976e9f9f97219135f3c6d1bae2cc745cb9ec7fea9bcd4ae1508f95b

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