Skip to content

Instantly share code, notes, and snippets.

@materro
Created October 18, 2024 10:27
Show Gist options
  • Select an option

  • Save materro/c8efa3e5b2abebf24e783b28fc0ed116 to your computer and use it in GitHub Desktop.

Select an option

Save materro/c8efa3e5b2abebf24e783b28fc0ed116 to your computer and use it in GitHub Desktop.
Download large protected PDF from GDrive

1. Load patched jsPDF

Copy source from custom laucheukhim jsPDF repository and just paste it to the Dev Console.

2. Run command

const { jsPDF } = window.jspdf;

3. Paste function

Inspired by tuhinpal drive-protected-pdf-downloader.js:

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,
                     pdf.internal.pageSize.getWidth(),
                     pdf.internal.pageSize.getHeight());

        // 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.");
}

4. Open GDrive PDF

Zoom it to e.g. fit the window. The more You increase zoom – the more details (and size) final PDF will have.

5. Run function

generatePDF()

6. Wait... for the download prompt

🤩

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