1.下载PDF.js

官网地址: http://mozilla.github.io/pdf.js/
解压后放入vue项目,我是放在pubilc文件夹中

在这里插入图片描述

我是放在pubilc文件夹中

2.使用

//看效果直接在页面中调用即可
window.open("/pdfjs-3.0.279-legacy-dist/web/viewer.html")

在这里插入图片描述

我的需求是只能预览,所以用了下面的写法来隐藏阿里云链接,在viewer.js里注释掉这不然会报错

在这里插入图片描述

 open(){
        var newWin = window.open('','newwindow','toolbar =no, menubar=no, scrollbars=no, resizeable=no, location=no, status=no');
        newWin.document.write('<body scroll="no" style="margin: 0px;padding: 0px;border:0px;overflow:hidden;"><iframe style="margin: 0px;padding: 0px;border: 0px;width:100%;height:100%" src="/pdfjs/web/viewer.html?file=https://online.oss-cn-shenzhen.aliyuncs.com/*****.pdf"></iframe></body>');
      },
如果直接访问阿里云链接有跨域问题,我的解决方法是在阿里云跨域规则配置中开启GET请求即可

需要禁止打印和下载的在viewer.html和viewer.js文件中加入以下操作
style=“visibility: hidden”
eventBus._on(“print”, webViewerPrint)
在这里插入图片描述
在这里插入图片描述

![在这里插入图片描述](https://img-blog.csdnimg.cn/7985d621fde94c47993260ffab4e2768.png

在PDF中添加水印

在这个地方注释后添加以下代码,找不到可以搜索这几句
	textLayerDiv.className = "textLayer";
    textLayerDiv.style.width = canvasWrapper.style.width;
    textLayerDiv.style.height = canvasWrapper.style.height;

在这里插入图片描述

  var cover = document.createElement('div');
      cover.className = "cover";
      for (var y = 0; y < 5; y++) {
        for (var x = 0; x < 4; x++) {
          let c = document.createElement('div')
          c.className = "cover"
          c.style.position = 'absolute';
          c.style.left = (10+x/4*100)+'%';
          c.style.top = (10+y/5*100)+'%';
          c.style.transform = "rotate(315deg)";
          c.style.color = "rgba(0, 191, 255, 0.2)";
          c.innerText = text;//text为水印内容,可以在viewer.html中传入,也可以直接替换成固定的字符串如:c.innerText = "这是一个水印";
          cover.appendChild(c);
        }
      }
      if (this.annotationLayer?.div) {
        div.insertBefore(textLayerDiv, this.annotationLayer.div);
        div.appendChild(cover);
      } else {
        div.appendChild(textLayerDiv);
        div.appendChild(cover);
      }
GitHub 加速计划 / pd / pdf.js
9
0
下载
PDF Reader in JavaScript
最近提交(Master分支:7 个月前 )
a4950c0b Simplify singularValueDecompose2dScale in order to make it using less memory 20 小时前
6e9fbd94 In using the Firefox profiler (with JS allocations tracking) and wuppertal.pdf, I noticed we were using a bit too much memory for a function which is supposed to just compute 2 numbers. The memory used by itself isn't so important but having a too much objects lead to waste some time to gc them. So this patch aims to simplify it a bit. 23 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