The Pdfcrowd JavaScript library provides useful helper functions that enable to dynamically modify the converted document. You can insert a table of contents, disable hyperlinks, remove elements and many others.
The library is automatically injected into the converted HTML document. There are two ways to call the libPdfcrowd functions:
client.setCustomJavascript("libPdfcrowd.insertTableOfContents()");
onLoadPdfcrowd
callback:
<script> window.onLoadPdfcrowd = function() { libPdfcrowd.insertTableOfContents(); } </script>
function libPdfcrowd.insertStyle(PlainObject options)
first
false
libPdfcrowd.insertStyle({style: 'body { color: red; }'});
function libPdfcrowd.disableHyperlinks(PlainObject options)
document
false
libPdfcrowd.disableHyperlinks({element: document.body});
function libPdfcrowd.removeBySelector(PlainObject options)
document
false
libPdfcrowd.removeBySelector({selector: 'img', recursive: true});
function libPdfcrowd.removeZIndexHigherThan(PlainObject options)
document
false
libPdfcrowd.removeZIndexHigherThan({zlimit: 1000});
function libPdfcrowd.highlightHtmlElements(PlainObject options)
'rgba(255, 191, 0, 0.1)'
'orange'
'red'
false
null
null
libPdfcrowd.highlightHtmlElements({backgroundColor: null, padding: '4px', margin: '4px'});
function libPdfcrowd.insertTableOfContents(PlainObject options)
document.body
'first'
'last'
'single'
'replace'
'first'
['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
'h2'
['h1', 'h2']
['.toc1', '.toc2']
'a'
'h1:not(:first-of-type)'
document.body
'#content'
'Table of Contents'
'<span style='color: aqua;'>My Book TOC</span>'
'toc-and-headings'
'toc'
'headings'
'toc-and-headings'
'arabic'
'roman'
'roman-lowercase'
'alphabet'
'alphabet-lowercase'
'arabic'
'alphabet'
['my <strong>$i-th</strong>', 'my sublevel <span class="my-class">$A</span>']
'all'
[1, 2, 2]
'10pt'
'1cm'
['10pt', '2in']
'. '
'…'
['+', '-']
'\u25AB '
['<img src="level1.png"/>', '<b>+++</b>']
true
'arabic'
'roman'
'roman-lowercase'
'arabic'
'roman'
[null, 'arabic']
'before'
'after'
'before-and-after'
null
null
"background: #f4f4f4 none repeat scroll 0 0;
border: 1px solid #aaa;
padding: 1em;
box-sizing: border-box;"
null
function(element) { return element.textContent; }
function(element, level, numbering, entry) { return entry; }
libPdfcrowd.insertTableOfContents();
libPdfcrowd.insertTableOfContents({selectors: 'h3'});
libPdfcrowd.insertTableOfContents({ placement: 'last', selectors: 'img', title: 'Image List', onGetEntryText: function(element) { return element.getAttribute('alt'); } });
libPdfcrowd.insertStyle({ style: '.pdfcrowd-toc-page-nr { color: red; }' }); libPdfcrowd.insertTableOfContents();
var toc = libPdfcrowd.insertTableOfContents({target: null}); document.getElementById('#toc').appendChild(toc);
html2pdfClient.setOnLoadJavascript(" libPdfcrowd.insertTableOfContents({ source: '#content', target: '#toc', className: 'toc', placement: 'replace', selectors: ['h2 .mw-headline', 'h3 .mw-headline', 'h4 .mw-headline'], title: 'Contents <span style="font-size: 10px">powered by Pdfcrowd</span>' }); "); html2pdfClient.convertUrlToFile("https://en.wikipedia.org/wiki/PDF", "example.pdf");
libPdfcrowd.insertTableOfContents({tocId: 'my-toc', className: 'my-class'})
. It shows the TOC structure and the classes that you style in your CSS.
<div id="my-toc" class="my-class pdfcrowd-toc pdfcrowd-toc-style-1"> <div class="pdfcrowd-toc-title">Table of Contents </div> <table class="pdfcrowd-toc-row"> <tbody> <tr class="pdfcrowd-toc-level pdfcrowd-toc-level-1"> <td class="pdfcrowd-toc-caption pdfcrowd-toc-caption-level-1"> <a href="#my-toc-el-1"> <div class="pdfcrowd-toc-text pdfcrowd-toc-text-level-1"> <span class="pdfcrowd-toc-numbering pdfcrowd-toc-numbering-level-1"> 1. </span> <span class="pdfcrowd-toc-text-content pdfcrowd-toc-text-content-level-1"> Heading Level 1 </span> </div> </a> </td> <td class="pdfcrowd-toc-page-number pdfcrowd-toc-page-number-level-1"> <a data-pdfcrowd-link="my-toc-el-1" data-pdfcrowd-number-format="arabic" class="pdfcrowd-toc-page-nr" href="#my-toc-el-1"> <div>$N </div> </a> </td> </tr> </tbody> </table> <table class="pdfcrowd-toc-row"> <tbody> <tr class="pdfcrowd-toc-level pdfcrowd-toc-level-2"> <td class="pdfcrowd-toc-caption pdfcrowd-toc-caption-level-2"> <a href="#my-toc-el-2"> <div class="pdfcrowd-toc-text pdfcrowd-toc-text-level-2"> <span class="pdfcrowd-toc-numbering pdfcrowd-toc-numbering-level-2"> 1.1. </span> <span class="pdfcrowd-toc-text-content pdfcrowd-toc-text-content-level-2"> Heading Level 2 </span> </div> </a> </td> <td class="pdfcrowd-toc-page-number pdfcrowd-toc-page-number-level-2"> <a data-pdfcrowd-link="my-toc-el-2" data-pdfcrowd-number-format="arabic" class="pdfcrowd-toc-page-nr" href="#my-toc-el-2"> <div>$N </div> </a> </td> </tr> </tbody> </table> <table class="pdfcrowd-toc-row"> <tbody> <tr class="pdfcrowd-toc-level pdfcrowd-toc-level-3"> <td class="pdfcrowd-toc-caption pdfcrowd-toc-caption-level-3"> <a href="#my-toc-el-3"> <div class="pdfcrowd-toc-text pdfcrowd-toc-text-level-3"> <span class="pdfcrowd-toc-numbering pdfcrowd-toc-numbering-level-3"> 1.1.1. </span> <span class="pdfcrowd-toc-text-content pdfcrowd-toc-text-content-level-3"> Heading Level 3 </span> </div> </a> </td> <td class="pdfcrowd-toc-page-number pdfcrowd-toc-page-number-level-3"> <a data-pdfcrowd-link="my-toc-el-3" data-pdfcrowd-number-format="arabic" class="pdfcrowd-toc-page-nr" href="#my-toc-el-3"> <div>$N </div> </a> </td> </tr> </tbody> </table> </div>