html2pdf.js icon indicating copy to clipboard operation
html2pdf.js copied to clipboard

To be able to use the many pages workaround we need to pass in pageNr…

Open oskarleonard opened this issue 5 years ago • 1 comments

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();
  };

oskarleonard avatar May 18 '20 11:05 oskarleonard

Great 👍

markuslarssonlvls avatar May 18 '20 12:05 markuslarssonlvls