Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly set the page number when generate the pdf #300

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ismail9k
Copy link

@ismail9k ismail9k commented Mar 6, 2020

Motivation

When generating a pdf file with more than many multiple pages, it does not create successfully, due to canvas size limitations. We can generate each page individually then bind it to the pdf. The problem with this technique, that the clickable links will work only on the first page, because of the way the current page being calculated.

Solution

In my PR I introduce a way to force the current page which is currently being generating.
You can then use the code as follows:

const pages = Array.from(document.querySelectorAll('.page'));
const opt = {};

let worker = html2pdf()
  .from(pages[0])
  .set(opt)
  .toPdf();

pages.forEach(function(page, idx) {
  worker = worker
    .get('pdf')
    .then(pdf => {
      if (!idx) return;
      pdf.addPage();
    })
    .from(page)
    .toContainer()
    .toCanvas()
    .toPdf(idx + 1); // <== Explicitly set the page number
});
worker = worker.save();
``

@ismail9k ismail9k changed the title feat: ability to force add current page Explicitly set the page number when generate the pdf Mar 6, 2020
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.

1 participant