Skip to content

Commit a56d8ea

Browse files
committed
feat(docs): update documentation with configure_splat usage
1 parent 61a9c9f commit a56d8ea

File tree

5 files changed

+75
-16
lines changed

5 files changed

+75
-16
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
dist/
12
invoke.json
23
splat/prince
34
splat/fonts/*
@@ -14,4 +15,4 @@ fonts.zip
1415
# Prince licence file
1516
license.dat
1617
# Layer working directory
17-
python/
18+
python/

README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,60 @@
11
# splat
2+
23
splat is simple aws lambda function that takes HTML/CSS/JS, invokes PrinceXML to render it and returns the resulting PDF in one of many ways.
34

45
It is intended to be a DIY docraptor of sorts.
56

67
## Installation
8+
79
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>"}`
810

911
## Ways of invoking splat
12+
1013
### Input
14+
1115
Pass content in event: `{"document_content": "<h1>Hello, World!</h1>"}`
1216
Pass content via URL: `{"doucment_url": "<h1>Hello, World!</h1>"}`
1317

1418
### Output
19+
1520
Returns PDF base64 encoded by default.
1621
To save to an s3 bucket (lambda requires permission): `{"bucket_name": "<BUCKET>"}`
1722
To save to a presigned url: `{"presigned_url": "<URL>"}`
1823

1924
### Options
25+
2026
To enable Javascript: `{"javascript": true}`
2127

2228
## PrinceXML License
29+
2330
splat will attempt to install a PrinceXML license file by default. Just drop your `license.dat` in the root directory before you build the docker container. The licence file is gitignored for your convenience.
2431
If you do not have a licence file, Prince will watermark your PDFs, and you can only use them non-commercially. See their [license page](https://www.princexml.com/purchase/license_faq/) for more information.
2532

2633
You can check the status of the licence by invoking the lambda with the `{"check_license": true}` option, and interpreting the response, you can use this to periodically check the status of the licence and raise an alert if it's about to expire, and to verify that your new licence has updated correctly.
2734

2835
## Fonts
29-
splat will add any fonts inside a `fonts.zip` file. Ensure the zip file contains a folder called `fonts` with all fonts inside. Simply drop into the root directory and build the docker container. The `fonts.zip` file is gitignored for your convenience. By default, prince comes with a small suite of liberation fonts.
36+
37+
splat will add any fonts inside a `fonts.zip` file. Ensure the zip file contains a folder called `fonts` with all fonts inside. Simply drop into the root directory and build the docker container. The `fonts.zip` file is gitignored for your convenience. By default, prince comes with a small suite of liberation fonts.
38+
39+
## Library
40+
41+
Splat can be used via the `uptick_splat` library. Install with `pip install uptick_splat`.
42+
43+
Usage:
44+
45+
```python
46+
from uptick_splat import configure_splat, pdf_with_splat
47+
48+
49+
configure_splat(
50+
function_region="ap-southeast-2",
51+
function_name="splat",
52+
default_bucket_name="your-bucket-to-upload-html-to",
53+
)
54+
55+
some_html = "<h1>test</h1>"
56+
57+
pdf_with_splat(some_html, bucket_name="test_bucket")
58+
# or
59+
pdf_with_splat(some_html)
60+
```

uptick_splat/__init__.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from .config import config # noqa
1+
from .config import config, configure # noqa
22
from .utils import SplatPDFGenerationFailure, pdf_with_splat # noqa
33

4-
__version__ = "0.1.0"
5-
64
__all__ = [
75
"config",
6+
"configure",
87
"SplatPDFGenerationFailure",
98
"pdf_with_splat",
109
"__version__",

uptick_splat/config.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,43 @@ def delete_key(bucket_name: str, path: str) -> None:
2222
logger.warning(f"Failed to delete {path} from s3: {e}")
2323

2424

25+
def configure_splat(
26+
function_region: str = "ap-southeast-2",
27+
function_name: str = "splat-prod",
28+
default_bucket_name: str = "",
29+
default_tagging: str = "ExpireAfter=1w",
30+
get_session_fn: Callable[[], Any] = get_session,
31+
delete_key_fn: Callable[[str, str], None] = delete_key,
32+
):
33+
"""Configure the splat function.
34+
35+
:param function_region: the default region for the splat function
36+
:param function_name: the name of the splat function
37+
:param default_bucket_name: the default bucket name to store html uploaded to s3
38+
:param default_tagging: the default tag to apply to html uploaded to s3
39+
:param get_session_fn: a function that returns a boto3 session
40+
:param default_key_delete_fn: a function that deletes a key from s3
41+
"""
42+
global config
43+
config = Config(
44+
function_region=function_region,
45+
function_name=function_name,
46+
default_bucket_name=default_bucket_name,
47+
default_tagging=default_tagging,
48+
get_session_fn=get_session_fn,
49+
delete_key_fn=delete_key_fn,
50+
)
51+
52+
2553
@dataclass
2654
class Config:
27-
function_region: str = "ap-southeast-2"
28-
function_name: str = "splat-prod"
29-
default_bucket_name: str = ""
30-
default_html_key: str = "ExpireAfter=1w"
55+
function_region: str
56+
function_name: str
57+
default_bucket_name: str
58+
default_tagging: str
3159

32-
get_session: Callable[[], Any] = get_session
33-
delete_key: Callable[[str, str], None] = delete_key
60+
get_session_fn: Callable[[], Any]
61+
delete_key_fn: Callable[[str, str], None]
3462

3563

36-
config = Config()
64+
configure_splat()

uptick_splat/utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def pdf_with_splat(
3434

3535
is_streaming = not bool(s3_filepath)
3636

37-
session = config.get_session()
37+
session = config.get_session_fn()
3838
lambda_client = session.client(
3939
"lambda", region_name=config.function_region, config=Config(read_timeout=120)
4040
)
@@ -62,7 +62,7 @@ def pdf_with_splat(
6262
Body=body_html,
6363
Bucket=bucket_name,
6464
Key=html_key,
65-
Tagging="ExpireAfter=1w",
65+
Tagging=config.default_tagging,
6666
)
6767

6868
document_url = s3_client.generate_presigned_url(
@@ -81,7 +81,7 @@ def pdf_with_splat(
8181
)
8282

8383
# Remove the temporary html file from s3
84-
config.delete_key(bucket_name, html_key)
84+
config.delete_key_fn(bucket_name, html_key)
8585

8686
# Check response of the invocation. Note that a successful invocation doesn't mean the PDF was generated.
8787
if response.get("StatusCode") != 200:
@@ -105,7 +105,7 @@ def pdf_with_splat(
105105
obj = s3_client.get_object(Bucket=bucket_name, Key=destination_path)
106106
pdf_bytes = obj["Body"].read()
107107
try:
108-
config.delete_key(bucket_name, destination_path)
108+
config.delete_key_fn(bucket_name, destination_path)
109109
except Exception as e:
110110
pass
111111
return cast(bytes, pdf_bytes)

0 commit comments

Comments
 (0)