Skip to main content

pyhtml2pdf

Simple python wrapper to convert HTML to PDF with headless Chrome via selenium.

Install

pip install pyhtml2pdf

Dependencies

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)

Source distribution for pyhtml2pdf 0.1.0
File Size Uploaded
pyhtml2pdf-0.1.0.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyhtml2pdf 0.1.0
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.9

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page