jdcaballerov / weasyprint_lambda

Dockerfile image that builds AWS Lambda Layer with native libraries for running WeasyPrint HTML to PDF renderer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Weasyprint Lambda Layer for Docker

This repository contains a Dockerfile which will build a AWS Lambda layer containing the native libraries for the WeasyPrint HTML to PDF

The procedure is based upon the instructions here.

This currently only supports the Python 3.7 runtime, but should be easy to adapt by modifying the base image in the Dockerfile

How to Build

# Build the Docker image, which creates the zip file
docker build -t weasyprint .

# Create an instance of the image (without actually running it)
# this is just so we can copy the zip file out
docker run -it weasyprint /bin/bash
CTRL P  + CTRL Q  to detach
docker ps 
# container id
docker cp CONTAINERID:/opt/lib/weasyprint_lambda_layer.zip .
docker rm weasyprint

The weasyprint_lambda_layer.zip file can now be uploaded as an AWS Lambda layer

aws lambda publish-layer-version --layer-name weasyprint --region us-east-1 --compatible-runtimes python3.7 --zip-file fileb://weasyprint_lambda_layer.zip

Using the Library

The weasyprint pip package still needs to be included in the deployed package; it is not included in the zip file. Only the native (binary) libraries are included.

The deployed should now support use of WeasyPrint like the below:

from weasyprint import HTML
html = HTML(string='<html><body><h1>Hello, world</h1></body></html>')
html.write_pdf('/tmp/hello_world.pdf')

About

Dockerfile image that builds AWS Lambda Layer with native libraries for running WeasyPrint HTML to PDF renderer

License:MIT License


Languages

Language:Dockerfile 100.0%