Print your HTML to PDF through a Web server.
This is a simple Express server listening for POST requests passing some arbitrary HTML to print as PDF for generating fancy reports.
Technologies used:
It offers images for both ARM and AMD architectures.
This example (which you can copy and directly paste into your shell interpreter) will run it on port 3000 and print a simple HTML document to PDF.
docker run --name html2pdf --detach --publish=3000:3000 --memory 500M ghcr.io/ccjmne/puppeteer-html2pdf:latest sleep 1 # Or for a quick test, to be killed with Ctrl-C: # docker run -it --rm -p=3000:3000 ghcr.io/ccjmne/puppeteer-html2pdf:latest curl localhost:3000 -H 'Content-Type: text/html' --data ' <h1>Hello World!</h1> <blockquote> I love deadlines. I like the whooshing sound they make as they fly by. <br> <small style="float:right">— Douglas Adams</small> </blockquote> ' > out.pdf && xdg-open out.pdf| Name | Description | Default Value |
|---|---|---|
| BODY_LIMIT | Maximum request body size. Passed on to body-parser and express.json. | 1mb |
| BROWSER_KEEPALIVE | Period (in ms) of inactivity after which the shared browser instance is shut down. | 30000 (30s) |
The simplest way to add fonts is to mount a volume with the fonts you want to use to the /usr/share/fonts directory in the container.
# Obtain a font longcang=$(curl -L 'https://fonts.googleapis.com/css2?family=Long+Cang' | grep -Po '(?<=url\()[^)]+') curl -L "$longcang" -o LongCang-Regular.ttf # Add it to the container docker run -it --rm -p3000:3000 \ -v./LongCang-Regular.ttf:/usr/local/share/fonts/LongCang-Regular.ttf \ ghcr.io/ccjmne/puppeteer-html2pdf:latest # Use it in your HTML curl localhost:3000 -H 'Content-Type: text/html' --data ' <html> <body style="font-family: '\''Long Cang'\'', cursive;"> <h1>I play nice with custom fonts, too!</h1> <p>鉴于对人类家庭所有成员的固有尊严及其平等的和不移的权利的承认,乃是世界自由、正义与和平的基础</p> </body> </html>' > out.pdf && xdg-open out.pdfThe Web server listens on a port of your choosing (see the Quick Start 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 | HTML content | JSON array of strings containing HTML |
| Request Body (example) | <h1>Hello World!</h1> | ["<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.pdfextension if absent)- all the options supported by Puppeteer's page#pdf([options]), except:
pathheaderTemplatemargin
Single-page document, default settings (format: A4, orientation: portrait):
curl '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 '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 'http://localhost:3000/multiple' \ -H 'Content-Type: application/json' \ -d '[ "<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>
- Support printing screenshots as images
- Rename to html-printer?
- Provide as plain TypeScript library rather than a Web server
- Publish library to npm
MIT. Do as you please.
Refer to the LICENSE file for more details.