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 pyppeteerpage.pdf(options)call.optionspassed as argument to API call will take precedence over the default options.meta_tag_prefix- the prefix forpdfmatespecific meta tags - by default this ispdfmate-.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?
Release files for pdfmate 0.4.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pdfmate-0.4.9.tar.gz | 9.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pdfmate-0.4.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.0 kB
Release files / pdfmate-0.4.9.tar.gz
| Download URL | pdfmate-0.4.9.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ffda05e1ff3c56d924d599a088fccabd718b2905ebdd3bba91f87dc74bc535b5
|
|
BLAKE2b-256 checksum How to use checksums |
05b216772e46ac290c49ad0b6d9e2541b354b57dd59d24ac7fca4f64240e90a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.10 CPython/3.9.0 Darwin/19.3.0
|
Release files / pdfmate-0.4.9-py3-none-any.whl
| Download URL | pdfmate-0.4.9-py3-none-any.whl |
|---|---|
| Size | 12.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ac67a07b60f714902529c8429919acd207d2637b3e731e5393d7315cae01c7a3
|
|
BLAKE2b-256 checksum How to use checksums |
f4be4e186e51696bdb1013bc02e8667885ed70f32fef32fa60938e322bc8244a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.10 CPython/3.9.0 Darwin/19.3.0
|