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

generating a PDF with multiple HTML div elements on separate pages using JS? #3726

Open
rajashripise opened this issue Apr 25, 2024 · 3 comments

Comments

@rajashripise
Copy link

rajashripise commented Apr 25, 2024

I am facing the issue to generate the a pdf with multiple HTML div elements on separate pages
only text getting printed but not the content from second div getting blank
i want to allow the autopaging text for pages

const doc = new jsPDF('p', 'mm', 'a4');
   const element = document.getElementById('div1');
   const element2 = document.getElementById('div2');
doc.html(element, {
           callback: function (doc) {
            
             resolve();
           },
           margin: [8,8,8,8],
           html2canvas: {
             useCORS: true,
             allowTaint: true,
             letterRendering: true,
             logging: false,
             scale: 0.6,
           },
           x: 3,
           y: 3
         });
       });
doc.addpage();
doc.text("Description = " ,15,20);
doc.html(element2, {
           callback: function (doc) {
            
             resolve();
           },
           margin: [8,8,8,8],
           html2canvas: {
             useCORS: true,
             allowTaint: true,
             letterRendering: true,
             logging: false,
             scale: 0.6,
           },
           x: 3,
           y: 3
         });
       });  doc.output('dataurlnewwindow');   
@Amar-Gill
Copy link

In my experience, setting autoPaging: false inside HTMLOptions would allow you to use doc.addPage() and set the element content to the next page.

One caveat is that overflowing content on the page will just be cut off instead of starting a new page. That's what autoPaging: 'text' option is for.

@MedcaphRepo
Copy link

Same issue i am facing

@Amar-Gill
Copy link

Amar-Gill commented Apr 30, 2024

ok found a solution that works for me, and allows me to use autoPaging: text.

it involves setting the y position for each element that needs to be on a new page.

can determine the correct y value by using doc.internal.pageSize.height in your loop.

  const doc = new jsPdf();

  for (let i = 0; i < numPages; i++) {
    const y = (doc.getNumberOfPages() - 1) * doc.internal.pageSize.height;

    const page = document.createElement('div') // append stuff into the div

    await doc.html(page, { ...htmlOptions, y });
    doc.addPage();
  }

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

No branches or pull requests

3 participants