Last active
January 15, 2026 05:50
-
-
Save BlazeIsClone/fdf19d67fd7b9de3c0d59ccb5e3fa0b2 to your computer and use it in GitHub Desktop.
Google View Only PDF Export
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| // 1. Create a Trusted Types policy if supported by the browser | |
| const policy = window.trustedTypes | |
| ? trustedTypes.createPolicy('jspdf-policy', { createScriptURL: url => url }) | |
| : null; | |
| const scriptUrl = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js'; | |
| let jspdfScript = document.createElement("script"); | |
| jspdfScript.onload = function () { | |
| // jsPDF 1.3.2 uses 'jsPDF' as the constructor | |
| let elements = document.getElementsByTagName("img"); | |
| let pdf = null; | |
| // Convert HTMLCollection to Array to avoid issues with dynamic DOM changes | |
| let images = Array.from(elements).filter(img => /^blob:/.test(img.src)); | |
| if (images.length === 0) { | |
| console.warn("No blob images found on this page."); | |
| return; | |
| } | |
| images.forEach((img, index) => { | |
| // Get actual pixel dimensions | |
| const w = img.naturalWidth; | |
| const h = img.naturalHeight; | |
| // Initialize the PDF on the first valid image | |
| if (!pdf) { | |
| pdf = new jsPDF({ | |
| orientation: w > h ? 'l' : 'p', | |
| unit: 'px', | |
| format: [w, h] | |
| }); | |
| } else { | |
| // Add a new page matching the current image's dimensions | |
| pdf.addPage([w, h], w > h ? 'l' : 'p'); | |
| } | |
| // Create a canvas to extract image data | |
| let canvas = document.createElement('canvas'); | |
| canvas.width = w; | |
| canvas.height = h; | |
| let ctx = canvas.getContext("2d"); | |
| ctx.drawImage(img, 0, 0, w, h); | |
| // Convert to JPEG | |
| let imgData = canvas.toDataURL("image/jpeg", 1.0); | |
| // Add image at 0,0 with its actual width and height | |
| pdf.addImage(imgData, 'JPEG', 0, 0, w, h); | |
| }); | |
| if (pdf) { | |
| pdf.save("actual_size_images.pdf"); | |
| } | |
| }; | |
| // 2. Set the script source using the Trusted Types policy if available | |
| jspdfScript.src = policy ? policy.createScriptURL(scriptUrl) : scriptUrl; | |
| document.head.appendChild(jspdfScript); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment