Skip to content

HTML to PDF based on Chromium's render engine Puppeteer with a simple HTTP interface.

License

Notifications You must be signed in to change notification settings

dbl-works/pdf-renderer

Repository files navigation

PDF Renderer

Docker/dblworks/pdf-renderer

It's an HTTP service that will convert a HTML string to PDF. It uses Puppeteer under the hood, which means rendering is performed by Chromium's render engine; thus the HTML sent to this service should be developed and tested using Chrome/Chromium.

Read more about technical details of Puppeteer on their Github repo.

Installation

Because it's a docker image, it can be installed anywhere

To try out locally:

docker build -t pdf-renderer .
docker run --name pdf-renderer -d -p 5017:5017 pdf-renderer

If you prefer running the code in your system

⚠️ macOS Monterey runs the ControlCenter on port 5000.

Prepend e.g. PORT=5017 to the command

yarn run dev

or

yarn run local

which runs NODE_ENV=development yarn run dev.

Passing NODE_ENV=development will let puppeteer auto-detect the executable path, which is different from the hardcoded path, that is specifc for a Linux deploy.

      browser = await puppeteer.launch({
        headless: 'new',
        args: [
          '--no-sandbox',
          '--disable-setuid-sandbox',
        ],
        // executablePath: 'google-chrome-stable',
      })

Environment variables

AWS_BUCKET_NAME

AWS

To store the files to AWS we need to config the system to use aws-sdk

AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY

(this will need to be provided to you)

API Reference

Generate a PDF from HTML markup

POST /
content="<div>Hello World</div>"
filename="pdf_example.pdf"
saveFile=true
format={"landscape": false}

Parameters

content(required) - String - Html markup that will be converted to a PDF

saveFile(optional) - Boolean, true by default - Whether it should respond with a base64 encoded string for the pdf (e.g. to attach to an email provider) or upload the file as PDF to AWS S3

filename(optional) - String, current timestamp by default - The filename that the PDF file will be created.

format(optional) - Object, {} by default.

{
  "landscape": false, // - Boolean, `false` by default - Whether the PDF should be in landscape mode
}

Building

On a x86 chip

docker build -t dblworks/pdf-renderer:$TAGNAME .

On a ARM chip (for a x86 target):

docker build -t dblworks/pdf-renderer:$TAGNAME . --platform linux/amd64

Publishing

git fetch --all --tags
TAGNAME="$(git describe --abbrev=0 --tags)"

docker push dblworks/pdf-renderer:$TAGNAME

Deployment

Using AWS ECR as container registry

yarn build

docker build -t localhost/pdf-renderer .

git fetch --all --tags
LATEST_RELEASE="$(git describe --abbrev=0 --tags)"
AWS_REGION=eu-central-1
AWS_PROFILE=
AWS_ACCOUNT_ID=

aws ecr get-login-password --profile $AWS_PROFILE --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
docker tag localhost/pdf-renderer $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/pdf-renderer:$LATEST_RELEASE
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/pdf-renderer:$LATEST_RELEASE