Skip to content

Instantly share code, notes, and snippets.

@tuhinpal
Last active May 11, 2024 11:37
Show Gist options
  • Save tuhinpal/70bbde93c3bc5bc09db7d16594bd43bb to your computer and use it in GitHub Desktop.
Save tuhinpal/70bbde93c3bc5bc09db7d16594bd43bb to your computer and use it in GitHub Desktop.
Download google drive protected PDF
// Ensure jsPDF is loaded only if not already present
if (typeof jsPDF === 'undefined') {
const jspdfScript = document.createElement('script');
jspdfScript.onload = function () {
generatePDF();
};
jspdfScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js';
document.body.appendChild(jspdfScript);
} else {
generatePDF();
}
function generatePDF() {
console.log("Processing... Please wait while the PDF is being generated.");
const pdf = new jsPDF();
const images = document.querySelectorAll('img[src^="blob:"]');
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
let isFirstPage = true;
const imageStats = [];
for (const img of images) {
// Set canvas dimensions to the image dimensions
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0, img.width, img.height);
// Convert the canvas to image data
const imgData = canvas.toDataURL('image/jpeg', 1.0);
if (!isFirstPage) {
pdf.addPage();
}
isFirstPage = false;
// Add image data to the current PDF page
pdf.addImage(imgData, 'JPEG', 0, 0);
// Collect image stats for console output
imageStats.push({
Index: imageStats.length + 1,
Width: img.width,
Height: img.height,
'Source URL': img.src
});
}
// Log stats using console.table
console.table(imageStats);
// Print summary statistics
console.log(`Total Images Processed: ${imageStats.length}`);
console.log(`Total Pages in the PDF: ${imageStats.length}`);
// Save the final PDF
pdf.save(`${document.title.replace(' - Google Drive', '')}.pdf`);
console.log("PDF generation completed successfully.");
}
@tuhinpal
Copy link
Author

Usage

  • Make sure to load the whole document first
  • Make sure all pages are loaded
  • Open open developer tool cmd + shift + i and navigate to console
  • Copy the above code and paste it

Thanks!

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