Giter Club home page Giter Club logo

pdfmate's Introduction

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.

pdfmate's People

Contributors

alanhamlett avatar edwardbetts avatar exit99 avatar fbataill avatar fvox13 avatar irux avatar jazzcore avatar lexhung avatar manrajgrover avatar martin-rehr avatar michaelkim0407 avatar mzonzon avatar nestortejero avatar nuno-andre avatar pdelsante avatar shivanshs9 avatar signalkraft avatar snwfdhmp avatar tomscytale avatar woodb avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.