Skip to content

Instantly share code, notes, and snippets.

@cirippo
Last active February 20, 2026 22:51
Show Gist options
  • Select an option

  • Save cirippo/86edfbddc3125eb64ee4b2d8faa52caa to your computer and use it in GitHub Desktop.

Select an option

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);
@parzibyte
Copy link

Working as today. Just adjust zoom to desired quality and make sure every page is loaded. Then open console, paste and hit Enter:

const jspdfModule = await import('https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js');

async function crearPDF(nombre) {
    const { jsPDF } = window.jspdf;
    /*Las imágenes del libro tienen el alt en "Página 1 de X" */
    const imagenes = document.querySelectorAll('img[alt^="Página"]');
    
    if (imagenes.length === 0) {
        console.error("No se encontraron imágenes.");
        return;
    }

    let doc;
    const calidadJpeg = 0.95; // Número entre 0 y 1. Entre más grande mejor calidad y más peso

    for (let i = 0; i < imagenes.length; i++) {
        console.log("Añadiendo página %d de %d", i, imagenes.length);
        const img = imagenes[i];
        
        const canvas = document.createElement("canvas");
        canvas.width = img.naturalWidth;
        canvas.height = img.naturalHeight;
        const ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0);
        
        const imgData = canvas.toDataURL("image/jpeg", calidadJpeg);
        
        const imgW = img.naturalWidth;
        const imgH = img.naturalHeight;
        
        const orientacion = imgW > imgH ? 'l' : 'p'; // 'l' landscape, 'p' portrait

        if (i === 0) {
            // Crear el documento con el tamaño de la primera imagen
            doc = new jsPDF({
                orientation: orientacion,
                unit: 'px',
                format: [imgW, imgH]
            });
        } else {
            // Añadir nueva página con el tamaño exacto de la imagen actual
            doc.addPage([imgW, imgH], orientacion);
        }

        doc.addImage(imgData, 'JPEG', 0, 0, imgW, imgH);
    }
    
    doc.save(nombre);
}

// Ejecutar
crearPDF("miLibro.pdf");

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