Skip to content

Commit

Permalink
feat: add page.pdf more optional params
Browse files Browse the repository at this point in the history
  • Loading branch information
vvanglro committed Nov 4, 2022
1 parent 0efb8df commit 6a84557
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pwhtmltopdf/abc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import abc
import pathlib
import tempfile
import typing

from jinja2 import Template

Expand All @@ -9,18 +10,33 @@


class BaseHTP(abc.ABC):
def __init__(self, static_root: StrPath = None):
def __init__(
self,
static_root: StrPath = None,
wait_until: typing.Optional[
typing.Literal["commit", "domcontentloaded", "load", "networkidle"]
] = None,
print_background: typing.Optional[bool] = None,
prefer_css_page_size: typing.Optional[bool] = None,
):
"""
param static_root: The resource directory in html, which is passed in for subsequent rendering
"""
self.pw_server = PlayWrightServer()
self.static_root = static_root
self.wait_until = wait_until
self.print_background = print_background
self.prefer_css_page_size = prefer_css_page_size

async def _page_render(self, url: str, output_path: StrPath = None) -> bytes:
async with self.pw_server.new_page() as page:
await page.goto(url, wait_until="load")
await page.goto(url, wait_until=self.wait_until)
await page.emulate_media(media="print")
return await page.pdf(path=output_path)
return await page.pdf(
path=output_path,
print_background=self.print_background,
prefer_css_page_size=self.prefer_css_page_size,
)

async def _content_render(
self, content: str, output_path: StrPath = None, **render_kwargs
Expand Down

0 comments on commit 6a84557

Please sign in to comment.