Console PDF Download
Save G-Drive PDF
- A4 Size
- 2k Image Dimension
- Compression
- Auto Rotate
(function() {
let scriptUrl = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js';
if (window.trustedTypes && window.trustedTypes.createPolicy) {
try {
const policy = window.trustedTypes.createPolicy('jsPDF-loader', {
createScriptURL: (url) => {
if (url === scriptUrl) return url;
throw new Error('URL not allowed');
}
});
scriptUrl = policy.createScriptURL(scriptUrl);
} catch (e) {
console.warn("TrustedTypes policy creation blocked or already exists:", e);
}
}
let jspdf = document.createElement("script");
jspdf.src = scriptUrl;
jspdf.onerror = function() {
console.error("Failed to load jsPDF due to CSP.");
};
jspdf.onload = function () {
let pdf = null;
let elements = document.getElementsByTagName("img");
let validImages = [];
for (let i = 0; i < elements.length; i++) {
let img = elements[i];
if (/^blob:/.test(img.src)) {
validImages.push(img);
}
}
if (validImages.length === 0) {
console.log("No blob images found on this page.");
return;
}
console.log(`Found ${validImages.length} blob images. Generating compressed PDF...`);
try {
for (let i = 0; i < validImages.length; i++) {
let img = validImages[i];
let origW = img.naturalWidth || img.width;
let origH = img.naturalHeight || img.height;
let maxWidth = 2000;
let maxHeight = 2000;
let w = origW;
let h = origH;
if (origW > maxWidth || origH > maxHeight) {
let scale = Math.min(maxWidth / origW, maxHeight / origH);
w = Math.floor(origW * scale);
h = Math.floor(origH * scale);
}
let orientation = w > h ? 'l' : 'p';
let canvasElement = document.createElement('canvas');
let con = canvasElement.getContext("2d");
canvasElement.width = w;
canvasElement.height = h;
con.drawImage(img, 0, 0, w, h);
let imgData = canvasElement.toDataURL("image/jpeg", 0.65);
if (i === 0) {
pdf = new jsPDF(orientation, 'px', 'a4');
} else {
pdf.addPage('a4', orientation);
}
let pageWidth = pdf.internal.pageSize.width || pdf.internal.pageSize.getWidth();
let pageHeight = pdf.internal.pageSize.height || pdf.internal.pageSize.getHeight();
let ratio = Math.min(pageWidth / w, pageHeight / h);
let finalW = w * ratio;
let finalH = h * ratio;
let x = (pageWidth - finalW) / 2;
let y = (pageHeight - finalH) / 2;
pdf.addImage(imgData, 'JPEG', x, y, finalW, finalH, undefined, 'FAST');
}
if (pdf) {
let filename = "download.pdf"; // Fallback name
// NEW: Search the page text for a .pdf filename
// Looks for valid filename characters (letters, numbers, spaces, dashes, underscores) ending in .pdf
let textMatch = document.body.innerText.match(/([a-zA-Z0-9_\-\(\) ]{3,60}\.pdf)/i);
if (textMatch && textMatch[1]) {
// Clean up any weird leading spaces
filename = textMatch[1].trim();
console.log("Found dynamic filename on page:", filename);
} else {
console.log("No .pdf name found in page text. Defaulting to download.pdf.");
}
pdf.save(filename);
console.log(`Success! PDF downloaded as: ${filename}`);
}
} catch (error) {
console.error("An error occurred while generating the PDF:", error);
}
};
document.body.appendChild(jspdf);
})();