question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to fix content cut off issues in jspdf ?

See original GitHub issue

I am using jspsf and html2canvas to convert the components into a PDF.

Below is the code for that,

  function generatePDF() {
    const input = document.getElementById("pdf");
    html2canvas(input, {
      logging: true,
      letterRendering: 1,
      scale: 2,
      windowWidth: 1440,
      useCORS: true
    }).then((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 = 0;
      doc.addImage(imgData, "jpeg", 0, position, imgWidth, imgHeight);
      heightLeft -= pageHeight;
      while (heightLeft >= 0) {
        position = heightLeft - imgHeight;
        doc.addPage();
        doc.addImage(imgData, "jpeg", 0, position, imgWidth, imgHeight);
        heightLeft -= pageHeight;
      }

      const pages = doc.internal.getNumberOfPages();

      for (let j = 1; j < pages + 1; j++) {
        let horizontalPos = imgWidth / 2; //Can be fixed number
        let verticalPos = pageHeight - 10; //Can be fixed number
        doc.setPage(j);
        doc.setFontSize(10);
        doc.text(`${j} of ${pages}`, horizontalPos, verticalPos, {
          align: "center" //Optional text styling});
        });
      }

      doc.save("output.pdf");
    });
  }

is there any way to fix this issue? Please help me to solve this one.

Here you can get the working demo link - https://codesandbox.io/s/react-component-to-pdf-goe-page-cutting-3rnl29?file=/src/App.js:412-1697

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
guio12commented, Oct 25, 2022

Did anyone find a solution ?

1reaction
cljaraycommented, Sep 21, 2022

Not at the moment, because I’m still working on it. But you could try to wrap the elements you want on the next page in a div with the following class

CSS @media print { .documentToPdf { break-before: always; } }

You could also wrap the elements before the one you want on the next page with the following class

CSS @media print { .documentToPdf { break-after: always; } }

I’m sorry i don’t have the working example, but as I said, I’m working on it too.

Please note that

Read more comments on GitHub >

github_iconTop Results From Across the Web

jspdf too long texts in the table are cut off · Issue #3052 - GitHub
I'm no React expert but I would say you need to use setState to set the src variable. Otherwise React won't update the...
Read more >
jspdf - last paragraph line is being cut - Stack Overflow
Try this: Open your jspdf.js or jspdf.debug.js local file and modify this: this.pdf.internal.getVerticalCoordinateString(this.y) ...
Read more >
anyone experience with jsPDF? My image is cut off - Reddit
This turned out to be my problem, moving the element to the top of my html ... the pdf is successfully generated but...
Read more >
html2pdf.js | Client-side HTML-to-PDF rendering using pure JS.
Rendering: The rendering engine html2canvas isn't perfect (though it's pretty good!). If html2canvas isn't rendering your content correctly, I can't fix it.
Read more >
jspdf get page count: Image in PDF cut off - RasterEdge.com
Hi. For solve this problem, I suggestion that you using the function "fromHTML". Below there are a code in javascript for print html...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found