Pyppeteer-based async python wrapper to convert html to pdf
Project description
PDFMate
Async / sync wrapper for Pyppeteer
Install
pip install pdfmate
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 pyppeteerpage.pdf(options)
call.options
passed as argument to API call will take precedence over the default options.meta_tag_prefix
- the prefix forpdfgen
specific meta tags - by default this ispdfgen-
.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})
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?
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
Built Distribution
File details
Details for the file pdfmate-0.4.1.tar.gz
.
File metadata
- Download URL: pdfmate-0.4.1.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2924700a54a024859c08b995f8f71bb5adc2a6286dd9b029cbed96ca983b3e6 |
|
MD5 | 67c2aadc2d82eb5179153187f3d19f13 |
|
BLAKE2b-256 | 648ab6296fb9c6dadf88dd6c51bffdbd5d8e3e8525297ae83da116a2fcf77ec6 |
File details
Details for the file pdfmate-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: pdfmate-0.4.1-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | edec80956ae461c7231d70cdfd386db373c8d57a914ebff7e277595ccad21aa7 |
|
MD5 | da37c66e3a7c689d7c54b8f018253793 |
|
BLAKE2b-256 | a587d4a676cf756f1d8e4d50aa2b829fb51f4d5dd329c92fc09ce61a0cee8834 |