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

Java Script

The document contains JavaScript code that utilizes Trusted Types to create a secure URL for loading the jsPDF library. Once loaded, it generates a PDF from images on the page by converting them to JPEG format and adding them to the PDF document. Finally, it triggers a download of the generated PDF file named 'download.pdf'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views1 page

Java Script

The document contains JavaScript code that utilizes Trusted Types to create a secure URL for loading the jsPDF library. Once loaded, it generates a PDF from images on the page by converting them to JPEG format and adding them to the PDF document. Finally, it triggers a download of the generated PDF file named 'download.pdf'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 1

let trustedURL;

if (window.trustedTypes && trustedTypes.createPolicy) {


// Create a trusted policy for the script URL
const policy = trustedTypes.createPolicy('myPolicy', {
createScriptURL: (input) => input
});
trustedURL = policy.createScriptURL('https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/
jspdf.min.js');
} else {
console.warn("Trusted Types are not supported in this browser. Falling back to using a
plain URL.");
trustedURL = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js';
}

let jspdf = document.createElement("script");


jspdf.onload = function () {
// Create the PDF document once jsPDF is loaded
let pdf = new jsPDF();
let elements = document.getElementsByTagName("img");
for (let i in elements) {
let img = elements[i];
if (!/^blob:/.test(img.src)) {
continue;
}
let canvasElement = document.createElement('canvas');
let con = canvasElement.getContext("2d");
canvasElement.width = img.width;
canvasElement.height = img.height;
con.drawImage(img, 0, 0, img.width, img.height);
let imgData = canvasElement.toDataURL("image/jpeg", 1.0);
pdf.addImage(imgData, 'JPEG', 0, 0);
pdf.addPage();
}
pdf.save("download.pdf");
};

// Use the trusted URL as the script source


jspdf.src = trustedURL;
document.body.appendChild(jspdf);

You might also like