Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit an image in the center of a page using jsPDF?

Using html2canvas & jsPDF, I'm trying to print an entire DIV I have on my screen and I've gotten this far:

const printAsPdf = () => {
  html2canvas(pageElement.current, {
    useCORS: true,
    allowTaint: true,
    scrollY: -window.scrollY,
  }).then(canvas => {
    const image = canvas.toDataURL('image/jpeg', 1.0);
    const doc = new jsPDF('p', 'px', 'a4');
    const pageWidth = doc.internal.pageSize.getWidth();
    const pageHeight = doc.internal.pageSize.getHeight();

    const widthRatio = pageWidth / canvas.width;
    const heightRatio = pageHeight / canvas.height;
    const ratio = widthRatio > heightRatio ? heightRatio : widthRatio;

    const canvasWidth = canvas.width * ratio;
    const canvasHeight = canvas.height * ratio;

    doc.addImage(image, 'JPEG', 0, 0, canvasWidth, canvasHeight);
    doc.output('dataurlnewwindow');
  });
};

This fills up the page with the image/canvas of my choosing. But I am not able to align the image dead center on the page.

like image 789
Amruth Pillai Avatar asked Mar 31 '20 14:03

Amruth Pillai


People also ask

Does jspdf work with html2canvas?

I used jsPDF in conjuction with html2canvas and I calculated the ratio from my div 's width and height. I applied that same ratio to the pdf document and the page fit perfectly onto the page without any distortion.

Is there a way to add a page to a jspdf?

If this is a bit hard to follow, you can also use .addPage ( [imgWidth, imgHeight]), which is more straightforward. The downside of this method is that the first page is fixed by new jsPDF (). See this answer for details.

How do I Center an image in an HTML page?

To center an image, set left and right margin to auto and make it into a block element: Note that it cannot be centered if the width is set to 100% (full-width).

How to make an image fit the page size of PDF?

var doc = new jsPDF ("p", "mm", "a4"); var width = doc.internal.pageSize.getWidth (); var height = doc.internal.pageSize.getHeight (); Then you can use this width and height for your image to fit the entire PDF document.


Video Answer


1 Answers

This problem required some old-school CSS tricks. I recalled how we used to center images back in the day with position: absolute; where we would calculate the image width, and canvas width, negate it and divide it by half. Using the same technique here worked like a charm!

const marginX = (pageWidth - canvasWidth) / 2;
const marginY = (pageHeight - canvasHeight) / 2;

So, the complete function looks like this:

const printAsPdf = () => {
  html2canvas(pageElement.current, {
    useCORS: true,
    allowTaint: true,
    scrollY: -window.scrollY,
  }).then(canvas => {
    const image = canvas.toDataURL('image/jpeg', 1.0);
    const doc = new jsPDF('p', 'px', 'a4');
    const pageWidth = doc.internal.pageSize.getWidth();
    const pageHeight = doc.internal.pageSize.getHeight();

    const widthRatio = pageWidth / canvas.width;
    const heightRatio = pageHeight / canvas.height;
    const ratio = widthRatio > heightRatio ? heightRatio : widthRatio;

    const canvasWidth = canvas.width * ratio;
    const canvasHeight = canvas.height * ratio;

    const marginX = (pageWidth - canvasWidth) / 2;
    const marginY = (pageHeight - canvasHeight) / 2;

    doc.addImage(image, 'JPEG', marginX, marginY, canvasWidth, canvasHeight);
    doc.output('dataurlnewwindow');
  });
};
like image 180
Amruth Pillai Avatar answered Oct 22 '22 14:10

Amruth Pillai