Skip to content

Instantly share code, notes, and snippets.

@nguyenhiepvan
Last active August 23, 2022 10:26
Show Gist options
  • Save nguyenhiepvan/2a6974a2d35a512ccc8c5d43e717caec to your computer and use it in GitHub Desktop.
Save nguyenhiepvan/2a6974a2d35a512ccc8c5d43e717caec to your computer and use it in GitHub Desktop.
download pdf view only from google drive

Usage

  1. Open or Preview Any view-only or protected files from google drive.

  2. Open Developer Console.

    If you are previewing in Google Chrome or Firefox

    Press Shift + Ctrl + J ( on Windows / Linux) or Option + ⌘ + J (on Mac)

    If you are previewing in Microsoft Edge Press Shift + Ctrl + I

    If you are previewing in Apple Safari

    Press Option + ⌘ + C

Then you will find yourself inside the developer tools.

  1. Navigate to the Console tab.

  2. Paste below code and press Enter

Reference

let res = document.createElement("script");
res.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
document.body.appendChild(res);
res.onload = function () {
const pdf = new jsPDF();
const added_images = [];
const added_pages = [];
const images = [];
// var loopCounter = 0;
let allElements = document.querySelectorAll("*");
var chosenElement;
var total_pages;
var timestamp = new Date().getTime();
const regex = /(?<current_page>\d+)\s\/\s(?<total_pages>\d+)$/;
let heightOfScrollableElement = 0;
for (let i = 0; i < allElements.length; i++) {
if (allElements[i].scrollHeight >= allElements[i].clientHeight) {
if (heightOfScrollableElement < allElements[i].scrollHeight) {
heightOfScrollableElement = allElements[i].scrollHeight;
chosenElement = allElements[i];
}
}
}
function addImage(img) {
let result = img.alt.match(regex);
if (result) {
var current_page = parseInt(result.groups.current_page);
if (total_pages === undefined) {
total_pages = parseInt(result.groups.total_pages);
console.log("Total page ", total_pages);
}
} else {
return;
}
if (added_pages.includes(current_page) || added_images.includes(img.src)) {
return;
}
if (!/^blob:/.test(img.src)) {
console.log("invalid src");
return;
}
images[current_page] = img;
added_images.push(img.src);
added_pages.push(current_page);
console.log("added page ", current_page);
}
function rawPdf() {
images.forEach((img, index) => {
let can = document.createElement('canvas');
let con = can.getContext("2d");
can.width = img.width;
can.height = img.height;
con.drawImage(img, 0, 0);
let imgData = can.toDataURL("image/jpeg", 1.0);
pdf.addImage(imgData, 'JPEG', 0, 0);
pdf.addPage();
});
pdf.save(timestamp + ".pdf");
}
function generatePDF(remainingHeightToScroll, scrollToLocation) {
if (total_pages !== undefined && added_images.length >= total_pages) {
rawPdf();
return;
}
let scrollDistance = chosenElement.clientHeight;
let img = chosenElement.querySelectorAll("img");
for (let i = 0; i < img.length; i++) {
addImage(img[i]);
}
setTimeout(function () {
if (remainingHeightToScroll === 0) {
scrollToLocation = scrollDistance;
chosenElement.scrollTo(0, scrollToLocation);
remainingHeightToScroll = chosenElement.scrollHeight - scrollDistance;
} else {
scrollToLocation = scrollToLocation + scrollDistance;
chosenElement.scrollTo(0, scrollToLocation);
remainingHeightToScroll = remainingHeightToScroll - scrollDistance;
}
if (remainingHeightToScroll >= chosenElement.clientHeight) {
generatePDF(remainingHeightToScroll, scrollToLocation)
} else {
rawPdf();
}
}, 100)
}
generatePDF(0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment