Chromium
The Chromium module interacts with the Chromium browser to convert HTML documents to PDF.
It starts a dedicated Chromium instance for each request.
The amd64
Docker image uses Google Chrome stable, while other architectures use an earlier version of Chromium.
Until Google or Debian packages' managers provide a more up-to-date version of Chromium for arm64
, armhf
, and i386
platforms, the Docker images will differ, alas.
Properties
- Security
- Health Check
- Network
- JavaScript
- Disable
--chromium-allow-file-access-from-files bool Allow file:// URIs to read other file:// URIs
--chromium-allow-insecure-localhost bool Ignore TLS/SSL errors on localhost
--chromium-allow-list string Set the allowed URLs for Chromium using a regular expression
--chromium-deny-list string Set the denied URLs for Chromium using a regular expression (default "^file:///[^tmp].*")
--chromium-ignore-certificate-errors bool Ignore the certificate errors
--chromium-disable-web-security bool Don't enforce the same-origin policy
--chromium-incognito bool Start Chromium with incognito mode
--chromium-failed-starts-threshold int Set the number of consecutive failed starts after which the module is considered unhealthy - 0 means ignore (default 5)
--chromium-host-resolver-rules string Set custom mappings to the host resolver
--chromium-proxy-server string Set the outbound proxy server; this switch only affects HTTP and HTTPS requests
--chromium-disable-javascript bool Disable JavaScript
--chromium-disable-routes bool Disable the routes
--chromium-user-agent
property is deprecated. Prefer the userAgent
form field instead.
Routes
Each route accepts multipart/form-data
requests.
They share the following form fields:
- Page Properties
- Header & Footer
- Wait
- HTTP Headers
- JavaScript
- CSS
- PDF Format
paperWidth float Paper width, in inches (default 8.5)
paperHeight float Paper height, in inches (default 11)
marginTop float Top margin, in inches (default 0.39)
marginBottom float Bottom margin, in inches (default 0.39)
marginLeft float Left margin, in inches (default 0.39)
marginRight float Right margin, in inches (default 0.39)
preferCssPageSize bool Define whether to prefer page size as defined by CSS (default false)
printBackground bool Print the background graphics (default false)
omitBackground bool Hide the default white background and allow generating PDFs with transparency (default false)
landscape bool Set the paper orientation to landscape (default false)
scale float The scale of the page rendering (default 1.0)
nativePageRanges string Page ranges to print, e.g., '1-5, 8, 11-13' - empty means all pages
For instance:
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/url' \
--form 'url="https://my.url"' \
--form 'paperWidth="8.27"' \
--form 'paperHeight="11.7"' \
--form 'marginTop="1"' \
--form 'marginBottom="1"' \
--form 'marginLeft="1"' \
--form 'marginRight="1"' \
--form 'preferCssPageSize="false"' \
--form 'printBackground="true"' \
--form 'omitBackground="true"' \
--form 'landscape="true"' \
--form 'scale="2.0"' \
--form 'nativePageRanges="1-5"' \
-o my.pdf
Examples of paper size (width x height):
Letter
- 8.5 x 11 (default)Legal
- 8.5 x 14Tabloid
- 11 x 17Ledger
- 17 x 11A0
- 33.1 x 46.8A1
- 23.4 x 33.1A2
- 16.54 x 23.4A3
- 11.7 x 16.54A4
- 8.27 x 11.7A5
- 5.83 x 8.27A6
- 4.13 x 5.83
The rules regarding the printBackground
and omitBackground
form fields are the following:
- If
printBackground
is set to false, no background is printed. - If
printBackground
is set to true:- If the HTML document has a background, that background is used.
- If not:
- If
omitBackground
is set to true, the default background is transparent. - If not, the default white background is used.
- If
Each route accepts files named header.html
and footer.html
.
For instance:
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/url' \
--form 'url="https://my.url"' \
--form 'files=@"/path/to/header.html"' \
--form 'files=@"/path/to/footer.html"' \
--form 'marginTop="1"' \
--form 'marginBottom="1"' \
-o my.pdf
Each of them has to be a complete HTML document:
<html>
<head>
<style>
body {
font-size: 8rem;
margin: 4rem auto;
}
</style>
</head>
<body>
<p><span class="pageNumber"></span> of <span class="totalPages"></span></p>
</body>
</html>
The following classes allow you to inject printing values:
date
- formatted print date.title
- document title.url
- document location.pageNumber
- current page number.totalPages
- total pages in the document.
Make sure that:
- Margins top and bottom are large enough (
marginTop
andmarginBottom
form fields). - The font size is big enough.
There are some limitations:
- No JavaScript.
- The CSS properties are independent of the ones from the HTML document.
footer.html
CSS properties override the ones fromheader.html
.- Only fonts installed in the Docker image are loaded - see the Fonts chapter.
- Images only work using a base64 encoded source - i.e.,
data:image/png;base64, iVBORw0K...
. background-color
andcolor
CSS properties require an additional-webkit-print-color-adjust: exact
CSS property in order to work.- Assets are not loaded (i.e., CSS files, scripts, fonts, etc.).
- Background form fields do not apply.
waitDelay duration Duration to wait when loading an HTML document before converting it to PDF
waitForExpression string The JavaScript expression to wait before converting an HTML document to PDF until it returns true
waitDelay
When the page relies on JavaScript for rendering, and you don't have access to the page's code, you may want to wait a certain amount of time to make sure Chromium has fully rendered the page you're trying to generate.
For instance:
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/url' \
--form 'url="https://my.url"' \
--form 'waitDelay="5s"' \
-o my.pdf
waitForExpression
You may also wait until a given JavaScript expression returns true.
For instance:
// Somewhere in the HTML document.
await promises()
window.status = 'ready'
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/url' \
--form 'url="https://my.url"' \
--form 'waitForExpression="window.status === '\''ready'\''"' \
-o my.pdf
Prefer this option over waitDelay
.
The form field waitWindowStatus
is deprecated in favor of waitForExpression
.
These form fields do not work if JavaScript is disabled via --chromium-disable-javascript
.
userAgent string Override the default User-Agent header
extraHttpHeaders string HTTP headers to send by Chromium while loading the HTML document (JSON format)
For instance:
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/url' \
--form 'url="https://my.url"' \
--form 'userAgent="Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"' \
--form 'extraHttpHeaders="{\"MyHeader\": \"MyValue\"}"' \
-o my.pdf
failOnConsoleExceptions bool Return a 409 Conflict response if there are exceptions in the Chromium console (default false)
For instance:
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/html' \
--form 'files=@"/path/to/index.html"' \
--form 'failOnConsoleExceptions="true"'
Status: 409 Conflict
Content-Type: text/plain; charset=UTF-8
Gotenberg-Trace: {trace}
Body:
Chromium console exceptions:
exception "Uncaught" (17:10): Error: Exception 1
at file:///tmp/db09d2c8-31e3-4058-9923-c2705350f2b3/index.html:18:11;
exception "Uncaught" (20:10): Error: Exception 2
at file:///tmp/db09d2c8-31e3-4058-9923-c2705350f2b3/index.html:21:11:
This form field does not work if JavaScript is disabled via --chromium-disable-javascript
.
emulatedMediaType string The media type to emulate, either "screen" or "print" - empty means "print"
For instance:
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/url' \
--form 'url="https://my.url"' \
--form 'emulatedMediaType="screen"' \
-o my.pdf
Some websites have dedicated CSS rules for print. Using "screen" allows you to force the "standard" CSS rules.
pdfFormat string The PDF format of the resulting PDF
For instance:
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/url' \
--form 'url="https://my.url"' \
--form 'pdfFormat="PDF/A-1a"' \
-o my.pdf
After a Chromium conversion, the PDF engines will convert the resulting PDF to a specific format.
Check the available PDF formats:
URL
POST /forms/chromium/convert/url
This route accepts a form field url
with the URL of the page you want to convert to PDF.
For instance:
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/url' \
--form 'url="https://my.url"' \
--form 'marginTop="0"' \
--form 'marginBottom="0"' \
--form 'marginLeft="0"' \
--form 'marginRight="0"' \
-o my.pdf
When converting a website to PDF, you should remove all margins. If not, the resulting PDF might not contain all content from the page.
The form fields extraLinkTags
and extraScriptTags
(JSON format) allows you to add <link>
and <script>
HTML elements
with remote paths.
For instance:
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/url' \
--form 'url="https://my.url"' \
--form 'extraLinkTags="[{\"href\":\"https://my.cdn.css\"}]"' \
--form 'extraScriptTags="[{\"src\":\"https://my.cdn.js\"}]"' \
-o my.pdf
For <link>
HTML elements, only the href
attribute is available.
For <script>
HTML elements, only the src
attribute is available.
<script>
HTML elements are not added if JavaScript is disabled via --chromium-disable-javascript
.
HTML
POST /forms/chromium/convert/html
This route accepts a file named index.html
.
For instance:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My PDF</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/html' \
--form 'files=@"/path/to/index.html"' \
-o my.pdf
You may also send additional files, like images, fonts, stylesheets, and so on.
The only requirement is that their paths in the index.html
file are on the root level.
For instance, this will work:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My PDF</title>
</head>
<body>
<h1>Hello world!</h1>
<img src="img.png" />
</body>
</html>
But this won't:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My PDF</title>
</head>
<body>
<h1>Hello world!</h1>
<img src="/foo/img.png" />
</body>
</html>
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/html' \
--form 'files=@"/path/to/index.html"' \
--form 'files=@"/path/to/style.css"' \
--form 'files=@"/path/to/img.png"' \
--form 'files=@"/path/to/font.woff"' \
-o my.pdf
Remote paths for images, fonts (e.g., Google Fonts), etc., works too.
See the Fonts chapter if you want to install fonts in the Docker image.
Markdown
POST /forms/chromium/convert/markdown
This route accepts a file named index.html
plus markdown files.
It works like the HTML route but with access to a Go template function toHTML
. It converts a
markdown file's content to HTML.
For instance:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My PDF</title>
</head>
<body>
{{ toHTML "file.md" }}
</body>
</html>
curl \
--request POST 'http://localhost:3000/forms/chromium/convert/markdown' \
--form 'files=@"/path/to/index.html"' \
--form 'files=@"/path/to/file.md"' \
-o my.pdf
Health Check
If --chromium-failed-starts-threshold=0
, there is no health check.
The health route returns a 503 Service Unvailable
response if the failed starts
threshold has been reached.
Metric
The Chromium module exposes the following metric:
{namespace}_chromium_active_instances_count
- current number of active Chromium instances.{namespace}_chromium_failed_starts_count
- current number of Chromium consecutive starting failures.
See Prometheus for more details.
Troubleshooting
PDF files size too large
Using webfonts significantly increases the PDF file size. When using a custom font, see the Fonts chapter on how to install them in the Docker container instead. See issue #521 for more details.