0% found this document useful (0 votes)
153 views1 page

Convert Images to PDF with jsPDF

This script converts all images on a webpage into a PDF document. It loops through each image element, draws it onto a canvas to get its natural size, adds the canvas image to the PDF, and adds a new page for each subsequent image. Once complete, it saves the generated PDF as "download.pdf".

Uploaded by

mh8640537
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views1 page

Convert Images to PDF with jsPDF

This script converts all images on a webpage into a PDF document. It loops through each image element, draws it onto a canvas to get its natural size, adds the canvas image to the PDF, and adds a new page for each subsequent image. Once complete, it saves the generated PDF as "download.pdf".

Uploaded by

mh8640537
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

let jspdf = document.

createElement("script");

[Link] = function () {
let pdf = new jsPDF();
let elements = [Link]("img");

for (let i = 0; i < [Link]; i++) {


let img = elements[i];
[Link]("add img ", img);
if (!(img instanceof HTMLImageElement) || !/^blob:/.test([Link])) {
[Link]("invalid src");
continue;
}

// Create a canvas with natural dimensions of the image


let can = [Link]('canvas');
[Link] = [Link];
[Link] = [Link];
let con = [Link]("2d");

// Draw the image on the canvas


[Link](img, 0, 0, [Link], [Link]);

// Add the image to the PDF with natural dimensions


[Link]([Link]("image/jpeg", 1.0), 'JPEG', 0, 0,
[Link], [Link]);

// Add a new page for the next image


if (i < [Link] - 1) {
[Link]();
}
}

[Link]("[Link]");
};

[Link] = '[Link]
[Link](jspdf);

You might also like