Print your HTML to PDF via Puppeteer in a Docker container.
This is a simple Express server listening for POST
requests passing some custom HTML to print as PDF for generating fancy reports.
Technologies used:
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
Name | Description | Default Value |
---|---|---|
BODY_LIMIT | Maximum request body size. Passed on to body-parser and express.json . |
1mb |
LOG_FORMAT | Custom morgan format string. | common |
LOG_FILE | Will log all requests to this file | log.txt |
LOG_SIZE | Size from rotating-file-stream used in morgan for rotating the file based on the size | 10M |
LOG_INTERVAL | Interval from rotating-file-stream used in morgan for rotating the file | 1d |
LOG_TO_CONSOLE | Log requests to console | true |
LOG_TO_CONSOLE_LOG_LEVEL | When set to 'ERRORS' only error responses are logged to the console. | ERRORS |
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
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"
]
}'
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>
MIT. Do as you please.
Refer to the LICENSE file for more details.