Skip to content

Conversation

@oskarleonard
Copy link

@oskarleonard oskarleonard commented May 18, 2020

#19 (comment)

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

@markuslarssonlvls
Copy link

Great 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants