diff --git a/README.md b/README.md index 5875e33..1ccb6de 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Event payload body. | **document_url** | url | Fetch the html content from `document_url` to disk before rendering. | | **browser_url** | url | Browser the `browser_url` with `playwright` before rendering with `renderer` | | **browser_headers** | Mapping[str,str] | Add additional headers to playwright before visiting `browser_url` | -| **browser_papersize** | `A4` or `Letter` | Size of the page when creating PDF with playwright | | +| **browser_pdf_options** | Mapping[str,str] | Add additional options to playwright `.pdf()` call | | | **renderer** | `princexml` or `playwright` | Renderer to render the html with | | **bucket_name** | string | Output the resulting pdf to `s3://{bucket_name}/{uuid}.pdf`. The lambda will require permission to upload to the bucket. The response will include `bucket`, `key`, `presigned_url` | | **presigned_url** | url | Output the resulting pdf to the presigned url. Generate the presigned url with `put_object`. See Output for more information. | diff --git a/lambda_function.py b/lambda_function.py index c5afac5..749988f 100644 --- a/lambda_function.py +++ b/lambda_function.py @@ -8,7 +8,7 @@ import tempfile import uuid import xml.etree.ElementTree as ET -from collections.abc import Iterator +from collections.abc import Iterator, Mapping from contextlib import contextmanager from dataclasses import dataclass, field from urllib.parse import urlparse @@ -39,11 +39,6 @@ class Renderers(str, enum.Enum): princexml = "princexml" -class Papersize(str, enum.Enum): - A4 = "A4" - Letter = "Letter" - - class Payload(pydantic.BaseModel): # NOTE: When updating this model, also update the equivalent documentation # General Parameters @@ -58,7 +53,7 @@ class Payload(pydantic.BaseModel): ## Browse the document in a browser before rendering browser_url: str | None = None browser_headers: dict = pydantic.Field(default_factory=dict) - browser_papersize: Papersize = Papersize.A4 + browser_pdf_options: Mapping[str, str] = pydantic.Field(default_factory=dict) renderer: Renderers = Renderers.princexml # Output parameters @@ -158,10 +153,10 @@ def _playwright_visit_page(browser_url: str, headers: dict) -> Iterator[playwrig def playwright_page_to_pdf( - browser_url: str, headers: dict, output_filepath: str, papersize: Papersize = Papersize.A4 + browser_url: str, headers: dict, output_filepath: str, pdf_options: Mapping[str, str] ) -> None: with _playwright_visit_page(browser_url, headers) as page: - page.pdf(path=output_filepath, format=papersize) + page.pdf(path=output_filepath, **pdf_options) def playwright_page_to_html_string(browser_url: str, headers: dict) -> str: @@ -183,7 +178,7 @@ def pdf_from_document_content(payload: Payload, output_filepath: str) -> None: f"file://{temporary_html_file.name}", payload.browser_headers, output_filepath, - payload.browser_papersize, + payload.browser_pdf_options, ) @@ -206,7 +201,7 @@ def pdf_from_document_url(payload: Payload, output_filepath: str) -> None: f"file://{temporary_html_file.name}", payload.browser_headers, output_filepath, - payload.browser_papersize, + payload.browser_pdf_options, ) @@ -226,7 +221,7 @@ def pdf_from_browser_url(payload: Payload, output_filepath: str) -> None: payload.browser_url, payload.browser_headers, output_filepath, - payload.browser_papersize, + payload.browser_pdf_options, ) diff --git a/tests/test_lambda_e2e.py b/tests/test_lambda_e2e.py index cefd606..09f4a48 100644 --- a/tests/test_lambda_e2e.py +++ b/tests/test_lambda_e2e.py @@ -68,7 +68,11 @@ def test_generating_pdf_from_document_url(self, renderer: str, browser_papersize ) status_code, _, pdf_body = call_lamdba( - {"document_url": document_url, "renderer": renderer, "browser_papersize": browser_papersize}, + { + "document_url": document_url, + "renderer": renderer, + "browser_pdf_options": {"format": browser_papersize}, + }, ) assert b"Z" in pdf_body @@ -77,7 +81,11 @@ def test_generating_pdf_from_document_url(self, renderer: str, browser_papersize def test_generating_pdf_from_document_content(self, renderer: str, browser_papersize: str): """Send an embedded html document and receive the pdf bytes for it""" status_code, _, pdf_body = call_lamdba( - {"document_content": "

Z

", "renderer": renderer, "browser_papersize": browser_papersize}, + { + "document_content": "

Z

", + "renderer": renderer, + "browser_pdf_options": {"format": browser_papersize}, + }, ) assert b"Z" in pdf_body @@ -85,7 +93,11 @@ def test_generating_pdf_from_document_content(self, renderer: str, browser_paper def test_generating_pdf_from_browser_url(self, renderer: str, browser_papersize: str): status_code, _, pdf_body = call_lamdba( - {"browser_url": "http://google.com", "renderer": renderer, "browser_papersize": browser_papersize}, + { + "browser_url": "http://google.com", + "renderer": renderer, + "browser_pdf_options": {"format": browser_papersize}, + }, ) assert b"Z" in pdf_body