Google Drive View-Only PDF Script Downloader

Blog Post · Google Drive PDF Downloader
🔧 Google Drive trick

Google Drive View-Only PDF Script Downloader

⚠️ Use only in desperate need — respect content creators’ rights. This script is meant for personal archiving when no other option exists.

Here you can use this script to download view‑only PDF files from Google Drive. This script works like a screenshot: it captures all PDF pages to a set of high‑resolution images and combines them into one PDF file.

📋 Instructions

  1. Open the PDF from Google Drive.
  2. Click Preview.
  3. On the top right page, click the three vertical dots menu → Open in new window.
  4. Scroll down the PDF to the very end and make sure all content is loaded.
  5. Inspect element (right‑click → Inspect) and go to the Console tab.
  6. If you’re using Chrome and cannot paste code directly, type allow pasting and press ENTER.
  7. Copy and paste the script below into the console, then press ENTER.
(function () {
  console.log("Loading script ...");

  let script = document.createElement("script");
  script.onload = function () {
    const { jsPDF } = window.jspdf;

    // Generate a PDF from images with "blob:" sources.
    let pdf = null;
    let imgElements = document.getElementsByTagName("img");
    let validImgs = [];

    console.log("Scanning content ...");
    for (let i = 0; i < imgElements.length; i++) {
      let img = imgElements[i];

      // specific check for Google Drive blob images
      let checkURLString = "blob:https://drive.google.com/";
      if (img.src.substring(0, checkURLString.length) !== checkURLString) {
        continue;
      }

      validImgs.push(img);
    }

    console.log(`${validImgs.length} content found!`);
    console.log("Generating PDF file ...");

    for (let i = 0; i < validImgs.length; i++) {
      let img = validImgs[i];
      
      // Convert image to DataURL via Canvas
      let canvasElement = document.createElement("canvas");
      let con = canvasElement.getContext("2d");
      canvasElement.width = img.naturalWidth;
      canvasElement.height = img.naturalHeight;
      con.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
      let imgData = canvasElement.toDataURL();

      // Determine orientation and dimensions for THIS specific image
      let orientation = img.naturalWidth > img.naturalHeight ? "l" : "p";
      let pageWidth = img.naturalWidth;
      let pageHeight = img.naturalHeight;

      if (i === 0) {
        // Initialize PDF with the dimensions of the FIRST image
        pdf = new jsPDF({
          orientation: orientation,
          unit: "px",
          format: [pageWidth, pageHeight],
        });
      } else {
        // For subsequent images, add a new page with THAT image's specific dimensions
        pdf.addPage([pageWidth, pageHeight], orientation);
      }

      // Add the image to the current page
      pdf.addImage(imgData, "PNG", 0, 0, pageWidth, pageHeight, "", "SLOW");

      const percentages = Math.floor(((i + 1) / validImgs.length) * 100);
      console.log(`Processing content ${percentages}%`);
    }

    // Check if title contains .pdf in end of the title
    let title = document.querySelector('meta[itemprop="name"]')?.content || document.title || 'download.pdf';
    if ((title.split(".").pop() || "").toLowerCase() !== "pdf") {
      title = title + ".pdf";
    }

    // Download the generated PDF.
    console.log("Downloading PDF file ...");
    pdf.save(title, { returnPromise: true }).then(() => {
      document.body.removeChild(script);
      console.log("PDF downloaded!");
    });
  };

  // Load the jsPDF library using the trusted URL.
  let scriptURL = "https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js";
  let trustedURL;
  if (window.trustedTypes && trustedTypes.createPolicy) {
    const policy = trustedTypes.createPolicy("myPolicy", {
      createScriptURL: (input) => {
        return input;
      },
    });
    trustedURL = policy.createScriptURL(scriptURL);
  } else {
    trustedURL = scriptURL;
  }

  script.src = trustedURL;
  document.body.appendChild(script);
})();
⏱️ Processing time depends on the PDF content (number of pages, resolution). Wait until the download prompt appears.

✨ Enjoy… but please, use it responsibly.

🔧 Astuce Google Drive

Téléchargeur de PDF en lecture seule depuis Google Drive

⚠️ À utiliser uniquement en cas de besoin désespéré — respectez les droits des créateurs. Ce script est conçu pour l’archivage personnel lorsqu’aucune autre option n’existe.

