UNPKG

filepond-plugin-pdf-convert

Version:
9 lines (7 loc) 1.97 kB
/*! * FilePondPluginPdfConvert 1.0.0 * Licensed under ISC, https://opensource.org/licenses/ISC/ * Please visit undefined for details. */ /* eslint-disable */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).FilePondPluginPdfConvert=t()}(this,(function(){"use strict";const e=(e,t,n)=>{const i=[],o=[];let a=0,d=0,r=1;return new Promise(((s,c)=>{if("undefined"==typeof pdfjsLib){const e="The library PDF.js is required to convert PDF in image";return console.warn(e),void c(e)}try{const c=new FileReader;c.onload=async function(){const c=new Uint8Array(this.result),u=await pdfjsLib.getDocument(c).promise;!async function c(){const f=await u.getPage(r),h=f.getViewport({scale:1.5}),l=document.createElement("canvas"),p=l.getContext("2d"),g={canvasContext:p,viewport:h};l.height=h.height,l.width=h.width,await f.render(g).promise,i.push(p.getImageData(0,0,l.width,l.height)),o.push(d),d+=l.height,a<l.width&&(a=l.width);if(r<u.numPages)r++,d+=n,c();else{const n=function(){const e=document.createElement("canvas"),t=e.getContext("2d");e.width=a,e.height=d;for(let e=0;e<i.length;e++)t.putImageData(i[e],0,o[e]);return e}(),r=atob(n.toDataURL(t).split(",")[1]),c=[];for(let e=0;e<r.length;e++)c.push(r.charCodeAt(e));const u=new Blob([new Uint8Array(c)],{type:t});s(new File([u],e.name,{type:t}))}}()},c.readAsArrayBuffer(e)}catch(e){c(e)}}))},t=({addFilter:t,utils:n})=>{const{Type:i}=n;return t("LOAD_FILE",((t,{query:n})=>new Promise(((i,o)=>{(e=>/pdf$/.test(e.type))(t)?e(t,n("GET_PDF_CONVERT_TYPE"),n("GET_PDF_CONVERT_MARGIN_HEIGHT")).then((function(e){i(e)})).catch((()=>i(t))):i(t)})))),{options:{pdfConvertType:["image/png",i.STRING],pdfConvertMarginHeight:[30,i.NUMBER]}}};return"undefined"!=typeof window&&void 0!==window.document&&document.dispatchEvent(new CustomEvent("FilePond:pluginloaded",{detail:t})),t}));