var d=Object.defineProperty;var u=(r,t,e)=>t in r?d(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var a=(r,t,e)=>(u(r,typeof t!="symbol"?t+"":t,e),e);import{o as f,w,p as m}from"./omit-bd103a0f.js";import{l as v,g,d as P}from"./url-de9b02cf.js";/* empty css */import{r as M,o as b,a as k,c as x}from"./index-50d431e6.js";const _=`data:text/javascript;base64,${m}`,C=`data:text/javascript;base64,${w}`;class D{constructor(t,e={},i={}){a(this,"container",null);a(this,"wrapper",null);a(this,"wrapperMain",null);a(this,"options",{});a(this,"requestOptions",{});a(this,"pdfDocument",null);this.container=t,this.options={staticFileUrl:"https://unpkg.com/pdfjs-dist@3.1.81/",...e},this.requestOptions=i,this.createWrapper()}createWrapper(){this.wrapper=document.createElement("div"),this.wrapper.className="vue-office-pdf",this.wrapper.setAttribute("style","text-align: center;overflow-y: auto;"),this.container.appendChild(this.wrapper)}createWrapperMain(){this.wrapperMain=document.createElement("div"),this.wrapperMain.className="vue-office-pdf-wrapper",this.wrapperMain.setAttribute("style","background: gray; padding: 30px 0;position: relative;"),this.wrapper.appendChild(this.wrapperMain)}createCanvas(){const t=document.createElement("canvas");return t.setAttribute("style","width:100%"),this.wrapperMain.appendChild(t),[t,t.getContext("2d")]}installPdfScript(){return v(_).then(()=>{window.pdfjsLib.GlobalWorkerOptions.workerSrc=C})}checkPdfLib(){return window.pdfjsLib?Promise.resolve():this.installPdfScript()}getDocument(t){return window.pdfjsLib.getDocument({url:g(t),cMapUrl:`${this.options.staticFileUrl.endsWith("/")?this.options.staticFileUrl:this.options.staticFileUrl+"/"}cmaps/`,cMapPacked:!0,enableXfa:!0,...f(this.options,["width","staticFileUrl"])}).promise}renderSinglePage(t){return this.pdfDocument.getPage(t).then(e=>{const i=e.getViewport({scale:2}),s=window.devicePixelRatio||1;let[n,l]=this.createCanvas();n.width=Math.floor(i.width*s),n.height=Math.floor(i.height*s);let o=Math.floor(i.width),p=Math.floor(i.height);if(this.options.width){let c=this.options.width/o;o=Math.floor(this.options.width),p=Math.floor(p*c)}if(o>document.documentElement.clientWidth){let c=document.documentElement.clientWidth/o;o=Math.floor(document.documentElement.clientWidth),p=Math.floor(p*c)}n.style.width=o+"px",n.style.height=p+"px";const h=s!==1?[s,0,0,s,0,0]:null;return e.render({canvasContext:l,transform:h,viewport:i}).promise.then(()=>{this.pdfDocument.numPages>t&&this.renderSinglePage(t+1)})})}renderPage(){return this.wrapperMain||this.createWrapperMain(),this.renderSinglePage(1)}clearAllCanvas(){this.wrapperMain&&(this.wrapper.removeChild(this.wrapperMain),this.wrapperMain=null)}setOptions(t){this.options=t}setRequestOptions(t){this.requestOptions=t}preview(t){return new Promise((e,i)=>{if(!t){this.clearAllCanvas(),i(new Error("预览地址不能为空"));return}this.checkPdfLib().then(s=>{this.getDocument(t).then(n=>{this.pdfDocument=n,this.renderPage().then(l=>{e()}).catch(l=>{this.clearAllCanvas(),i(l)})}).catch(n=>{this.clearAllCanvas(),i(n)})}).catch(s=>{this.clearAllCanvas(),i(s)})})}download(t){this.pdfDocument&&this.pdfDocument._transport&&this.pdfDocument._transport.getData().then(e=>{P(t||`js-preview-pdf-${new Date().getTime()}.pdf`,e.buffer)})}destroy(){this.container.removeChild(this.wrapper),this.container=null,this.wrapper=null,this.wrapperMain=null,this.options={},this.requestOptions={},this.pdfDocument=null}}function y(r,t,e){return new D(r,t,e)}const W={init:y},j={__name:"JsPdfDemo",setup(r){const t=M(null);return b(()=>{window.myPdfPreview=W.init(t.value),window.myPdfPreview.preview("/vue-office/examples/dist/static/test-files/test.pdf").then(e=>{console.log("excel preview done",window.myPdfPreview)}).catch(e=>{console.log("err",e)})}),(e,i)=>(k(),x("div",{ref_key:"dom",ref:t,style:{height:"calc(100vh - 50px)"}},null,512))}};export{j as default};