Picviewer CE+ PDF 扩展

取代 ZIP, 打包下载时下载为 PDF

当前为 2024-06-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Picviewer CE+ PDF addon
  3. // @name:zh-CN Picviewer CE+ PDF 扩展
  4. // @name:zh-TW Picviewer CE+ PDF 擴充
  5. // @namespace https://github.com/hoothin/UserScripts
  6. // @version 2024-06-20
  7. // @description Batch Download as PDF instead of ZIP
  8. // @description:zh-CN 取代 ZIP, 打包下载时下载为 PDF
  9. // @description:zh-TW 取代 ZIP, 打包下載時下載為 PDF
  10. // @author hoothin
  11. // @match *://*/*
  12. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  13. // @grant unsafeWindow
  14. // @require https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19. async function blobToDataURL(blob) {
  20. return new Promise((resolve) => {
  21. setTimeout(() => {
  22. var a = new FileReader();
  23. a.readAsDataURL(blob);
  24. a.onload = function (e) {
  25. resolve(e.target.result);
  26. };
  27. a.onerror = function (e) {
  28. resolve(null);
  29. };
  30. }, 0);
  31. });
  32. }
  33.  
  34. function img2pdf(pdfName) {
  35. if (!(this instanceof img2pdf)) {
  36. return new img2pdf();
  37. }
  38. this.fileList = [];
  39. this.file = async (fileName, blob) => {
  40. this.fileList.push([fileName, blob]);
  41. };
  42. this.generateAsync = async (config, progress) => {
  43. const pdf = new window.jspdf.jsPDF();
  44. const fileLength = this.fileList.length;
  45. for (const [key, param] of this.fileList.entries()) {
  46. const fileName = param[0];
  47. let blob = param[1];
  48. let dataUrl = await blobToDataURL(blob);
  49. const imgProps = pdf.getImageProperties(dataUrl);
  50. const pdfWidth = pdf.internal.pageSize.getWidth();
  51. const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
  52. pdf.addImage(dataUrl, blob.type, 0, 0, pdfWidth, pdfHeight);
  53. progress({percent: (key + 1) / fileLength * 100, currentFile: fileName});
  54. if (key + 1 < fileLength) {
  55. pdf.addPage();
  56. }
  57. }
  58. pdf.save(pdfName);
  59. };
  60. }
  61. const _unsafeWindow = typeof unsafeWindow === 'undefined' ? window : unsafeWindow;
  62. _unsafeWindow.pvcepimg2pdf = img2pdf;
  63. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址