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

PNG images are getting cropped #3745

Closed
tanishq-agarwal opened this issue Jul 3, 2024 · 6 comments
Closed

PNG images are getting cropped #3745

tanishq-agarwal opened this issue Jul 3, 2024 · 6 comments

Comments

@tanishq-agarwal
Copy link

When I click the download button to generate the PDF, everything is downloaded correctly except for PNG images, which are getting cropped. SVG images are downloaded as expected. I’m attaching a screenshot for reference.

Here is the code which I've used:

` const downloadPDF = () => {
const input = document.querySelector("#downloadPDF");

html2canvas(input, {
  scale: window.devicePixelRatio,
  useCORS: true, // Allows capturing images from other domains by enabling CORS
  allowTaint: true, // Allows capturing images with CORS security.
}).then((canvas) => {
  // Capture the content inside 'input' element into 'canvas'
  const imgData = canvas.toDataURL("image/png");
  const pdf = new jsPDF("p", "mm", "a4"); //portrait view of A4 size paper in mm unit
  const pdfWidth = pdf.internal.pageSize.getWidth();
  const pdfHeight = pdf.internal.pageSize.getHeight();

  // Calculate image dimensions based on aspect ratio
  const imgWidth = pdfWidth;
  const imgHeight = (canvas.height * pdfWidth) / canvas.width;

  let heightLeft = imgHeight;
  let position = 0;

  // Uses pdf.addImage to add the captured image (imgData) to the PDF at the specified coordinates (0, position) with dimensions (imgWidth, imgHeight)
  pdf.addImage(imgData, "PNG", 0, position, imgWidth, imgHeight);
  heightLeft -= pdfHeight;

  // Loops (while loop) to add additional pages if the content exceeds the initial page height (pdfHeight)
  while (heightLeft >= 0) {
    position = heightLeft - pdfHeight;
    pdf.addPage();
    pdf.addImage(imgData, "PNG", 0, position, imgWidth, imgHeight);
    heightLeft -= pdfHeight;
  }

  pdf.save("download.pdf");
});

};`

Screenshot 2024-07-03 at 12 48 39 PM
@tanishq-agarwal
Copy link
Author

@jamesbrobb can you help

@jamesbrobb
Copy link
Contributor

I'm afraid i can't be any help here, i last worked on this about 15 years ago

@wlsdn1101
Copy link

same issue here 🥲

@vigneshpamu
Copy link

Hey Hi,
@tanishq-agarwal I would really like to work on this issue, could u please assign this to me ?

@ArunKushhhh
Copy link

Hello @tanishq-agarwal
The issue you're encountering with PNG images getting cropped during PDF generation using the html2canvas library may be due to how the images are rendered within the canvas. I would like to work on this issue, so could you please assign it to me?

Copy link

github-actions bot commented Jan 7, 2025

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants