How apply margin when identify page number using HTML on jsPDF

153 Views Asked by At

I'm exporting an HTML to PDF and I noticed that when it has more than one page, the content is cut if I don't use the margins.

I would like to:

  • Subscribing to jsPDF's "addPage" event by adding a callback function
  • When a page is created (after the first one, that is, > 1), then I would add a "margin-top" so that the content is not cut off.

Library reference: https://github.com/parallax/jsPDF/blob/master/dist/jspdf.es.js#L3562

Example of how my code is using the lib:

    const doc = new jsPDF('p', 'mm', 'a4');
    const pdfElement = state.exportContent.get(0);

    doc.html(pdfElement, {
        autoPaging: 'text',
        callback: function (pdf) {
            window.open(pdf.output('bloburl'));
        },
        x: 5,
        y: 5,
        width: 200, //target width in the PDF document
        windowWidth: 900, //window width in CSS pixels
        margin: [10, 0, 10, 0], //I don't want this forever and on every page!!!
    })

I then tried to directly add the event, but it didn't work:

     jsPDF.API.events.push(["addPage", function (addPageData) {
        //make something if the page is bigger than one
     }]

I tried using the file creation callback function (before window.open), with the following code:

     var pageCount = doc.internal.getNumberOfPages();
     for(i = 1; i < pageCount; i++) {
        doc.setPage(i);
        doc.setMargin([0, 0, 10, 0]); //problem
     }

But, I had a problem, because the "setMargin" method does not exist. https://github.com/parallax/jsPDF/blob/master/dist/jspdf.es.js#L15346

How can I add "margin-top" only after the first page? (using HTML)

I'm using the following versions within Laravel:

  • "html2canvas": "^1.0.0-rc.1",
  • "jspdf": "^2.5.1",
  • "laravel/framework": "^9.19",
0

There are 0 best solutions below