Playwright HTML to PDF
A modern html to pdf scheme based on playwright, Support more html and css technologies.
Installation
- Install pwhtmltopdf
pip install pwhtmltopdf
- Install chromium
playwright install chromium
Usage
Simple example:
import asyncio
import pathlib
from pwhtmltopdf import HtmlToPdf
async def this_from_url():
async with HtmlToPdf() as htp:
await htp.from_url("https://playwright.dev/", "from_url.pdf")
async def this_from_file():
async with HtmlToPdf() as htp:
# Make sure the current directory has a test.html file
await htp.from_file("test.html", "from_file.pdf")
async def this_from_string():
async with HtmlToPdf() as htp:
content = pathlib.Path("test.html").read_text()
await htp.from_string(content, "from_string.pdf")
if __name__ == '__main__':
asyncio.run(this_from_url())
Render fill:
When local_render is equal to true, jinja2 template syntax will be used to render filled html,
If html needs to use local static resources, you need to set static_root,
If you want to render filled data dynamically to generate pdf(Based on jinja2), try the following method👇
import asyncio
import pathlib
from pwhtmltopdf import HtmlToPdf
async def this_render_from_url():
file_path = pathlib.Path("tests/images.html").absolute()
async with HtmlToPdf(static_root="tests/static",
wait_until="load", pdf_kwargs={"print_background": True}) as htp:
await htp.from_url(
f"file://{file_path}",
"tests/effect/from_url/local_url_render.pdf",
local_render=True,
char_code=123,
)
async def this_render_from_file():
htp = HtmlToPdf(static_root="tests/static")
await htp.from_file(
"tests/images.html", "tests/effect/from_file/images_render.pdf",
local_render=True, char_code=123
)
await htp.close()
async def this_render_from_string():
content = pathlib.Path("tests/images.html").read_text()
htp = HtmlToPdf(static_root="tests/static")
await htp.from_string(content, "tests/effect/from_string/images_render.pdf",
local_render=True, char_code=123)
await htp.close()
if __name__ == '__main__':
asyncio.run(this_render_from_url())
Advanced usage
Support playwright new_page and page.pdf all parameters passthrough.
import asyncio
from pwhtmltopdf import HtmlToPdf
async def example():
async with HtmlToPdf(pdf_kwargs={"print_background": True},
page_kwargs={"locale": "de-DE", "is_mobile": True}) as htp:
await htp.from_url("https://playwright.dev/", "from_url.pdf")
if __name__ == '__main__':
asyncio.run(example())
Metadata
Release files for pwhtmltopdf 0.2.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 | Provenance | Actions |
|---|---|---|---|---|
| pwhtmltopdf-0.2.0.tar.gz | 16.0 kB | Without provenance | Details for pwhtmltopdf-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pwhtmltopdf-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for pwhtmltopdf-0.2.0-py3-none-any.whl |
Total release size: 26.4 kB
Release files / pwhtmltopdf-0.2.0.tar.gz
| Download URL | pwhtmltopdf-0.2.0.tar.gz |
|---|---|
| Size | 16.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b6e662dc013e04d84c5fc0ca8a0e6b18f9c8bf105d5d420d6f93224e6a0e8178
|
|
BLAKE2b-256 checksum How to use checksums |
af4db4106902315a41ccc5e8de13fa8a7781d405158e01b043816c0586364883
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.4 CPython/3.12.0
|
Release files / pwhtmltopdf-0.2.0-py3-none-any.whl
| Download URL | pwhtmltopdf-0.2.0-py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
673f8dfca7e99a3f4934b3876469d38504ac22c240faaac8893508c7c46f092a
|
|
BLAKE2b-256 checksum How to use checksums |
d146fa41b97cda2bc0fc8723577618a1bb63b83dd436a450b955ba3ef4ef92ef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.4 CPython/3.12.0
|
Security for pwhtmltopdf 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.