Skip to content

Commit

Permalink
Merge pull request #18 from uptick/feat/papersize-3
Browse files Browse the repository at this point in the history
feat(playwright): switch to configurable options
  • Loading branch information
uptickmetachu authored May 16, 2024
2 parents 5dfb0df + e24c4c8 commit 9a9032c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
19 changes: 7 additions & 12 deletions lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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,
)


Expand All @@ -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,
)


Expand All @@ -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,
)


Expand Down
18 changes: 15 additions & 3 deletions tests/test_lambda_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -77,15 +81,23 @@ 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": "<h1>Z</h1>", "renderer": renderer, "browser_papersize": browser_papersize},
{
"document_content": "<h1>Z</h1>",
"renderer": renderer,
"browser_pdf_options": {"format": browser_papersize},
},
)

assert b"Z" in pdf_body
assert status_code == 200

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
Expand Down

0 comments on commit 9a9032c

Please sign in to comment.