Utilisez ce script pour télécharger des fichiers PDF en affichage seul depuis Google Drive. Il fonctionne comme une capture d’écran : il capture chaque page du PDF en images de haute qualité et les assemble en un seul fichier PDF.

📋 Instructions

  1. Ouvrez le PDF depuis Google Drive.
  2. Cliquez sur Preview (Aperçu).
  3. En haut à droite, cliquez sur les trois points verticaux → Ouvrir dans une nouvelle fenêtre.
  4. Faites défiler le PDF jusqu’à la fin et assurez-vous que tout le contenu est chargé.
  5. Inspectez l’élément (clic droit → Inspecter) et allez dans l’onglet Console.
  6. Si vous utilisez Chrome et ne pouvez pas coller directement, tapez allow pasting et appuyez sur ENTRÉE.
  7. Copiez et collez le script ci‑dessous dans la console, puis appuyez sur ENTRÉE.
(function () {
  console.log("Loading script ...");

  let script = document.createElement("script");
  script.onload = function () {
    const { jsPDF } = window.jspdf;

    // Generate a PDF from images with "blob:" sources.
    let pdf = null;
    let imgElements = document.getElementsByTagName("img");
    let validImgs = [];

    console.log("Scanning content ...");
    for (let i = 0; i < imgElements.length; i++) {
      let img = imgElements[i];

      // specific check for Google Drive blob images
      let checkURLString = "blob:https://drive.google.com/";
      if (img.src.substring(0, checkURLString.length) !== checkURLString) {
        continue;
      }

      validImgs.push(img);
    }

    console.log(`${validImgs.length} content found!`);
    console.log("Generating PDF file ...");

    for (let i = 0; i < validImgs.length; i++) {
      let img = validImgs[i];
      
      // Convert image to DataURL via Canvas
      let canvasElement = document.createElement("canvas");
      let con = canvasElement.getContext("2d");
      canvasElement.width = img.naturalWidth;
      canvasElement.height = img.naturalHeight;
      con.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
      let imgData = canvasElement.toDataURL();

      // Determine orientation and dimensions for THIS specific image
      let orientation = img.naturalWidth > img.naturalHeight ? "l" : "p";
      let pageWidth = img.naturalWidth;
      let pageHeight = img.naturalHeight;

      if (i === 0) {
        // Initialize PDF with the dimensions of the FIRST image
        pdf = new jsPDF({
          orientation: orientation,
          unit: "px",
          format: [pageWidth, pageHeight],
        });
      } else {
        // For subsequent images, add a new page with THAT image's specific dimensions
        pdf.addPage([pageWidth, pageHeight], orientation);
      }

      // Add the image to the current page
      pdf.addImage(imgData, "PNG", 0, 0, pageWidth, pageHeight, "", "SLOW");

      const percentages = Math.floor(((i + 1) / validImgs.length) * 100);
      console.log(`Processing content ${percentages}%`);
    }

    // Check if title contains .pdf in end of the title
    let title = document.querySelector('meta[itemprop="name"]')?.content || document.title || 'download.pdf';
    if ((title.split(".").pop() || "").toLowerCase() !== "pdf") {
      title = title + ".pdf";
    }

    // Download the generated PDF.
    console.log("Downloading PDF file ...");
    pdf.save(title, { returnPromise: true }).then(() => {
      document.body.removeChild(script);
      console.log("PDF downloaded!");
    });
  };

  // Load the jsPDF library using the trusted URL.
  let scriptURL = "https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js";
  let trustedURL;
  if (window.trustedTypes && trustedTypes.createPolicy) {
    const policy = trustedTypes.createPolicy("myPolicy", {
      createScriptURL: (input) => {
        return input;
      },
    });
    trustedURL = policy.createScriptURL(scriptURL);
  } else {
    trustedURL = scriptURL;
  }

  script.src = trustedURL;
  document.body.appendChild(script);
})();
⏱️ La vitesse de traitement dépend du contenu du PDF (nombre de pages, résolution). Attendez l’invite de téléchargement.

✨ Profitez… mais utilisez‑le avec modération.

🔧 حيلة Google Drive

تحميل ملف PDF للعرض فقط من Google Drive

⚠️ استخدم هذا فقط في حالات الضرورة القصوى — احترم حقوق منشئي المحتوى. هذه الأداة مخصصة للأرشفة الشخصية عندما لا تتوفر أي وسيلة أخرى.

