An API service for generating PDFs from the given html.
cheesecake87/pdf-generator-service:latest
or
cheesecake87/pdf-generator-service-gfonts:latest
-gfonts image is VERY large (~3GB)
This service uses Flask + granian (a Rust HTTP server) and WeasyPrint or the Chromium web browser to generate PDFs from the given html.
Chromium is set up to print in A4 pages, and Weasyprint is a little more flexible and allows you to pass in the page size via css.
The service listens on port 9898 by default, but can be changed
by setting the environment variable PDFGS_PORT. Although it is
recommended to use dockers --publish flag, somthing like:
docker run --publish 127.0.0.1:9898:9898/tcp cheesecake87/pdf-generator-service:latestYou would then sit this behind a reverse proxy. Or use it directly from other services.
Weasyprint
curl -X POST -F "html=<h1>Hello, World!</h1>" http://localhost:9898/pdfor
Chromium
curl -X POST -F "html=<h1>Hello, World!</h1>" http://localhost:9898/chromium/pdfBoth routes will return a PDF file as Content-Type application/pdf
Weasyprint
curl -X POST -H "Content-Type: application/json" -d '{"html": "<h1>Hello, World!</h1>"}' http://localhost:9898/pdfor
Chromium
curl -X POST -H "Content-Type: application/json" -d '{"html": "<h1>Hello, World!</h1>"}' http://localhost:9898/chromium/pdfBoth routes will return:
{
"pdf": "<base64_encoded_pdf>"
}When passing in html with css, it's best to use the style tag in the head:
<head>
...
<style>
.styles-here {
...
}
</style>
</head>You can adjust the page settings, and add fonts by doing:
<style>
@page {
size: A4 !important;
padding: 0 !important;
margin: 2rem !important;
}
@font-face {
font-family: 'OpenSans';
src: url('https://<url_to_font>/OpenSans.ttf') format('truetype');
}
...
</style>The cheesecake87/pdf-generator-service-gfonts:latest image includes all the
fonts from Google Fonts.
To access these fonts use the following css:
<style>
@page {
size: A4 !important;
padding: 0 !important;
margin: 2rem !important;
}
@font-face {
font-family: 'Font Name Here';
src: url('file:///fonts/MonsieurLaDoulaise-Regular.ttf') format('truetype');
}
p {
font-family: 'Font Name Here';
}
...
</style>A list of all the available fonts can be found in the FONTS.md file.
If you set the environment variable PDFGS_X_API_KEY, this will set the
/pdf and /chromium/pdf routes
to look for the header X-API-KEY and check if it matches the value of
PDFGS_X_API_KEY.
docker run --publish 127.0.0.1:9898:9898/tcp -e PDFGS_X_API_KEY=<your_api_key> cheesecake87/pdf-generator-service:latestcurl -X POST -H "X-API-KEY: <your_api_key>" -H "Content-Type: application/json" -d '{"html": "<html_string>"}' http://localhost:9898/pdfPDFGS_PORT- The port to listen on.PDFGS_X_API_KEY- The API key value to use to secure the/pdfroute.PDFGS_IN_TESTING- If set totrue, the service will enable the/testand/test-api-keyroutes.
Please see the WeasyPrint documentation for more information on how the
passed html works.
https://doc.courtbouillon.org/weasyprint/stable/common_use_cases.html