jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

addPage() in for loop

Open pr0t0 opened this issue 11 years ago • 17 comments

I'm not able to dynamically create and populate pages in a for loop. Is this possible? A simplified version to get four populated pages might look like:

var doc = new jsPDF('p', 'pt', 'letter'); var requiredPages = 4 for(var i = 0; i < requiredPages; i++){ doc.addPage(); //populate page }

pr0t0 avatar May 01 '15 15:05 pr0t0

Does anyone have a comment or info on this? Possible? Not? Trick to make it work?

I've got a web form that allows a user to make any number of entries. I'm using jsPDF to print those entries in a formatted output, but only 7 entries can fit on the page. The typical number of entries is probably around 40-60, but could be far less or a little more.

pr0t0 avatar May 27 '15 13:05 pr0t0

Hi, I tried and it works without any problems:

var doc = new jsPDF(); var requiredPages = 4 for(var i = 0; i < requiredPages; i++){ doc.addPage(); //doc.text(20, 100, 'Some Text.'); } doc.save('hello.pdf');

Ema7 avatar Jun 11 '15 10:06 Ema7

How it do if we don't know how many pages required.

Dinesh-Wasnik avatar Aug 24 '17 10:08 Dinesh-Wasnik

@Dinesh-Wasnik It already knows how many pages are required. For every loop it will create a page, in this case the loop of lenth 4 var requiredPages = 4 for(var i = 0; i < requiredPages; i++){ doc.addPage(); }

Delink-D avatar Aug 30 '17 16:08 Delink-D

Seems that it solved over the time.

Uzlopak avatar Feb 24 '18 14:02 Uzlopak

@Delink-D. If you have content created dynamically. (Some time less , sometime more) Then how you know , how many pages required if PDF on the fly.

Dinesh-Wasnik avatar Feb 24 '18 15:02 Dinesh-Wasnik

@Delink-D. If you have content created dynamically. (Some time less , sometime more) Then how you know , how many pages required if PDF on the fly.

You don't need that. Just let JSPDF do the work it will add pages if needed.

flccrakers avatar Oct 17 '18 08:10 flccrakers

@Delink-D. If you have content created dynamically. (Some time less , sometime more) Then how you know , how many pages required if PDF on the fly.

You don't need that. Just let JSPDF do the work it will add pages if needed.

I am trying to export table data into pdf but JSPDF not adding page dynamically when the data exceeding capacity of one page. here is the code

  var data = document.getElementById('tableId');
  html2canvas(data).then(canvas => {
    // Few necessary setting options  
    var imgWidth = 208;
    var pageHeight = 295;
    var imgHeight = canvas.height * imgWidth / canvas.width;
    var heightLeft = imgHeight;

    const contentDataURL = canvas.toDataURL('image/png')
    let pdf = new jspdf('p', 'mm', 'a4', true); // A4 size page of PDF  
    var position = 0;
    pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);
   // pdf.addPage()

    pdf.save('MYPdf.pdf'); // Generated PDF 

  });

bikas-kumar avatar Mar 07 '19 07:03 bikas-kumar

@bikas-kumar It's because you are trying to convert a table into a picture and insert that picture in your document. If the picture is too big it will not be able to split it.

For table, I recommend using jsPDF-AutoTable : https://github.com/simonbengtsson/jsPDF-AutoTable. Look at the exemples.js provided, you will find a lot of nice examples.

Sorry to only giving you link to an API, but as we don't know the structure of your table, we can not help you more, beside its very well explained in the GitHub page.

flccrakers avatar Mar 07 '19 08:03 flccrakers

@bikas-kumar It's because you are trying to convert a table into a picture and insert that picture in your document. If the picture is too big it will not be able to split it.

For table, I recommend using jsPDF-AutoTable : https://github.com/simonbengtsson/jsPDF-AutoTable. Look at the exemples.js provided, you will find a lot of nice examples.

Sorry to only giving you link to an API, but as we don't know the structure of your table, we can not help you more, beside its very well explained in the GitHub page.

Thank you so much ,now it is coming with all pages. the name of the header is coming in two line . Is there any way to fix that one. Example : header name is "Score" , "Scor" is coming in one line and "e" is coming in next line. Even though table is having only two column

bikas-kumar avatar Mar 07 '19 09:03 bikas-kumar

Thank you so much ,now it is coming with all pages. the name of the header is coming in two line . Is there any way to fix that one. Example : header name is "Score" , "Scor" is coming in one line and "e" is coming in next line. Even though table is having only two column

Nice to hear. As your question is about usage of the tool and not reporting issues, I suggest continuing this discussion in https://stackoverflow.com/. Remember to pasting your code to help us help you (use code snippet, it helps a lot).

flccrakers avatar Mar 07 '19 10:03 flccrakers

let div = document.getElementById('photo');

html2canvas(div).then(
    function (canvas) {
       
        var imgData = canvas.toDataURL('image/png');
        var imgWidth = 210;
        var pageHeight = 295;
        var imgHeight = canvas.height * imgWidth / canvas.width;
        var heightLeft = imgHeight;
        var doc = new jsPDF('p', 'mm');
        var position = 10; // give some top padding to first page

        doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
        heightLeft -= pageHeight;

        while (heightLeft >= 0) {
            position += heightLeft - imgHeight; // top padding for other pages
            doc.addPage();
            doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
            heightLeft -= pageHeight;
        }
        
        doc.save('doctor.pdf')


    })

//try this i used this for my pdf generation using html2canvas and jspdf

fadhilsaheer avatar Oct 01 '20 05:10 fadhilsaheer

I receive one base64 images array and need put one image per page. How can i do this? i need put one image per page.How can i do this?

HerickRaposo avatar May 12 '21 20:05 HerickRaposo

Found any Solutions guys? Having the same issue using .HTML() method

doc.html(document.getElementById("Page1"), {
   callback: function (pdf) {
     pdf.addPage("a4", "l");
     pdf.html(document.getElementById("Page2"), {
       callback: function (pdf2) {
         pdf2.addPage("a4", "l");
         pdf2.html(document.getElementById("Page3"), {
           callback: function (pdf3) {
             pdf3.addPage("a4", "l");
             pdf3.html(document.getElementById("Page4"), {
               callback: function (pdf4) {
                 pdf4.addPage("a4", "l");
                 pdf4.html(document.getElementById("Page5"), {
                   callback: function (pdf5) {
                     pdf5.addPage("a4", "l");
                     pdf5.html(document.getElementById("Page6"), {
                       callback: function (pdf6) {
                         pdf6.save("FinancialStatement.pdf");
                       },
                       x: 0,
                       y: 5 * pageHeight,
                     });
                   },
                   x: 0,
                   y: 4 * pageHeight,
                 });
               },
               x: 0,
               y: 3 * pageHeight,
             });
           },
           x: 0,
           y: 2 * pageHeight,
         });
       },

       x: 0,
       y: pageHeight,
     });
   },
   x: 0,
   y: 0,
 });

I've to make this code dynamic using For Loop, Any suggestion guyz?

prasheel888 avatar Nov 07 '22 12:11 prasheel888

@prasheel888 You solved my problem. Thank you so much!!

ajayraho4 avatar Mar 29 '23 10:03 ajayraho4

RENDER SEVERAL HTML IN ONE DOC

function generatePDF(elements) { const pdf = new jsPDF('l', 'pt', 'a4'); const pageHeight = pdf.internal.pageSize.getHeight(); const pageWidth = pdf.internal.pageSize.getWidth();

let currentIndex = 0;

function addElementToPDF(index) { if (index >= elements.length) { pdf.save('report.pdf'); return; }

const element = document.getElementById(elements[index]);

pdf.html(element, {
  callback: function (pdf) {
    pdf.addPage("a4", "l");
    addElementToPDF(index + 1);
  },
  x: 0,
  y: 0,
});

}

addElementToPDF(currentIndex); }

// Example usage: const elementIds = ["Page1", "Page2", "Page3", "Page4", "Page5", "Page6"]; generatePDF(elementIds);

hayyangovinda avatar Jun 04 '24 06:06 hayyangovinda

this is what i used in angular, replace tableArray with an array of your html elements..

Note: the number of page must be same as tableArray length

downloadPdfReport() { const pdf = new jsPDF("l", "pt", "a4"); const pageHeight = pdf.internal.pageSize.getHeight(); let currentPage = 0; const tableCopy = this.table.nativeElement.cloneNode(true); // const tableArray = [tableCopy, tableCopy, tableCopy]; // array of your html elements

function addTableCopyToPDF(pageIndex) {
  if (pageIndex >= 2) {
    pdf.save("FinancialStatement.pdf");
    return;
  }
  const table = tableArray[pageIndex];
  pdf.html(table, {
    callback: function (pdfInstance) {
      pdfInstance.addPage("a4", "l");
      addTableCopyToPDF(pageIndex + 1);
    },
    x: 0,
    y: pageIndex * pageHeight,
  });
}

addTableCopyToPDF(currentPage);

}

hayyangovinda avatar Jun 04 '24 06:06 hayyangovinda