html2pdf.js
html2pdf.js copied to clipboard
To be able to use the many pages workaround we need to pass in pageNr…
https://github.com/eKoopmans/html2pdf.js/issues/19#issuecomment-484240946
Then you can use the code snippet below to make links work in multipages pdf
const saveMultiPagePdf = (opt) => {
// To avoid blank pages in the pdf we need to manually add the divs
// See issue https://github.com/eKoopmans/html2pdf.js/issues/19
const html2pdf = require('html2pdf.js');
const domElementCollection = pageClassNameId
? document.getElementsByClassName(pageClassNameId)
: pdfContent.current.children;
const domPages = [...domElementCollection].map((htmlElement) => {
return htmlElement.outerHTML;
});
let worker = html2pdf().set(opt);
domPages.forEach((page, index) => {
worker = worker
.from(page)
.toContainer(index + 1)
.toCanvas()
.toPdf()
.get('pdf')
.then((pdf) => {
if (index < domPages.length - 1) {
// dont add last blank page
pdf.addPage();
}
});
});
return worker.save();
};
Great 👍