h5手机端pdf预览(可缩放)

  1. npm i pdfh5 -s

  2. import Pdfh5 from "pdfh5";

    import "pdfh5/css/pdfh5.css";

  3. html中使用如下:

    <div id="pdf-content" style="height:100%;width:100%;"></div>

  4. 代码中使用如下:

    initPdf() {
          // pdfh5实例化时传两个参数:selector选择器,options配置项参数,会返回一个pdfh5实例对象,
          // 可以用来操作pdf,监听相关事件
          // pdfh5 = new Pdfh5(selector, options) goto初始到第几页,logo设置每一页pdf上的水印
          this.pdfh5 = new Pdfh5("#pdf-content", {
            pdfurl: this.pdfUrl,
            goto: 1,
            // 设置每一页pdf上的水印
            // logo: { src: require("@/assets/images/icon27.png"), x: 20, y: 70, width: 60, height: 60 },
          });
          this.pdfh5.scrollEnable(true); // 允许pdf滚动
          // 监听pdf准备开始渲染,此时可以拿到pdf总页数
          this.pdfh5.on("ready", function () {
            console.log("总页数:" + this.totalNum);
          });
          // 监听pdf加载完成事件,加载失败、渲染成功都会触发
          this.pdfh5.on("complete", (status, msg, time) => {
            console.log("状态:" + status + ",信息:" + msg + ",耗时:" + time + "毫秒");
          });
        },
    
    //自带的下载貌似不管用
    
    this.pdfh5.download()

    下载可利用浏览器访问的链接下载:

  5. // 下载文件,自定义文件名称
        downFile(url, fileName) {
          let ele = document.createElement('a')
          ele.download = fileName
          ele.style.display = 'none'
          ele.href = url
          document.body.appendChild(ele)
          ele.click()
          document.body.removeChild(ele)
        },

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用pdf.js库来在H5手机iframe中PDF文件。下面是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <title>PDF Preview</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js"></script> </head> <body> <iframe id="pdfPreview" style="width:100%;height:100%;border:none;"></iframe> <script> const pdfUrl = "https://example.com/sample.pdf"; const iframe = document.getElementById("pdfPreview"); const pdfjsLib = window['pdfjs-dist/build/pdf']; pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js'; pdfjsLib.getDocument(pdfUrl).promise.then(pdf => { pdf.getPage(1).then(page => { const scale = 1.5; const viewport = page.getViewport({scale: scale}); const canvas = document.createElement("canvas"); const context = canvas.getContext("2d"); canvas.height = viewport.height; canvas.width = viewport.width; const renderContext = { canvasContext: context, viewport: viewport }; page.render(renderContext).promise.then(() => { iframe.src = canvas.toDataURL(); }); }); }); </script> </body> </html> ``` 在上面的示例中,我们使用pdf.js库来获取PDF文件的第一页,并将其渲染到canvas元素中。然后,我们将canvas的数据URL设置为iframe的src属性。这将在iframe中显示PDF文件的。请注意,示例中使用的PDF文件URL(pdfUrl)应替换为您自己的PDF文件URL。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值