vue-office/examples/dist/assets/PdfDemo-7ac44a10.js

2 lines
3.0 KiB
JavaScript

import{o as L,p as $,w as j}from"./omit-bd103a0f.js";import{g as S,d as W,l as R}from"./url-de9b02cf.js";import{d as E,_ as F,r as v,o as O,w as U,a as p,c as w,F as B,k as C,l as D,e as V,f as T,g as J,u as N}from"./index-50d431e6.js";import{P as q,u as P}from"./PreviewWrapper-97fd9bf3.js";const A=`data:text/javascript;base64,${$}`,G=`data:text/javascript;base64,${j}`,H=E({name:"VueOfficePdf",props:{src:[String,ArrayBuffer,Blob],requestOptions:{type:Object,default:()=>({})},staticFileUrl:{type:String,default:"https://unpkg.com/pdfjs-dist@3.1.81/"},options:{type:Object,default:()=>({})}},emits:["rendered","error"],setup(e,{emit:n}){let o=null,i=null;const c=v([]),r=v(0);function m(){return R(A).then(()=>{window.pdfjsLib.GlobalWorkerOptions.workerSrc=G})}function g(){return window.pdfjsLib?Promise.resolve():m()}function k(){if(!e.src){r.value=0;return}i=window.pdfjsLib.getDocument({url:S(e.src),cMapUrl:`${e.staticFileUrl.endsWith("/")?e.staticFileUrl:e.staticFileUrl+"/"}cmaps/`,cMapPacked:!0,enableXfa:!0,...L(e.options,["width"])}),i.promise.then(t=>{o=t,r.value=o.numPages,x(1)}).catch(t=>{n("error",t)})}function x(t){o.getPage(t).then(a=>{const d=a.getViewport({scale:2}),l=window.devicePixelRatio||1,f=c.value[t-1],y=f.getContext("2d");f.width=Math.floor(d.width*l),f.height=Math.floor(d.height*l);let s=Math.floor(d.width),u=Math.floor(d.height);if(e.options.width){let h=e.options.width/s;s=Math.floor(e.options.width),u=Math.floor(u*h)}if(s>document.documentElement.clientWidth){let h=document.documentElement.clientWidth/s;s=Math.floor(document.documentElement.clientWidth),u=Math.floor(u*h)}f.style.width=s+"px",f.style.height=u+"px";const M=l!==1?[l,0,0,l,0,0]:null;a.render({canvasContext:y,transform:M,viewport:d}).promise.then(()=>{r.value>t?x(t+1):n("rendered")}).catch(h=>{n("error",h)})}).catch(a=>{n("error",a)})}O(()=>{e.src&&g().then(k)}),U(()=>e.src,()=>{g().then(k)});function b(t){o&&o._transport&&o._transport.getData().then(a=>{W(t||`vue-office-pdf-${new Date().getTime()}.pdf`,a.buffer)})}return{rootRef:c,numPages:r,download:b}}}),X={class:"vue-office-pdf",ref:"vue-office-pdf",style:{"text-align":"center","overflow-y":"auto"}},z={key:0,class:"vue-office-pdf-wrapper",style:{background:"gray",padding:"30px 0",position:"relative"}};function I(e,n,o,i,c,r){return p(),w("div",X,[e.numPages?(p(),w("div",z,[(p(!0),w(B,null,C(e.numPages,m=>(p(),w("canvas",{ref_for:!0,ref:"rootRef",key:m,style:{width:"100%"}}))),128))])):D("",!0)],512)}const _=F(H,[["render",I]]);_.install=function(e){e.component(_.name,_)};const te={__name:"PdfDemo",setup(e){function n(){P.hideLoading()}function o(r){console.log("出差",r),P.hideLoading()}const i=location.origin+(location.pathname+"/").replace("//","/")+"static/test-files/test.pdf",c=v();return(r,m)=>(p(),V(q,{accept:".pdf",placeholder:"请输入pdf文件地址","default-src":i},{default:T(g=>[J(N(_),{ref_key:"docxRef",ref:c,src:g.src,style:{flex:"1",height:"0"},options:{},onRendered:n,onError:o},null,8,["src"])]),_:1}))}};export{te as default};