首页 > 其他分享 >web页面展示PDF文件

web页面展示PDF文件

时间:2023-05-18 16:46:46浏览次数:28  
标签:web canvas const docx new window PDF pdf 页面

简单展示PDF

1. 下载pdf.js插件

<script src='https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.6.172/pdf.min.js'></script>

2. 解析PDF文件渲染为canvas

 const pdfjsLib = window.pdfjsLib
    pdfjsLib.getDocument('path/demo1.pdf').promise
      .then(function(pdf) {
      
      pdf.getData().then(e => {
       
      })
        pdf.getPage(pageNum).then(function(page) {
             page.getTextContent().then(function(textContent) {
                  let canvas = document.getElementById('canvas');               let context = canvas.getContext('2d');               let viewport = this.page.getViewport({scale: 2.5});               canvas.height = viewport.height;               canvas.width = viewport.width;                         let renderContext = {                 canvasContext: context,                 viewport: viewport,                 // background: '#f0f098',                 pageColors: {                   background: '#fff',                   foreground: '#fff'                 }               };             page.render(renderContext)
            })
        });
      }).catch(function(error) {
        
      });            

 

3. 下载PDF为word

    <script src="https://unpkg.com/[email protected]/build/index.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
page.render(renderContext).promise.then(() => {
   // 获取canvas画布上的图片数据      const imageData = canvas.toDataURL('image/png');      // 将数据转化为Blob格式      const pdfBlob = dataURItoBlob(imageData);


        const doc = new window.docx.Document({
          sections: [{
              children: [
                  new window.docx.Paragraph({
                      children: [
                        new window.docx.ImageRun({
                        data: imageData,
                          transformation: {
                            width: 600,
                            height: 1000,
                          }
                        })
                      ],
                  }),
              ],
          }]
        })
        // 保存文档
        window.docx.Packer.toBlob(doc).then(blob => {
          saveAs(blob, 'my-word.docx')
        })

})
function dataURItoBlob(dataURI) {   const byteString = atob(dataURI.split(',')[1]);   const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];   const ab = new ArrayBuffer(byteString.length);   const ia = new Uint8Array(ab);
  for (let i = 0; i < byteString.length; i++) {       ia[i] = byteString.charCodeAt(i);   } }
 

 

以上主要是把pdf解析为canvas,如果想解析dom标签,目前只能拿到位置信息把文本排版展示好,范样式信息,线段,表格等信息无法获取

如何想要拿到样式信息,需要深入阅读pdf.js源码了

标签:web,canvas,const,docx,new,window,PDF,pdf,页面
From: https://www.cnblogs.com/styleFeng/p/17412397.html

相关文章

  • vue中pdf预览,报错(Cannot read properties of undefined (reading ‘catch‘))解决
    1、在node_modules中找到vue-pdf,src下的pdfjsWrapper.jsif(pdfRender!==null){if(canceling)return;canceling=true;pdfRender.cancel().catch(function(err){emitEvent('error',err);});return;}修改为if(pdf......
  • 低版本Grafana使用原生webhook实现企业微信告警
    公司现有生产环境普遍使用的还是Grafana7版本,而Grafana9版本才支持企业微信告警。钉钉倒是支持,但是公司不用钉钉,不想就为了这个下一个软件。且Grafana的版本升级太激进,每个大版本之间的差异巨大,我也不想升级,于是另辟蹊径,整了个脚本接收Grafana的POST请求,把请求内容发送到我的企业......
  • IDEA/WEBSTORM配置静态的html,提供给同一局域网访问
    配置端口和勾选不信任的链接 配置Deployment 最重要的一步:重启IDE访问配置的链接即可,可以把localhost改成本机的ip,供同一局域网的人使用了。 ......
  • js下载pdf文件
    //点击下载文件参数href:下载地址,参数filename:文件名(可自定义文件名)```downloadFile(href,fileName){//获取heads中的filename文件名leta=document.createElement("a");a.style.display="none";//给a标签创建下载的链接a.href=href;//下载后文件名a.downloa......
  • web实践
    学了点前端,练个手代码<html><head><title>Web程序设计作业1</title><metacharset="utf-8"><scriptsrc="js/jquery-1.9.1.min.js"type="text/javascript"></script>&......
  • PowerShell-将word另存为pdf
    $folderPath="D:\工作\temp\2023年4月19日"$folderPathOut="D:\工作\temp\2023年4月19日"$wordFiles=Get-ChildItem-Path$folderPath-Filter"*.docx"try{Get-Process-Name"*word*"|Stop-Process#避免之前运行失败的......
  • [D盾_web查杀]网站后面查找工具
    本文转载自:[D盾_web查杀]网站后面查找工具更多内容请访问钻芒博客:https://www.zuanmang.net在一定程度上还是有点用的,从网上下载来的源码模板可能并没那么干净。官网:http://www.d99net.net/官网最新版本下载(2019-6-29)『D盾_防火墙』版本:v2.1.4.9:http://www.d99net.net/down......
  • Spartacus product 明细页面的产品图片显示
    Spartacus产品明细页面(productdetailpage)如下图所示:其selector为cx-product-images:在Spartacus实现里,有两个Component都使用了这个selector,但是PDP页面使用的是前者,即下图图例1所示的ProductImageZoomProductImagesComponent:这个Component里有一个cx-m......
  • c# web中实现文件上传下载的三种解决方案(推荐)
    ​IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头。 一. 两个必要响应头Accept-Ranges、ETag        客户端每次提交下载请求时,服务端都要添加这两个响应头,以保证客户端和服务端将此下载识别为可以断点续传......
  • VS2019新建WebService/Web服务/asmx并通过IIS实现发布和调用
    场景对接第三方系统提供接口文档中显示为asmx接口 访问接口返回数据格式为 xml中的数据格式为json数据。需要在本地新建并模拟调试环境。注:博客:https://blog.csdn.net/badao_liumang_qizhi实现1、WebServiceWebService是一种远程调用技术,也叫XMLWebServiceWe......