playwright render html to pdf
Project description
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():
htp = HtmlToPdf()
await htp.from_url("https://playwright.dev/", "from_url.pdf")
await htp.close()
async def this_from_file():
htp = HtmlToPdf()
# Make sure the current directory has a test.html file
await htp.from_file("test.html", "from_file.pdf")
await htp.close()
async def this_from_string():
htp = HtmlToPdf()
content = pathlib.Path("test.html").read_text()
await htp.from_string(content, "from_string.pdf")
await htp.close()
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()
htp = HtmlToPdf(static_root="tests/static")
await htp.from_url(
f"file://{file_path}",
"tests/effect/from_url/local_url_render.pdf",
local_render=True,
char_code=123,
)
await htp.close()
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())
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
pwhtmltopdf-0.1.0.tar.gz
(7.3 kB
view hashes)
Built Distributions
Close
Hashes for pwhtmltopdf-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d6b853d8b312cfd967bf6474b8517e19f9bb0615bd20748d63b665c9c90f512 |
|
MD5 | 2aec7ffecc59d7a66d98bdec31a5f4ce |
|
BLAKE2b-256 | 40b7c1cc64184cbbe5734c8a5370ec841aa2e65edf2e45e9d5ca6cd336f75abb |
Close
Hashes for pwhtmltopdf-0.1.0-cp38-cp38-macosx_12_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be19f0954f745bdf0d0bba600e524b9f9e391c3925a2791a90c208e39968e936 |
|
MD5 | 2db1aee3d44fa11df8825d2aa80ad32d |
|
BLAKE2b-256 | be2f053f5cef5b0d01e8c707d3675db4bdd47fbc2386f1d1110f35275efe5bd3 |