Headless Chrome PDF Bookmarks: Can It?
In this article
Yes, headless Chrome can generate PDFs with bookmarks, but not automatically. The built-in --print-to-pdf flag produces a PDF with no outline/bookmark tree; you must add bookmarks yourself using a separate tool or script after the PDF is created.
What “Bookmarks” Mean in a PDF
In PDF terminology, bookmarks are the clickable entries in the document outline (the sidebar you open with Ctrl+Shift+F in most readers). They are not the same as the bookmarks you save in a web browser. A PDF outline is a hierarchical list of destinations that jump to specific pages or positions. Without an outline, a long PDF is hard to navigate, especially if it’s a report, manual, or e-book.
How Headless Chrome Generates PDFs
Headless Chrome is a version of the Chrome browser that runs without a graphical interface. You can use it from the command line or via automation tools like Puppeteer (Node.js) or Selenium. The most common way to create a PDF is:
- Launch headless Chrome with
--headless. - Point it to a URL or local HTML file.
- Use the
--print-to-pdfflag to save the output.
That command produces a PDF that reflects the rendered page, including CSS styles, images, and layout. However, it does not scan the HTML for headings and convert them into a PDF outline. The resulting PDF has no bookmarks.
Why Headless Chrome Doesn’t Add Bookmarks Automatically
Chrome’s print-to-PDF feature is designed to mimic what you see when you print a web page. It focuses on visual fidelity, not document structure. The Chromium team has not implemented an option to generate a PDF outline from HTML headings. There is a long-standing feature request in the Chromium bug tracker for this, but as of now it remains unimplemented. So, if you need bookmarks, you must handle them after the PDF is generated.
How to Add Bookmarks to Headless Chrome PDFs
You have several options, depending on your technical comfort and the tools you already use.
Option 1: Use a PDF Library in Your Script
If you’re already using Puppeteer to generate the PDF, you can pipe the output to a PDF library that supports outlines. For example:
- pdf-lib (JavaScript) – lets you add outlines programmatically.
- PyPDF2 or reportlab (Python) – can merge or add outlines to existing PDFs.
- Ghostscript – can post-process PDFs, though it’s more complex.
You would need to extract the heading positions from your HTML (e.g., by adding named anchors or using a library like Puppeteer to get element positions) and then create outline entries that point to those coordinates.
Option 2: Use a Dedicated PDF Bookmark Tool
There are standalone utilities that can add bookmarks to an existing PDF. Some are command-line tools, others have a GUI. Examples include:
- jPDF Tweak (Java-based, cross-platform)
- Adobe Acrobat (proprietary, manual or via JavaScript)
- PDFtk (open-source, but bookmark support is limited)
These tools often let you import a text file that defines the bookmark hierarchy, then apply it to the PDF.
Option 3: Use a Headless Browser That Supports Outlines
Some alternatives to Chrome, like WeasyPrint (Python) or Prince (commercial), can generate PDFs with bookmarks directly from HTML and CSS. WeasyPrint, for instance, automatically creates bookmarks from <h1>–<h6> elements by default. If you’re not tied to Chrome, switching to one of these might save you the post-processing step.
How to Choose the Right Approach
Consider these factors when deciding:
- Volume: If you only need bookmarks occasionally, a manual tool like Acrobat might be fine. For batch processing, a scripted solution is better.
- Complexity of the outline: If you need a multi-level hierarchy (chapters and sub-sections), you’ll want a tool that supports nested entries.
- Programming language: Stick with what you know. If you’re already in Node.js, use pdf-lib; if Python, use PyPDF2.
- Cost: Some tools are free, some are paid. WeasyPrint is free and open-source; Prince is commercial.
Common Problems and Misconceptions
One common misconception is that adding <bookmark> tags to HTML will work. They don’t; Chrome ignores them. Another is that the PDF outline is the same as the table of contents. They are related but not identical: a table of contents is visible content in the document, while the outline is a separate metadata structure.
Another issue: if you use Puppeteer’s page.pdf() method, you might see a displayHeaderFooter option, but that only affects headers and footers, not bookmarks. Also, some users try to use --print-to-pdf with the --no-pdf-header-footer flag, but that doesn’t affect outlines either.
Finally, be aware that the PDF outline is stored as metadata. If you later edit the PDF with a tool that strips metadata, your bookmarks may be lost. Always keep the original HTML and the script that generates the PDF so you can regenerate with bookmarks if needed.
Frequently Asked Questions
Can I get bookmarks without writing code?
Yes. You can generate the PDF with headless Chrome, then open it in Adobe Acrobat (or a free alternative like PDF-XChange Editor) and manually add bookmarks. This is fine for one-off documents, but not practical for many files.
Does Puppeteer have a built-in bookmark option?
No. Puppeteer’s page.pdf() method does not expose any option to create a PDF outline. You have to combine it with a PDF library or a separate tool.
Are there any headless browsers that do add bookmarks automatically?
Yes, WeasyPrint and Prince do. They are not full browsers but HTML-to-PDF renderers. They support CSS Paged Media, which includes bookmark generation.
Will Chrome ever add this feature?
It’s possible, but there is no public timeline. The Chromium project has many feature requests, and this one has been open for years. You can track it on the Chromium bug tracker, but don’t hold your breath.
Can I add bookmarks to a PDF using JavaScript in the browser?
No. JavaScript running in a web page cannot modify the PDF file that the browser generates. The PDF is created after the page is rendered, and the browser does not expose an API for editing it.
What’s the easiest way to add bookmarks to a PDF generated by headless Chrome?
If you’re comfortable with a little scripting, use Puppeteer to generate the PDF, then use pdf-lib to add the outline. You’ll need to get the Y positions of your headings from Puppeteer and map them to PDF coordinates. There are tutorials online that walk through this exact process.