Skip to content

alexastall/puppeteer-html2pdfgg

 
 

Repository files navigation

ccjmne/puppeteer-html2pdf

Print your HTML to PDF via Puppeteer in a Docker container.

Docker Image CI

Inside the box

This is a simple Express server listening for POST requests passing some custom HTML to print as PDF for generating fancy reports.

Technologies used:

Run it

As a webserver, on the port of your choosing.

  • Testing:
docker run -it --rm -p=<port>:3000 ghcr.io/ccjmne/puppeteer-html2pdf:<version>

Kill with: Ctrl^C

  • Production:
docker run --name html2pdf --detach -p=<port>:3000 \
           --shm-size 1G --sysctl net.ipv6.conf.all.disable_ipv6=1 \
           ghcr.io/ccjmne/puppeteer-html2pdf:<version>

Stop with: docker stop html2pdf

Docker Environment Variables

Name Description Default Value
BODY_LIMIT Maximum request body size. Passed on to body-parser and express.json. 1mb

Use it

The webserver listens on the port (specified in the Run it section) and exposes two endpoints:

Single-page document, default settings (format: A4, orientation: portrait):

Single-page document Multi-page document
Request Path / /multiple
Request Method POST POST
Content-Type header text/html application/json
Request Body <h1>Hello World!</h1> { pages: ["<h1>Page 1</h2>", "<h1>Page 2</h1>" ] }

Both methods handle the following query parameters:

  • filename: the name of the resulting PDF file (will automatically append the .pdf extension if absent)
  • all the options supported by Puppeteer's page#pdf([options]), except:
    • path
    • headerTemplate
    • margin

Examples

Single-page document, default settings (format: A4, orientation: portrait):

curl -X POST \
  'http://localhost:3000' \
  -H 'Content-Type: text/html' \
  -d '<html><body><h1>Hello World!</h1></body></html>'

Single-page document (format: A3, orientation: landscape):

curl -X POST \
  'http://localhost:3000?format=a3&landscape=true' \
  -H 'Content-Type: text/html' \
  -d '<html><body><h1>Hello World!</h1></body></html>'

Multi-page document:

curl -X POST \
  'http://localhost:3000/multiple' \
  -H 'Content-Type: application/json' \
  -d '{
    "pages": [
        "<html><body><h1>Hello World!</h1></body></html>",
        "This is the <strong>second</strong> page"
    ]
}'

Build

Automatically builds and publishes to GitHub Packages (GitHub Container Registry) with each GitHub Release.

Includes a comprehensive script that lets you build and publish new versions of the image: ./compose.sh <version>

License

MIT. Do as you please.
Refer to the LICENSE file for more details.

About

Print your HTML to PDF via Puppeteer in a Docker container.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • JavaScript 64.5%
  • Shell 26.2%
  • Dockerfile 9.3%