最近公司要求能够在ie上线上预览pdf插件

vue有vue-pdf插件可以用 但是js虽然有pdfjs等 但是新版本只支持谷歌浏览器(语法有箭头函数),所以在github找到了老版本的pdfjs,发现可用

代码为

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>'Hello, world!' example</title>
    <style type="text/css">
        #the-canvas {
            border:1px solid black;
        }
    </style>
</head>
<body>
<script src="./js/pdf.js"></script>
<h1>PDF.js 'Hello, base64!' example</h1>
<canvas id="the-canvas"></canvas>
</body>
<script type="text/javascript">
    var _url = 'xxx.html' //服务器pdf地址
     PDFJS.workerSrc =
                    './js/pdf.worker.js';
    var loadingTask = PDFJS.getDocument({ url: _url, });//  _url为后台请求pdf流的路径
    loadingTask.promise.then(function(pdf) {
        pdf.getPage(1).then(function(page) {
            var scale = 1.2;
            var viewport = page.getViewport(scale);
            var canvas = document.getElementById('the-canvas');
            var context = canvas.getContext('2d');
            canvas.height = viewport.height;
            canvas.width = viewport.width;
            var renderContext = {
                canvasContext: context,
                viewport: viewport,
            };
            page.render(renderContext);
        });
    });
</script>
</html>

pdfjs和pdf.worker文件在(无需积分嘿嘿)

https://download.csdn.net/download/weixin_38992765/19359334?spm=1001.2014.3001.5503

效果

在这里插入图片描述

IE终于要结束啦!前端要解放啦!

Logo

前往低代码交流专区

更多推荐