هنا يمكنك استخدام هذا السكربت لتحميل ملفات PDF المعروضة للعرض فقط من Google Drive. يعمل السكربت مثل لقطة الشاشة: يلتقط كل صفحات PDF كصور بجودة عالية ثم يدمجها في ملف PDF واحد.

📋 التعليمات

  1. افتح ملف PDF من Google Drive.
  2. انقر على معاينة (Preview).
  3. في أعلى الصفحة على اليمين، انقر على القائمة الثلاثية النقاط ← فتح في نافذة جديدة.
  4. مرر PDF للأسفل حتى النهاية وتأكد من تحميل كل المحتوى.
  5. افتح فحص العنصر (زر الفأرة الأيمن ← فحص Inspect) واذهب إلى علامة التبويب Console.
  6. إذا كنت تستخدم كروم ولا تستطيع لصق الكود مباشرة، اكتب allow pasting واضغط إنتر.
  7. انسخ والصق السكربت أدناه في الكونسول، ثم اضغط إنتر.
(function () {
  console.log("Loading script ...");

  let script = document.createElement("script");
  script.onload = function () {
    const { jsPDF } = window.jspdf;

    // Generate a PDF from images with "blob:" sources.
    let pdf = null;
    let imgElements = document.getElementsByTagName("img");
    let validImgs = [];

    console.log("Scanning content ...");
    for (let i = 0; i < imgElements.length; i++) {
      let img = imgElements[i];

      // specific check for Google Drive blob images
      let checkURLString = "blob:https://drive.google.com/";
      if (img.src.substring(0, checkURLString.length) !== checkURLString) {
        continue;
      }

      validImgs.push(img);
    }

    console.log(`${validImgs.length} content found!`);
    console.log("Generating PDF file ...");

    for (let i = 0; i < validImgs.length; i++) {
      let img = validImgs[i];
      
      // Convert image to DataURL via Canvas
      let canvasElement = document.createElement("canvas");
      let con = canvasElement.getContext("2d");
      canvasElement.width = img.naturalWidth;
      canvasElement.height = img.naturalHeight;
      con.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
      let imgData = canvasElement.toDataURL();

      // Determine orientation and dimensions for THIS specific image
      let orientation = img.naturalWidth > img.naturalHeight ? "l" : "p";
      let pageWidth = img.naturalWidth;
      let pageHeight = img.naturalHeight;

      if (i === 0) {
        // Initialize PDF with the dimensions of the FIRST image
        pdf = new jsPDF({
          orientation: orientation,
          unit: "px",
          format: [pageWidth, pageHeight],
        });
      } else {
        // For subsequent images, add a new page with THAT image's specific dimensions
        pdf.addPage([pageWidth, pageHeight], orientation);
      }

      // Add the image to the current page
      pdf.addImage(imgData, "PNG", 0, 0, pageWidth, pageHeight, "", "SLOW");

      const percentages = Math.floor(((i + 1) / validImgs.length) * 100);
      console.log(`Processing content ${percentages}%`);
    }

    // Check if title contains .pdf in end of the title
    let title = document.querySelector('meta[itemprop="name"]')?.content || document.title || 'download.pdf';
    if ((title.split(".").pop() || "").toLowerCase() !== "pdf") {
      title = title + ".pdf";
    }

    // Download the generated PDF.
    console.log("Downloading PDF file ...");
    pdf.save(title, { returnPromise: true }).then(() => {
      document.body.removeChild(script);
      console.log("PDF downloaded!");
    });
  };

  // Load the jsPDF library using the trusted URL.
  let scriptURL = "https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js";
  let trustedURL;
  if (window.trustedTypes && trustedTypes.createPolicy) {
    const policy = trustedTypes.createPolicy("myPolicy", {
      createScriptURL: (input) => {
        return input;
      },
    });
    trustedURL = policy.createScriptURL(scriptURL);
  } else {
    trustedURL = scriptURL;
  }

  script.src = trustedURL;
  document.body.appendChild(script);
})();
⏱️ سرعة المعالجة تعتمد على محتوى PDF (عدد الصفحات، الدقة). انتظر حتى يظهر طلب التحميل.

✨ استمتع… ولكن استخدمه بمسؤولية.


Comments

Popular posts from this blog

Quiz — Introduction à la biologie cellulaire

Région Marrakech-Safi