-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
461 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,35 @@ | ||
# AWS Lambda to print pdfs | ||
# Cloud Print Utils | ||
|
||
## WeasyPrint | ||
|
||
[WeasyPrint](https://weasyprint.org/) is python based pdf/png print service. | ||
|
||
### Lambda Layer | ||
|
||
To build a layer you need **docker** installed on your system. | ||
This layer supports only amazon linux 2 runtimes. | ||
|
||
Build layer: | ||
|
||
$ make build/weasyprint-layer-python3.8.zip | ||
|
||
# for future runtimes, eg: python3.9 | ||
# RUNTIME=python3.9 make build/weasyprint-layer-python3.9.zip | ||
|
||
# to test your build run | ||
$ make test.weasyprint | ||
This is a collection of AWS Lambda layers and functions to render pdf documents | ||
and images from HTML. | ||
|
||
Deploy layer (see below for full stack deployment): | ||
Currently solutions based on these tools available: | ||
|
||
$ aws lambda publish-layer-version \ | ||
--region <region> \ | ||
--layer-name <name> \ | ||
--zip-file fileb://build/weasyprint-layer-python3.8.zip | ||
- [WeasyPrint](https://weasyprint.org/) | ||
- [wkhtmltopdf](https://wkhtmltopdf.org/) | ||
|
||
Environment variables expected by layer: | ||
To build a layer you need **make** and **docker** installed on your system. | ||
The layers support only amazon linux 2 runtimes, eg. python3.8, nodejs12.x. | ||
|
||
GDK_PIXBUF_MODULE_FILE="/opt/lib/loaders.cache" | ||
FONTCONFIG_PATH="/opt/fonts" | ||
XDG_DATA_DIRS="/opt/lib" | ||
By default only minimal subset of system fonts [installed](fonts/layer_builder.sh) if you need more fonts | ||
set `EXTRA_FONTS` env variable with space separated list | ||
of font packages to install, eg: | ||
|
||
### Lambda Function | ||
EXTRA_FONTS="liberation-fonts xorg-x11-fonts-cyrillic" make build/weasyprint-layer-python3.8.zip | ||
|
||
Simple lambda function [provided](weasyprint/lambda_function.py), it requires `BUCKET=<bucket name>` env variable if files stored on s3. | ||
You can search for available font packages with `make fonts.list`. | ||
|
||
Example payload to print pdf from url and return link to s3: | ||
|
||
{"url": "https://weasyprint.org/samples/report/report.html", "filename": "report.pdf"} | ||
|
||
Example paylod to print png from html and css data and return png content encoded in base64: | ||
|
||
{"html": "<html><h1>Header</h1></html>", "css": ["h1 { color: red }"], "filename": "report.png", "return": "base64"} | ||
|
||
### CloudFormatin Deployment | ||
|
||
**WARNING** review [code](cdk-stacks/lib/weasyprint-stack.ts) before deployment. | ||
|
||
Stack includes: printer lambda function and s3 bucket where files are stored. | ||
|
||
We use [CDK](https://docs.aws.amazon.com/cdk/latest/guide/home.html) to deploy stack, thus you need nodejs installed to run it: | ||
## WeasyPrint | ||
|
||
# build | ||
$ cd cdk-stacks && npm install && npm run build | ||
# view diff | ||
$ cdk diff --stack WeasyPrintStack --parameters uploadBucketName=<bucket name> | ||
# deploy | ||
$ cdk deploy --stack WeasyPrintStack --parameters uploadBucketName=<bucket name> | ||
[WeasyPrint](https://weasyprint.org/) is python based pdf/png print service. | ||
|
||
To test your deployment: | ||
Run `make build/weasyprint-layer-python3.8.zip` to build a layer, for details | ||
see related [readme](weasyprint/README.md). | ||
|
||
# invoke function | ||
$ aws lambda invoke --function-name weasyprint-print \ | ||
--payload '{"url": "https://weasyprint.org/samples/report/report.html", "filename": "report.pdf"}' \ | ||
--log-type Tail --query 'LogResult' --output text out | base64 -d | ||
## wkhtmltopdf | ||
|
||
# view output | ||
$ cat out | ||
{"statusCode": 200, "body": "https://your-bucket.s3.amazonaws.com/report.pdf?signature..."} | ||
[wkhtmltopdf](https://wkhtmltopdf.org/) is a comand line tool that renders HTML | ||
into PDF and various image formats using the Qt WebKit rendering engine. | ||
|
||
# open in browser | ||
$ chromium-browser $(cat out | jq .body | tr -d '"') | ||
Run `make build/wkhtmltox-layer.zip` to build a layer, for details | ||
see related [readme](wkhtmltox/README.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,37 @@ | ||
# Example CDK Stack | ||
# Infrastructure as Code example | ||
|
||
This is an example app that you can use to deploy aws stacks. | ||
Stack defined in `lib/`, app defined in `bin/`. | ||
We use [CDK](https://docs.aws.amazon.com/cdk/latest/guide/home.html) to deploy stack, thus you need nodejs installed to run it. | ||
|
||
## Useful commands | ||
Each stack consist of printer lambda function, layer and s3 bucket where files are stored. | ||
You must build layer before running deploy. | ||
|
||
- `npm run build` compile typescript to js | ||
- `npm run watch` watch for changes and compile | ||
- `npm run test` perform the jest unit tests | ||
- `cdk deploy` deploy this stack to your default AWS account/region | ||
- `cdk diff` compare deployed stack with current state | ||
- `cdk synth` emits the synthesized CloudFormation template | ||
We provide two stacks `WeasyPrintStack` and `WkhtmltoxStack`, below is deployment | ||
example of WeasyPrintStack. | ||
|
||
**WARNING** review code before deployment. | ||
|
||
# build (rebuild must be done on every change in typescript files) | ||
$ cd cdk-stacks && npm install && npm run build | ||
|
||
# view diff WeasyPrintStack | ||
$ npm run cdk diff WeasyPrintStack | ||
|
||
# deploy WeasyPrintStack | ||
$ npm run cdk deploy WeasyPrintStack --parameters uploadBucketName=<bucket name> | ||
|
||
# generate synthesized CloudFormation template | ||
$ npm run cdk synth WeasyPrintStack | ||
|
||
To test your deployment: | ||
|
||
# invoke function | ||
$ aws lambda invoke --function-name cloud-print \ | ||
--payload '{"url": "https://weasyprint.org/samples/report/report.html", "filename": "report.pdf"}' \ | ||
--log-type Tail --query 'LogResult' --output text out | base64 -d | ||
|
||
# view output | ||
$ cat out | ||
{"statusCode": 200, "body": "https://your-bucket.s3.amazonaws.com/report.pdf?signature..."} | ||
|
||
# open in browser | ||
$ chromium-browser $(cat out | jq .body | tr -d '"') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.