/lambda-pdf-generator

A lambda function that generates PDF with headless chrome

Primary LanguageHCL

lambda-pdf-generator

A lambda function that generates PDF with headless chrome on lambda

Features

  • Generates PDF from HTML string
  • Generates PDF from URL
  • Persists generated PDF to S3
  • CJK font support (no more tofu)

packaging

$ make package

will generates lambda_function.zip

Deployment

see example for terraform deployment example

Parameter

Input

parameter description default required
data HTML string you want to convert to pdf. e.g. <html><body>Hello World!</body></html> "" no
url Web site URL you want to print as pdf. If specified, data will be ignore. URL must accessible from lambda. e.g. https://www.google.com/ null no
persisted If you would like to persist pdf files on S3, turn on true false no
s3Bucket Destination S3 bucket. Required If persisted is true null no
s3Key Destination S3 path. Required If persisted is true null no

Output

parameter description
data Base64 encoded pdf. e.g. {"data":"JVBERi0xLjQKJdPr6eEKMSAw~~eEKMSAw"}

Example

  • Generate pdf from HTML
$ echo '{"data": "<html><body><p style=\"color: red\">Hello World</p></body></html>"}' > payload.json
$ aws lambda invoke \
 --function-name <your_deployed_lambda_function_name> \
 --invocation-type RequestResponse \
 --payload file://payload.json --cli-binary-format raw-in-base64-out response.json

 $ cat response.json | jq -r .data | base64 -d > example.pdf
 $ open example.pdf

image

The output result is same as Save as PDF result on Chrome (Ctrl+P)

  • Generate PDF from url
$ echo '{"url": "https://amazon.com"}' > payload.json
$ aws lambda invoke \
 --function-name <your_deployed_lambda_function_name> \
 --invocation-type RequestResponse \
 --payload file://payload.json --cli-binary-format raw-in-base64-out response.json

 $ cat response.json | jq -r .data | base64 -d > example.pdf
 $ open example.pdf

image

Fonts

If you want to use CJK fonts in PDF like Simplified Chinese (简体中文), Traditional Chinese — Taiwan (繁體中文—臺灣), Japanese (日本語), Korean (한국어),

You can use lambda-cjk-font-layer to enable CJK font.

Troubleshoot

According to AWS Lambda limitation, if input payload size or response payload size exceeded 6MB, Lambda API will returns error