Skip to content

UKHomeOffice/html-pdf-converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b25bbc8 · Jul 18, 2023
Nov 18, 2022
Nov 18, 2022
Apr 15, 2020
Nov 18, 2022
Nov 18, 2022
Apr 1, 2019
Jan 24, 2023
Apr 1, 2019
Nov 18, 2022
Jul 15, 2022
Nov 18, 2022
Nov 18, 2022
Mar 23, 2017
Jun 29, 2023
Jul 20, 2022
Jun 21, 2023
Mar 9, 2018
Apr 1, 2019
Jul 18, 2023
Jun 21, 2023

Repository files navigation

HTML PDF Converter

Docker Repository on Quay Build Status Build Status

Uses Chrome Headless to convert HTML to a PDF

Send a HTML or Mustache template and recieve a PDF stream as the response.

Install and start

Node App

docker pull quay.io/ukhomeofficedigital/html-pdf-converter
docker run -p 8080:8080 html-pdf-converter

Development

git clone git@github.com:UKHomeOffice/html-pdf-converter.git
cd html-pdf-converter
npm install
npm start

Example usage

Mustache and Data

curl -H "Content-Type:application/json" \
     -d '{
            "template": "'"\
              <html>\
                <head>\
                  <title>{{title}}</title>\
                </head>\
                <body>\
                  <h1>{{header}}</h1>\
                  <p>{{para}}</p>\
                </body>\
              </html>\
            "'",
            "data": {
              "title": "My title",
              "header": "My header",
              "para": "My content"
            }
         }' \
     -i localhost:8080/convert

HTML

curl -H "Content-Type:application/json" \
     -d '{
            "template": "'"\
              <html>\
                <head>\
                  <title>My title</title>\
                </head>\
                <body>\
                  <p>Hello world</p>\
                </body>\
              </html>\
            "'"
         }' \
     -i localhost:8080/convert

Response (example)

%PDF-1.4\n1 0 obj\n<<\n/Title ...

PDF Options

Chrome can accept a number of options to its PDF render function. These are documented here: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions

These can be set on a per-request basis by passing a pdfOptions object as part of your request body.

{
  "template":"<h1>Hello World!</h1>",
  "pdfOptions": {
    "printBackground": true
  }
}

External Resources

This service cannot resolve external resources such as linked CSS, JavaScript or images. If your template includes links to any of these resources, we suggest you use https://github.com/remy/inliner. The source for Inliner can be a URL, a file location or an HTML string.

Environment Variables

APP_PORT:    Defaults to 8080
APP_HOST:    Defaults to 'localhost'

Troubleshooting

If you get the following error locally, html-pdf-converter: Handling error message=Could not find browser revision 756035. Run "npm install" or "yarn install" to download a browser binary.

Then you may need to manually install puppeteer npm i puppeteer