Skip to content

Instantly share code, notes, and snippets.

@cirippo
Last active March 18, 2025 19:50
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?

@gururajkchandan
Copy link

https://drive.google.com/file/d/1zS8NeVvX46ROE3VJ1Vpuyx0K2fuUcGYv/view?ts=63ec8ad3

Pleas download this file for me I tried above steps not working please check

@norberci
Copy link

All pfds I download with hires sript are indeed higher resolution, but the right part of the page (around 1/5) is cut

@mabequinho
Copy link

All pfds I download with hires sript are indeed higher resolution, but the right part of the page (around 1/5) is cut

I had to open developer tools on a separate window in order to avoid

@norberci
Copy link

norberci commented Jun 5, 2024

All pfds I download with hires sript are indeed higher resolution, but the right part of the page (around 1/5) is cut

I had to open developer tools on a separate window in order to avoid

I find it very strange, but that actually worked, thanks

@gururajkchandan
Copy link

https://drive.google.com/file/d/1zS8NeVvX46ROE3VJ1Vpuyx0K2fuUcGYv/view?ts=63ec8ad3

Pleas download this file for me I tried above steps not working please check

Please download this file and give me another link for downloading .

@mabequinho
Copy link

https://drive.google.com/file/d/1zS8NeVvX46ROE3VJ1Vpuyx0K2fuUcGYv/view?ts=63ec8ad3
Pleas download this file for me I tried above steps not working please check

Please download this file and give me another link for downloading .

this file does'not apply to the script user case.

@eli-143
Copy link

eli-143 commented Jun 29, 2024

Thank you so much for this! It saved my life! I just needed to tweak some numbers to have the file on the correct paper size.

@pereira162
Copy link

How do I make the PDF file have the text available for selection? As if it were an OCR transformation. But the original file on the drive was already normal.

@mabequinho
Copy link

How do I make the PDF file have the text available for selection? As if it were an OCR transformation. But the original file on the drive was already normal.

@pereira162 https://github.com/ocrmypdf/OCRmyPDF

@loctran016
Copy link

Sadly it's patched :((

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