# Playwright HTML to PDF
[![Package version](https://img.shields.io/pypi/v/pwhtmltopdf?color=%2334D058&label=pypi%20package)](https://pypi.python.org/pypi/pwhtmltopdf)
A modern html to pdf scheme based on playwright, Support more html and css technologies.
## Installation
1. Install pwhtmltopdf
```sh
pip install pwhtmltopdf
```
2. Install chromium
```sh
playwright install chromium
```
## Usage
Simple example:
```python
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👇
```python
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](https://playwright.dev/python/docs/api/class-browser#browser-new-page) and [page.pdf](https://playwright.dev/python/docs/api/class-page#page-pdf) all parameters passthrough.
```python
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())
```
Raw data
{
"_id": null,
"home_page": "https://github.com/vvanglro/pwhtmltopdf",
"name": "pwhtmltopdf",
"maintainer": "",
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": "",
"keywords": "playwright html pdf",
"author": "",
"author_email": "vvanglro <vvanglro@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/af/4d/b4106902315a41ccc5e8de13fa8a7781d405158e01b043816c0586364883/pwhtmltopdf-0.2.0.tar.gz",
"platform": null,
"description": "# Playwright HTML to PDF\n[![Package version](https://img.shields.io/pypi/v/pwhtmltopdf?color=%2334D058&label=pypi%20package)](https://pypi.python.org/pypi/pwhtmltopdf)\n\nA modern html to pdf scheme based on playwright, Support more html and css technologies.\n\n## Installation\n\n1. Install pwhtmltopdf\n ```sh\n pip install pwhtmltopdf\n ```\n2. Install chromium\n ```sh\n playwright install chromium\n ```\n\n## Usage\n\nSimple example:\n\n```python\nimport asyncio\nimport pathlib\nfrom pwhtmltopdf import HtmlToPdf\n\n\nasync def this_from_url():\n async with HtmlToPdf() as htp:\n await htp.from_url(\"https://playwright.dev/\", \"from_url.pdf\")\n\n\nasync def this_from_file():\n async with HtmlToPdf() as htp:\n # Make sure the current directory has a test.html file\n await htp.from_file(\"test.html\", \"from_file.pdf\")\n\n\nasync def this_from_string():\n async with HtmlToPdf() as htp:\n content = pathlib.Path(\"test.html\").read_text()\n await htp.from_string(content, \"from_string.pdf\")\n\n\nif __name__ == '__main__':\n asyncio.run(this_from_url())\n```\n\nRender fill:\n\nWhen `local_render` is equal to true, jinja2 template syntax will be used to render filled html,\nIf html needs to use local static resources, you need to set `static_root`,\nIf you want to render filled data dynamically to generate pdf(Based on jinja2), try the following method\ud83d\udc47\n\n```python\nimport asyncio\nimport pathlib\nfrom pwhtmltopdf import HtmlToPdf\n\n\nasync def this_render_from_url():\n file_path = pathlib.Path(\"tests/images.html\").absolute()\n async with HtmlToPdf(static_root=\"tests/static\",\n wait_until=\"load\", pdf_kwargs={\"print_background\": True}) as htp:\n await htp.from_url(\n f\"file://{file_path}\",\n \"tests/effect/from_url/local_url_render.pdf\",\n local_render=True,\n char_code=123,\n )\n\n\nasync def this_render_from_file():\n htp = HtmlToPdf(static_root=\"tests/static\")\n await htp.from_file(\n \"tests/images.html\", \"tests/effect/from_file/images_render.pdf\",\n local_render=True, char_code=123\n )\n await htp.close()\n\n\nasync def this_render_from_string():\n content = pathlib.Path(\"tests/images.html\").read_text()\n htp = HtmlToPdf(static_root=\"tests/static\")\n await htp.from_string(content, \"tests/effect/from_string/images_render.pdf\",\n local_render=True, char_code=123)\n await htp.close()\n\n\nif __name__ == '__main__':\n asyncio.run(this_render_from_url())\n```\n\n## Advanced usage\nSupport playwright [new_page](https://playwright.dev/python/docs/api/class-browser#browser-new-page) and [page.pdf](https://playwright.dev/python/docs/api/class-page#page-pdf) all parameters passthrough.\n\n```python\nimport asyncio\nfrom pwhtmltopdf import HtmlToPdf\n\n\nasync def example():\n async with HtmlToPdf(pdf_kwargs={\"print_background\": True},\n page_kwargs={\"locale\": \"de-DE\", \"is_mobile\": True}) as htp:\n await htp.from_url(\"https://playwright.dev/\", \"from_url.pdf\")\n\n\nif __name__ == '__main__':\n asyncio.run(example())\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "playwright render html to pdf",
"version": "0.2.0",
"project_urls": {
"Documentation": "https://github.com/vvanglro/pwhtmltopdf",
"Homepage": "https://github.com/vvanglro/pwhtmltopdf",
"Repository": "https://github.com/vvanglro/pwhtmltopdf.git"
},
"split_keywords": [
"playwright",
"html",
"pdf"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d146fa41b97cda2bc0fc8723577618a1bb63b83dd436a450b955ba3ef4ef92ef",
"md5": "62a89624461882daff8fa026b7dda0d1",
"sha256": "673f8dfca7e99a3f4934b3876469d38504ac22c240faaac8893508c7c46f092a"
},
"downloads": -1,
"filename": "pwhtmltopdf-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "62a89624461882daff8fa026b7dda0d1",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 10375,
"upload_time": "2023-11-30T07:44:14",
"upload_time_iso_8601": "2023-11-30T07:44:14.818044Z",
"url": "https://files.pythonhosted.org/packages/d1/46/fa41b97cda2bc0fc8723577618a1bb63b83dd436a450b955ba3ef4ef92ef/pwhtmltopdf-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "af4db4106902315a41ccc5e8de13fa8a7781d405158e01b043816c0586364883",
"md5": "86584a7e26e47cda8ccb120548fa23cc",
"sha256": "b6e662dc013e04d84c5fc0ca8a0e6b18f9c8bf105d5d420d6f93224e6a0e8178"
},
"downloads": -1,
"filename": "pwhtmltopdf-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "86584a7e26e47cda8ccb120548fa23cc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 16005,
"upload_time": "2023-11-30T07:44:17",
"upload_time_iso_8601": "2023-11-30T07:44:17.088637Z",
"url": "https://files.pythonhosted.org/packages/af/4d/b4106902315a41ccc5e8de13fa8a7781d405158e01b043816c0586364883/pwhtmltopdf-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-30 07:44:17",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "vvanglro",
"github_project": "pwhtmltopdf",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pwhtmltopdf"
}