Skip to content

Commit

Permalink
Merge pull request #16 from uptick/feat/papersize
Browse files Browse the repository at this point in the history
feat(playwright): add papersize argument
  • Loading branch information
uptickmetachu authored May 16, 2024
2 parents 473e124 + 6a70de6 commit 901f7f9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ This can be done via a function_url, apigateway or lambda invoke.

Event payload body.

|Field | Type| Description|
|---|---|---|
| **javascript (princexml)** | boolean (False) | Enables [princeXML's javascript execution](https://www.princexml.com/doc/javascript/). This will not render react but can be used for formatting. |
| **check_license** | boolean (False) | Send this field to receive a check on remaining license usage |
| **document_content** | string | Embed the html content in the payload. There will be AWS payload size limitations.|
| **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`|
| **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.|
| Field | Type | Description |
|----------------------------|-----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **javascript (princexml)** | boolean (False) | Enables [princeXML's javascript execution](https://www.princexml.com/doc/javascript/). This will not render react but can be used for formatting. |
| **check_license** | boolean (False) | Send this field to receive a check on remaining license usage |
| **document_content** | string | Embed the html content in the payload. There will be AWS payload size limitations. |
| **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 | |
| **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. |

### Input

Expand Down
21 changes: 18 additions & 3 deletions lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ 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 @@ -53,6 +58,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
renderer: Renderers = Renderers.princexml

# Output parameters
Expand Down Expand Up @@ -151,9 +157,11 @@ def _playwright_visit_page(browser_url: str, headers: dict) -> Iterator[playwrig
yield page


def playwright_page_to_pdf(browser_url: str, headers: dict, output_filepath: str) -> None:
def playwright_page_to_pdf(
browser_url: str, headers: dict, output_filepath: str, papersize: Papersize = Papersize.A4
) -> None:
with _playwright_visit_page(browser_url, headers) as page:
page.pdf(path=output_filepath, format="A4")
page.pdf(path=output_filepath, format=papersize)


def playwright_page_to_html_string(browser_url: str, headers: dict) -> str:
Expand All @@ -175,6 +183,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,
)


Expand All @@ -197,6 +206,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,
)


Expand All @@ -212,7 +222,12 @@ def pdf_from_browser_url(payload: Payload, output_filepath: str) -> None:
output_filepath,
)
else:
playwright_page_to_pdf(payload.browser_url, payload.browser_headers, output_filepath)
playwright_page_to_pdf(
payload.browser_url,
payload.browser_headers,
output_filepath,
payload.browser_papersize,
)


def execute(cmd: list[str]) -> None:
Expand Down
13 changes: 7 additions & 6 deletions tests/test_lambda_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ def test_check_license_returns_a_license_payload() -> None:


@pytest.mark.parametrize("renderer", ["princexml", "playwright"])
@pytest.mark.parametrize("browser_papersize", ["A4", "letter"])
class TestRenderers:
def test_generating_pdf_from_document_url(self, renderer: str):
def test_generating_pdf_from_document_url(self, renderer: str, browser_papersize: str):
s3_client = get_s3_client()

key = gen_temp_key()
Expand All @@ -67,24 +68,24 @@ def test_generating_pdf_from_document_url(self, renderer: str):
)

status_code, _, pdf_body = call_lamdba(
{"document_url": document_url, "renderer": renderer},
{"document_url": document_url, "renderer": renderer, "browser_papersize": browser_papersize},
)

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

def test_generating_pdf_from_document_content(self, renderer: str):
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},
{"document_content": "<h1>Z</h1>", "renderer": renderer, "browser_papersize": browser_papersize},
)

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

def test_generating_pdf_from_browser_url(self, renderer: str):
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_url": "http://google.com", "renderer": renderer, "browser_papersize": browser_papersize},
)

assert b"Z" in pdf_body
Expand Down

0 comments on commit 901f7f9

Please sign in to comment.