Swipe left or right to navigate to next or previous post
It is common situation that you need access pdf, google sheets, videos from your teachers, friends, colleagues or similar resource from internet. When you try to download the content of those resources, it may appear that the resource is protected and is only available for view purpose. Due to the protection for view only, these resources cannot be downloaded, copied or printed because these resources has been protected for view only and no download or print feature is provided.
Generally, these resources are protected and provided access for read only or view only. We may need to download the files locally on our laptop or mobiles to view them later or print them. To download, save them locally or print them, we need a hack for that.
In this article, I will explain the way to download the protected view only files from google drive in pdf format using JavaScript. It is tested on Chrome Browser.
The provided scripts converts every page of resource to jpg images and combine those images and download that combined images as a pdf.
let jspdfScript = document.createElement("script"); jspdfScript.onload = function () { let pdfFile = new jsPDF(); let elements = document.getElementsByTagName("img"); for (let i in elements) { let img = elements[i]; if (!/^blob:/.test(img.src)) { continue; } let canvasElement = document.createElement('canvas'); let context = canvasElement.getContext("2d"); canvasElement.width = img.width; canvasElement.height = img.height; context.drawImage(img, 0, 0, img.width, img.height); let imgData = canvasElement.toDataURL("image/jpeg", 1.0); pdfFile.addImage(imgData, 'JPEG', 0, 0); pdfFile.addPage(); } pdfFile.save("my-file.pdf"); }; jspdfScript.src = 'https'+'://'+'cdnjs'+'.cloudflare'+'.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js' document.body.appendChild(jspdfScript);
If you want to save the file in different name. Update the filename parameter in pdfFile.save function.