Intelligent HTML-to-PDF conversion with layout pre-processing
Project description
html2pdf-smart
Intelligent HTML-to-PDF conversion with layout pre-processing.
Uses a hybrid architecture: a custom DOM analysis layer handles page break logic, font/margin adjustment, and layout optimization before Playwright (Chromium) renders the final PDF. Supports three interfaces: Python library, CLI, and REST API.
Installation
pip install html2pdf-smart
playwright install chromium
For CLI support:
pip install "html2pdf-smart[cli]"
For the REST API:
pip install "html2pdf-smart[api]"
Quickstart — Library
from html2pdf import convert, PDFConfig
# Convert an HTML string to PDF bytes
pdf_bytes = convert("<h1>Hello</h1><p>World</p>")
# With configuration overrides
pdf_bytes = convert(html, page_size="Letter", landscape=True, margin_top="25mm")
# Convert a file
from html2pdf import convert_file
output_path = convert_file("report.html", "report.pdf")
# Batch conversion (single browser instance — much faster)
from html2pdf import convert_batch
results = convert_batch([
("<h1>Doc 1</h1>", "doc1.pdf"),
("<h1>Doc 2</h1>", "doc2.pdf"),
])
for label, pdf_bytes, error in results:
if error:
print(f"{label}: FAILED — {error}")
else:
print(f"{label}: {len(pdf_bytes)} bytes")
Async
import asyncio
from html2pdf import async_convert, async_convert_batch
pdf = asyncio.run(async_convert("<h1>Async</h1>"))
Quickstart — CLI
# Single file
html2pdf convert report.html -o report.pdf
# Batch convert a directory
html2pdf convert *.html --output-dir ./pdfs/
# From stdin
cat page.html | html2pdf convert - -o page.pdf
# With options
html2pdf convert doc.html -o doc.pdf --format Letter --landscape --margin-top 25mm
Quickstart — REST API
# Start the server
uvicorn html2pdf.api.app:app --host 0.0.0.0 --port 8000
# Convert HTML
curl -X POST http://localhost:8000/convert/html \
-H "Content-Type: application/json" \
-d '{"html": "<h1>Hello</h1>"}' \
--output result.pdf
# Health check
curl http://localhost:8000/health
Configuration
PDFConfig fields (all have sensible defaults):
| Field | Default | Description |
|---|---|---|
page_size |
"A4" |
A0–A6, Letter, Legal, Tabloid, Ledger |
margin_top |
"20mm" |
Top margin |
margin_bottom |
"20mm" |
Bottom margin |
margin_left |
"15mm" |
Left margin |
margin_right |
"15mm" |
Right margin |
break_strategy |
"smart" |
"smart", "css-only", or "none" |
font_adjustment |
True |
Enable font normalization |
print_background |
True |
Include background colors/images |
scale |
1.0 |
Page scale factor |
landscape |
False |
Landscape orientation |
header_template |
None |
Custom header HTML |
footer_template |
None |
Custom footer HTML |
Config resolution order: function kwargs → config file → HTML2PDF_* env vars → frozen defaults.
Config file locations (first found wins): explicit path → ./html2pdf.toml → pyproject.toml [tool.html2pdf] → ~/.config/html2pdf/config.toml.
Requirements
- Python 3.10+
- Playwright with Chromium (
playwright install chromium)
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file html2pdf_smart-0.1.1.tar.gz.
File metadata
- Download URL: html2pdf_smart-0.1.1.tar.gz
- Upload date:
- Size: 76.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
735ea0ad32ebc867ea8cd60f60006d04f5005419d279a37b453f83c632a1fb60
|
|
| MD5 |
56fa5df904510a5ba015b1ca0e29249c
|
|
| BLAKE2b-256 |
e5ea0013dab09009fdf981a18d6e54d0d149cefebb2904ef0ca16c5d021f5d45
|
File details
Details for the file html2pdf_smart-0.1.1-py3-none-any.whl.
File metadata
- Download URL: html2pdf_smart-0.1.1-py3-none-any.whl
- Upload date:
- Size: 39.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f146df5389c2ea41d3d10ee2de3272d1999a85470e35c7c855967ef916a76126
|
|
| MD5 |
8d69d8d23d7ef13cc43de1011b21616e
|
|
| BLAKE2b-256 |
d67bd902e12786518e78bf9b512e6e1f4287965045a88d5c1f01000f27d52d71
|