Skip to content

Instantly share code, notes, and snippets.

@Ex-Communicad0
Forked from cirippo/ReadMe.md
Created January 7, 2024 18:24
Show Gist options
  • Select an option

  • Save Ex-Communicad0/9be7d2dd581c3217016cb81bde9c79a0 to your computer and use it in GitHub Desktop.

Select an option

Save Ex-Communicad0/9be7d2dd581c3217016cb81bde9c79a0 to your computer and use it in GitHub Desktop.
Download Google Drive protected PDF without TrustedScriptURL error at higher resolution

Easy step by step guide to download view only PDF from Google Drive - no TrustedScriptURL error and better quality


  1. Open the document in Google Docs
  2. Zoom in 2 times using Ctrl and + (VERY IMPORTANT!)
  3. Open Developer Tools
  4. Hit Ctrl + R to reload the document.
  5. Scroll to the bottom of the document to load all pages.
  6. To check if all pages are loaded, go to "Network" tab, type "img" in search bar. At the bottom bar you see "xx/yyy requests", "xx" must be equal to document's pages; if not scroll up to load missing pages
  7. Go to "Console" tab
  8. Paste the updated and improved script (download_pdf.js) that avoids TrustedScriptURL error and allows better file's quality

If you want to download the file at lower resolution or you have issues with zoom method, use lowres_download.js script.

// TrustedURL starting
let trustedURL;
if (window.trustedTypes && trustedTypes.createPolicy) {
const policy = trustedTypes.createPolicy('myPolicy', {
createScriptURL: (input) => {
return input;
}
});
trustedURL = policy.createScriptURL('https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js');
} else {
trustedURL = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
}
// Download script starting - EDITED AND IMPROVED BY cirippo
let jspdf = document.createElement("script");
jspdf.onload = function () {
let pdf = new jsPDF('p', 'mm', [1200, 1200*1.414]);
let elements = document.getElementsByTagName("img");
for (let i in elements) {
let img = elements[i];
console.log("add img ", img);
if (!/^blob:/.test(img.src)) {
console.log("invalid src");
continue;
}
let can = document.createElement('canvas');
let con = can.getContext("2d");
can.width = 1600;
can.height = 2263;
con.drawImage(img, 0, 0);
let imgData = can.toDataURL("image/jpeg", 1.0);
pdf.addImage(imgData, 'JPEG', 0, 0);
pdf.addPage();
}
pdf.save("download.pdf");
};
jspdf.src = trustedURL;
document.body.appendChild(jspdf);
// Created by 5nax, library updated by cirippo
let trustedURL;
if (window.trustedTypes && trustedTypes.createPolicy) {
const policy = trustedTypes.createPolicy('myPolicy', {
createScriptURL: (input) => {
return input;
}
});
trustedURL = policy.createScriptURL('https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js');
} else {
trustedURL = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
}
let jspdf = document.createElement("script");
jspdf.onload = function () {
let pdf = new jsPDF();
let elements = document.getElementsByTagName("img");
for (let i = 0; i < elements.length; i++) {
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);
if (i !== elements.length - 1) {
pdf.addPage();
}
}
pdf.save("download.pdf");
};
jspdf.src = trustedURL;
document.body.appendChild(jspdf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment