Skip to content

Instantly share code, notes, and snippets.

@Hans5958
Forked from dpaluy/README.md
Last active April 26, 2024 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hans5958/a5ddb984d7c0dd1ec82c361318b01799 to your computer and use it in GitHub Desktop.
Save Hans5958/a5ddb984d7c0dd1ec82c361318b01799 to your computer and use it in GitHub Desktop.
Google Drive PDF Ripper: Rips (protected/view-only) PDFs from Google Drive

Google Drive PDF Ripper

This script rips (protected/view-only) PDFs from Google Drive by copying all the page images provided by Google Drive, and then put it on a PDF. It is a ripper (not a downloader) because it does not download the PDF file directly, and it involves some sort of conversion (PDF > images > PDF).

This script is based on https://gist.github.com/dpaluy/74258794f7930401cc27262e0ea794dd and https://gist.github.com/cirippo/86edfbddc3125eb64ee4b2d8faa52caa, with updates made since I was quite bored, and didn't want to adjust parts of the code to my own when I'm using it.

Features

  • Uses latest jsPDF version and modern JavaScript techniques
  • Supports Trusted Types API (for Google Chrome/Chromium users)
  • Supports various pages sizes with no additional configuration
  • Uses original file name when downloading the finished document
  • Designed to be ran multiple times

How to

  1. Open the document in Google Docs.
  2. Zoom in by using the bottom toolbar so the images will be at the largest size possible
  3. Load all the images on the document by scrolling to the very bottom.
  4. Open the Console tab on Developer Tools.
  5. Execute the JavaScript by pasting the code and send it by pressing ENTER.
  6. Wait until the PDF is downloaded.
window.jspdfEl = document.createElement("script")
window.jspdfSrc = 'https://cdn.jsdelivr.net/npm/jspdf@2.5.1/dist/jspdf.umd.min.js'
if (window.trustedTypes && trustedTypes.createPolicy) {
const policy = trustedTypes.createPolicy('jspdf', {
createScriptURL: (input) => input
})
window.jspdfEl.src = policy.createScriptURL(window.jspdfSrc)
} else {
window.jspdfEl.src = window.jspdfSrc
}
window.jspdfEl.addEventListener('load', async () => {
console.log("!")
let pdf
const allImages = document.querySelectorAll("img[src*='blob:']")
console.log(`Found ${allImages.length} images.`)
const addPage = (canvas) => {
if (!pdf) pdf = new jspdf.jsPDF({
orientation: canvas.height > canvas.width ? 'potrait' : 'landscape',
unit: 'px',
format: [canvas.height, canvas.width],
hotfixes: ['px_scaling']
}); else {
pdf.addPage([canvas.width, canvas.height], canvas.height > canvas.width ? 'potrait' : 'landscape')
}
pdf.addImage(canvas, 'JPG', 0, 0)
}
console.log(allImages)
for (let i = 0; i < allImages.length; i++) {
const img = allImages[i]
console.log("Adding page:", img.alt, img.naturalWidth, img.naturalHeight)
const canvas = document.createElement('canvas')
const context = canvas.getContext("2d")
canvas.width = img.naturalWidth
canvas.height = img.naturalHeight
context.drawImage(img, 0, 0)
addPage(canvas)
}
console.log("Saving...")
pdf.save(document.querySelector('meta[property="og:title"]')?.content ?? "download.pdf")
document.body.removeChild(window.jspdfEl)
delete window.jspdfEl
delete window.jspdfSrc
})
document.body.appendChild(window.jspdfEl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment