Docs Packages
@procedures/pdf
npm install @procedures/pdf @procedures/client
HTML to PDF with a headless Chromium browser that is already on the host: Microsoft Edge, Google Chrome or Chromium. The package does not download a browser.
A host registers the procedure with createRegistry(pdfModule), or with import "@procedures/pdf/register" for the global registry. The package root and ./module register nothing.
The procedure#
| Procedure | What it does | Effects |
|---|---|---|
pdf.render | It prints an HTML text or an HTML file to a PDF file, and gives the path and the size of the file. | reads fs, network and process, writes fs and process, destructive |
- The input has
htmlorfile, not the two. It also hasoutput,executable,argsandtimeout. - Without
output, the PDF file goes into a new temporary folder. The caller moves or deletes it. - An existing output file is replaced. So the procedure is destructive.
- The page can load images, fonts and scripts from the network. So the procedure reads
network. - The caller can choose the browser program. So the procedure writes
process.
import "@procedures/pdf/register";
const { path, bytes } = await client.call(["pdf", "render"], {
html: "<h1>Menu</h1><p>Soup of the day</p>",
output: "menu.pdf",
});
The functions#
The package root also gives the functions without a registry. Their home is @procedures/browser, and this package gives them again, so no caller breaks. New code imports them from @procedures/browser, which also takes screenshots.
renderPdf(options)prints the page and gives{ path, bytes, browser }. The options are the input ofpdf.render, andsignal,env,platformandrunner.findBrowser(options)gives the path of the browser program, orundefined.browserCandidates(platform, env)gives the paths that the search examines.pdfArgs(page, output, profile, extra)gives the argv of the browser.isPdfFile(path)tells if a file starts with%PDF.
The browser#
The search uses the first of these:
- The input
executable. - The environment variable
PDF_BROWSER, thenCHROME_PATH. - On Windows: Edge, then Chrome, in
Program Files,Program Files (x86)andLOCALAPPDATA. - On macOS: the app bundles of Chrome, Edge and Chromium in
/Applicationsand~/Applications. - The
PATH:google-chrome,google-chrome-stable,chromium,chromium-browser,microsoft-edgeandmicrosoft-edge-stable. On Windows,msedge.exeandchrome.exe.
When the search finds no browser, the procedure gives an error that names PDF_BROWSER.
The print#
The browser starts with argv through runCommand of @procedures/shell, not through a shell:
<browser> --headless --disable-gpu --no-pdf-header-footer --user-data-dir=<temporary folder> --print-to-pdf=<output> [args] <file URL of the page>
- An HTML text goes into a temporary file first.
- The temporary profile keeps the print apart from a browser that the user has open.
- The default time limit is 60 seconds. At the limit, or when the signal of the call aborts,
runCommandstops the process tree of the browser. - After the print, the procedure examines the file. A missing file, an empty file or a file that does not start with
%PDFgives an error. - A browser that runs as root, for example in a container, needs
args: ["--no-sandbox"].