Skip to content

Instantly share code, notes, and snippets.

@cirippo
Last active April 14, 2024 15:47
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cirippo/86edfbddc3125eb64ee4b2d8faa52caa to your computer and use it in GitHub Desktop.
Save cirippo/86edfbddc3125eb64ee4b2d8faa52caa 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);
@Aditya22453D
Copy link

Sir plz can make the black color of the background white?

@trnkobayashi
Copy link

trnkobayashi commented Jan 31, 2024

I used the highres method and met this error

jspdf.debug.js:1922 Uncaught RangeError: Invalid string length
at Array.join ()
at API.private.buildDocument (jspdf.debug.js:1922:24)
at API.save (jspdf.debug.js:3648:26)
at jspdf.onload (:38:9)

https://imgur.com/a/7ChNHm3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment