filepond-plugin-pdf-convert
Version:
Include PDF file convertor plugin
9 lines (7 loc) • 1.72 kB
JavaScript
/*!
* FilePondPluginPdfConvert 1.0.0
* Licensed under ISC, https://opensource.org/licenses/ISC/
* Please visit undefined for details.
*/
/* eslint-disable */
const e=(e,t,n)=>{const i=[],o=[];let a=0,r=0,d=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),h=await pdfjsLib.getDocument(c).promise;!async function c(){const u=await h.getPage(d),p=u.getViewport({scale:1.5}),l=document.createElement("canvas"),w=l.getContext("2d"),g={canvasContext:w,viewport:p};l.height=p.height,l.width=p.width,await u.render(g).promise,i.push(w.getImageData(0,0,l.width,l.height)),o.push(r),r+=l.height,a<l.width&&(a=l.width);if(d<h.numPages)d++,r+=n,c();else{const n=function(){const e=document.createElement("canvas"),t=e.getContext("2d");e.width=a,e.height=r;for(let e=0;e<i.length;e++)t.putImageData(i[e],0,o[e]);return e}(),d=atob(n.toDataURL(t).split(",")[1]),c=[];for(let e=0;e<d.length;e++)c.push(d.charCodeAt(e));const h=new Blob([new Uint8Array(c)],{type:t});s(new File([h],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]}}};"undefined"!=typeof window&&void 0!==window.document&&document.dispatchEvent(new CustomEvent("FilePond:pluginloaded",{detail:t}));export default t;