pyhtml2pdf
Simple python wrapper to convert HTML to PDF with headless Chrome via selenium.
Install
pip install pyhtml2pdf
Dependencies
- Selenium Chrome Webdriver (If Chrome is installed on the machine you won't need to install the chrome driver)
- Ghostscript
Example
Convert to PDF
Use with website url
from pyhtml2pdf import converter
converter.convert('https://pypi.org', 'sample.pdf')
Use with html file from local machine
import os
from pyhtml2pdf import converter
path = os.path.abspath('index.html')
converter.convert(f'file:///{path}', 'sample.pdf')
Some JS objects may have animations or take a some time to render. You can set a time out in order to help render those objects. You can set timeout in seconds
converter.convert(source, target, timeout=2)
Compress the converted PDF
Some PDFs may be oversized. So there is a built in PDF compression feature.
The power of the compression,
- 0: default
- 1: prepress
- 2: printer
- 3: ebook
- 4: screen
converter.convert(source, target, compress=True, power=0)
Pass Print Options
You can use print options mentioned here
converter.convert( f"file:///{path}", f"sample.pdf", print_options={"scale": 0.95} )
Compress PDF
Use it to compress a PDF file from local machine
import os
from pyhtml2pdf import compressor
compressor.compress('sample.pdf', 'compressed_sample.pdf')
Inspired the works from,
Metadata
Release files for pyhtml2pdf 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyhtml2pdf-0.1.0.tar.gz | 6.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyhtml2pdf-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.8 kB
Release files / pyhtml2pdf-0.1.0.tar.gz
| Download URL | pyhtml2pdf-0.1.0.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e2457b82535bed7a9125e03096a5766144180c2a606ae753f137ec1e2064681
|
|
BLAKE2b-256 checksum How to use checksums |
4368ac2b28b58f81aebd17aa83d6dc92e889a207c16b95b226ff7c25990dc3b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / pyhtml2pdf-0.1.0-py3-none-any.whl
| Download URL | pyhtml2pdf-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fee7049c0560d340ad32909c1830e3c9fdf1aaa1495b5ce41a894f3330dc990e
|
|
BLAKE2b-256 checksum How to use checksums |
b432cd0389e8a137f8a82114ef3465e65e432ffa182fe4f922d0612cf5370e9e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|