Skip to content

JoeChapman/html-pdf-converter

 
 

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 8001:8001 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:8001/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:8001/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 8001
APP_HOST:    Defaults to 'localhost'

About

Convert HTML to a PDF

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • JavaScript 100.0%