首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >jsPDF 实现 Google 云端硬盘只读 PDF 文件下载导出

jsPDF 实现 Google 云端硬盘只读 PDF 文件下载导出

作者头像
Tony He
发布于 2022-11-17 14:41:14
发布于 2022-11-17 14:41:14
2.8K0
举报
文章被收录于专栏:ouorz.comouorz.com

背景

https://www.savemyexams.co.uk

蜜汁刷题网站,会员简直就是在抢钱

好不容易搞了个教师折扣,试卷资源还不能下载...

本方法适用于 Google Drive 被设置为 Read Only 的 PDF 文件的下载导出

代码

代码语言:javascript
复制
/* 
	Via https://codingcat.codes/2019/01/09/download-view-protected-pdf-google-drive-js-code/
	If the images are not complete, try zooming the page to get the full image.
	1. Open Developer Tools on separate window and choose the Console tab
	2. Paste the code below (and hit enter)
*/

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];
        if (!/^blob:/.test(img.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(document.title.split('.pdf - ')[0]+".pdf");
};
 
jspdf.src = 'https://cdn.bootcss.com/jspdf/1.5.3/jspdf.debug.js';
document.body.appendChild(jspdf);

↑ JavaScript 代码

注意

原代码来自 https://codingcat.codes/2019/01/09/download-view-protected-pdf-google-drive-js-code/

  1. 打开浏览器开发者工具(单窗口打开)选择 Console / 控制台
  2. 复制代码,回车执行

如果图片绘制不完整,尝试缩放浏览器页面

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 背景
  • 代码
  • 注意
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档