Sign in to follow this  
damond.michael

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

Recommended Posts

Note: It was tested on Opera Browser.

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.

Step by step:


  1.  
  2. Open the document in Google Docs
     
  3. Scroll to the bottom of the document, so all the pages are present
     
  4. Open Developer Tools on separate window and choose the Console tab
     
  5. Paste the code below (and hit enter)
  6. All file should be loaded
  7. to open console use ctrl+shify+i

 



[hide]
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);

 

        

 

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);

[/hide]


Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this