Skip to content

Commit b771ca9

Browse files
committed
docs: improve documentation
1 parent 3821f92 commit b771ca9

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

README.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,39 @@ It is intended to be a DIY docraptor of sorts.
88

99
Simply build the docker image, deploy it to AWS, then invoke the lambda with an event body JSON that performs the desired operation. For example: `{"document_content": "<h1>Hello, World!</h1>"}`
1010

11-
## Ways of invoking splat
11+
This can be done via a function_url, apigateway or lambda invoke.
12+
13+
## Invoking splat
14+
15+
Event payload body.
16+
17+
|Field | Type| Description|
18+
|---|---|---|
19+
| **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. |
20+
| **check_license** | boolean (False) | Send this field to receive a check on remaining license usage |
21+
| **document_content** | string | Embed the html content in the payload. There will be AWS payload size limitations.|
22+
| **document_url**| url | Fetch the html content from `document_url` to disk before rendering.|
23+
| **browser_url** | url | Browser the `browser_url` with `playwright` before rendering with `renderer`|
24+
| **browser_headers**| Mapping[str,str] | Add additional headers to playwright before visiting `browser_url`|
25+
| **renderer**| `princexml` or `playwright`| Renderer to render the html with |
26+
| **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`|
27+
| **presigned_url**| url | Output the resulting pdf to the presigned url. Generate the presigned url with `put_object`. See Output for more information.|
1228

1329
### Input
1430

1531
Pass content in event: `{"document_content": "<h1>Hello, World!</h1>"}`
16-
Pass content via URL: `{"doucment_url": "<h1>Hello, World!</h1>"}`
32+
33+
Pass content via URL: `{"document_url": "https://some_page/report.html"}`
34+
35+
Pass content via Browser page: `{"browser_url": "https://some_react_page/", "renderer": "princexml", "browser_headers": {"Authorization": "Bearer SOME_BEARER_TOKEN"}}`
1736

1837
### Output
1938

2039
Returns PDF base64 encoded by default.
21-
To save to an s3 bucket (lambda requires permission): `{"bucket_name": "<BUCKET>"}`
22-
To save to a presigned url: `{"presigned_url": "<URL>"}`
2340

24-
### Options
41+
To save to an s3 bucket (lambda requires permission): `{"bucket_name": "<BUCKET>"}`
2542

26-
To enable Javascript: `{"javascript": true}`
43+
To save to a presigned url: `{"presigned_url": "<URL>"}`
2744

2845
## PrinceXML License
2946

lambda_function.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232

3333
class Renderers(str, enum.Enum):
3434
playwright = "playwright"
35-
prince = "prince"
35+
princexml = "princexml"
3636

3737

3838
class Payload(pydantic.BaseModel):
39+
# NOTE: When updating this model, also update the equivalent documentation
3940
# General Parameters
4041
javascript: bool = False
4142
check_license: bool = False
@@ -48,7 +49,7 @@ class Payload(pydantic.BaseModel):
4849
## Browse the document in a browser before rendering
4950
browser_url: str | None = None
5051
browser_headers: dict = pydantic.Field(default_factory=dict)
51-
renderer: Renderers = Renderers.prince
52+
renderer: Renderers = Renderers.princexml
5253

5354
# Output parameters
5455
bucket_name: str | None = None
@@ -126,7 +127,7 @@ def pdf_from_document_content(payload: Payload, output_filepath: str) -> None:
126127
assert payload.document_content
127128
temporary_html_file.write(payload.document_content)
128129
temporary_html_file.flush()
129-
if payload.renderer == Renderers.prince:
130+
if payload.renderer == Renderers.princexml:
130131
prince_handler(temporary_html_file.name, output_filepath, payload.javascript)
131132
else:
132133
playwright_page_to_pdf(f"file://{temporary_html_file.name}", payload.browser_headers, output_filepath)
@@ -144,7 +145,7 @@ def pdf_from_document_url(payload: Payload, output_filepath: str) -> None:
144145
with tempfile.NamedTemporaryFile(mode="w", suffix=".html") as temporary_html_file:
145146
temporary_html_file.write(response.content.decode("utf-8"))
146147
temporary_html_file.flush()
147-
if payload.renderer == Renderers.prince:
148+
if payload.renderer == Renderers.princexml:
148149
prince_handler(temporary_html_file.name, output_filepath, payload.javascript)
149150
else:
150151
playwright_page_to_pdf(f"file://{temporary_html_file.name}", payload.browser_headers, output_filepath)
@@ -155,9 +156,9 @@ def pdf_from_browser_url(payload: Payload, output_filepath: str) -> None:
155156
print("splat|pdf_from_browser_url")
156157
# First we need to visit the browser with playwright and save the html
157158
assert payload.browser_url
158-
if payload.renderer == Renderers.prince:
159+
if payload.renderer == Renderers.princexml:
159160
html = playwright_page_to_html_string(payload.browser_url, payload.browser_headers)
160-
pdf_from_document_content(Payload(document_content=html, renderer=Renderers.prince), output_filepath)
161+
pdf_from_document_content(Payload(document_content=html, renderer=Renderers.princexml), output_filepath)
161162
else:
162163
playwright_page_to_pdf(payload.browser_url, payload.browser_headers, output_filepath)
163164

tests/test_lambda_e2e.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_check_license_returns_a_license_payload() -> None:
5353
assert body["is_demo_license"] is False
5454

5555

56-
@pytest.mark.parametrize("renderer", ["prince", "playwright"])
56+
@pytest.mark.parametrize("renderer", ["princexml", "playwright"])
5757
class TestRenderers:
5858
def test_generating_pdf_from_document_url(self, renderer: str):
5959
s3_client = get_s3_client()

0 commit comments

Comments
 (0)