How to download view only protected PDF from Google Drive (JS code)


Chia sẻ với BQT website www.hoahoc.org - Thầy Ngô Xuân Quỳnh

How to download view only protected PDF from Google Drive (JS code)

Sometimes you may have faced a situation like your friends or teachers has given you pdf, google docs, google sheets or video files with google drive links and when you open it you can preview the content of those files but when you try to download you will not get any download, copy, or print button because google has intentionally created those files with no download or print feature.

In general, these types of files are view-only or protected google drive files. Suppose you like download those files and save them locally then without a hack you cannot achieve. Don’t worry guys I will help you. In this article, I will be explaining to you how to download protected view-only files from google drive.

You can easily check whether files are view-only files for this just preview the file and hover your mouse cursor then at the top you will find no download button and print features are disabled as shown below.

Thao tac tai

The Coding Cat got some document that he could not read on his Android phone or iPad showing only error because of read only mode that the mobile devices could not handle. He needed it on other devices than his PC, so he wrote a little ‘hack’  in JS.

Note 1: It was tested on Opera Browser.

Lưu ý 1: Nó đã được thử nghiệm trên Trình duyệt Chrome.

Note 2: It converts pages to jpg images. The Coding Cat thinks it could be done preserving text, but he didn’t have more time for this and jpg solution was sufficient.

Lưu ý 2: Nó chuyển đổi các trang thành hình ảnh jpg. Tôi nghĩ rằng có thể thực hiện việc bảo quản văn bản, nhưng anh ấy không có nhiều thời gian hơn cho việc này và giải pháp jpg là đủ.

Note 3: If you’re getting only part of the document visible, try zooming out your browser and then run the script.

Lưu ý 3: Nếu bạn chỉ nhìn thấy một phần của tài liệu, hãy thử thu nhỏ trình duyệt của mình rồi chạy tập lệnh.

Thao tac

Open Developer Console.

   If you are previewing in Google Chrome or Firefox

     Press Shift Ctrl J ( on Windows / Linux) or Option ⌘  (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.

3  Navigate to the “Console” tab.

4  Paste below code and press Enter

Step by step:

Bước 1: Open the document in Google Docs

Bước 2: Scroll to the bottom of the document, so all the pages are present (Cuộn xuống cuối tài liệu để tất cả các trang đều hiện diện – lưu ý là bạn cần cuộn từ từ để hiển thị hoàn toàn các trang tài liệu)

Bước 3: Open Developer Tools on separate window and choose the Console tab (Mở Công cụ dành cho nhà phát triển  trên cửa sổ riêng và chọn tab Bảng điều khiển – Thao tác nhanh là nhấn tổ hợp phím Ctrl + Shift + I, sau đó chọn tới tap Console, sau đó bạn thực hiện bước 4)

Tai Lieu

Bước 4: Paste the code below (and hit enter) (Dán mã bên dưới (và nhấn enter))

Mã code

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 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, img.width, img.height);
        let imgData = can.toDataURL("image/jpeg", 1.0);
        pdf.addImage(imgData, 'JPEG', 0, 0);
        pdf.addPage();
    }
    pdf.save("download.pdf");
};
document.body.appendChild(jspdf);

5. Now the PDF should be downloaded (Bây giờ PDF sẽ được tải xuống)

 

What it does? It iterates trough the document checking for images (Google Drive stores pages as images) then writes it’s contents to a PDF.

Leave a comment if it works for you.


QC

Leave a comment

About The Author

Edusmart

➤ Website: www.hoahoc.org
➤ Facebook page: https://www.facebook.com/luyenthihathanhhn
➤ Hotline: 0989.16.18.66
📍 Cơ sở học tập 1: • 288 - Số 26 Đường Láng - Ngã Tư Sở
🏘️ Links google maps cơ sở 1:
https://maps.app.goo.gl/4DH9XTMFLawfd95S7

📍 Cơ sở học tập 2: • 47 - Ngõ 25 - Phố Phan Đình Phùng
🏘️ Links google maps cơ sở 2:
https://maps.app.goo.gl/kqHm3VUN89pkwo1X7

More From Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like