Search code examples
javahtmlpdfboxopenhtmltopdf

Using Pure HTML, how do you trigger OpenHTMLtoPDF/PDFBox to generate bookmarks or table of contents in the navigation menu?


We're trying to add bookmark or table of contents metadata to a PDF that is generated from HTML. How do you signal PDFBox/OpenHTMLtoPDF to create the bookmarks/TOC?

<div class="bkmrk0">Header One</div>
<div class="bkmrk1">Header Two for List</div>
...

<div class="bkmrk2">Header Three Text</div>
...

Bookmark example on left, TOC on right.

PDF Bookmarks Example


Solution

  • To create Bookmarks:

    <bookmarks>
     <bookmark name="Font Support" href="#fonts-feature-group"/>
     <bookmark name="RTL &amp; BIDI Text Support" href="#rtl-feature-group"/>
     <bookmark name="Forms Support" href="#forms-feature-group"/>
     <bookmark name="List Support" href="#lists-feature-group"/>
     <bookmark name="Z-Index Support" href="#z-index-feature-group"/>
     <bookmark name="SVG Support (Experimental)" href="#svg-feature-group"/>
    </bookmarks>
    

    The href is the id of the element where the bookmark should link to.

    https://github.com/danfickle/openhtmltopdf/blob/14aef95364684fe3c7b7207bcb1246e5c3af0335/openhtmltopdf-examples/src/main/resources/visualtest/html/bookmark-head-nested.html