Simple python wrapper to convert HTML to PDF with headless Chrome via selenium.
Project description
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,
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 pyhtml2pdf-0.0.7.tar.gz
.
File metadata
- Download URL: pyhtml2pdf-0.0.7.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40b09b4d84dba2573e7510c62bc9408992df4db61a1d17b5027783e3313532f3 |
|
MD5 | 2a038ad393cdde199d0012a8ae18869c |
|
BLAKE2b-256 | 36dac1426a2e442275cf05cc8cec9983825e62ff82834a2ab117540a55ef71f5 |
File details
Details for the file pyhtml2pdf-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: pyhtml2pdf-0.0.7-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a78b947e1e405399fac8536447d989aacb22d0f985c99b7678153fb24610bd01 |
|
MD5 | ce5340e6da0e96eae39bc3ce4f87d90e |
|
BLAKE2b-256 | 6d613b2fe502635db6ed8d75ba95ac717720374dca2371117f489752cfa82942 |