DEV Community

David Paluy
David Paluy

Posted on

Unlocking Protected PDFs

Several years ago, I wrote a Gist that received a lot of positive interest, highlighting a common need among developers. Given its popularity, it’s time to share this solution with you. This post will guide you through a practical method to download protected PDFs using JavaScript, ensuring high-resolution output.

This approach allows you to bypass view-only restrictions by capturing high-resolution images of each page.

Step 1: Open the Document

Open the protected document in Google Docs.
Scroll through the entire document to ensure all pages are fully loaded. Some documents require zoom-in to get a better resolution.

Step 2: Open Developer Tools

Navigate to the Console tab.

Step 3: Run this Script to convert images to PDF

let jspdf = document.createElement("script");

jspdf.onload = function () {

    let pdf = new jsPDF();
    let elements = document.getElementsByTagName("img");
    for (let i in elements) {
        let img = elements[i];
        console.log("add img ", img);
        if (!/^blob:/.test(img.src)) {
            console.log("invalid image src");
            continue;
        }
        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("download.pdf");
};

jspdf.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
document.body.appendChild(jspdf); 
Enter fullscreen mode Exit fullscreen mode

Note: Check the original Gist and other comments with various improvements and suggestions.

Note on Ethical Use

Remember to respect copyright and privacy laws. Use this method responsibly and only for documents you have the right to download.

Top comments (0)