欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用pdfjs插件在线预览PDF文件

程序员文章站 2022-12-21 22:29:34
前言 本文介绍在html中使用 pdfjs插件在线预览PDF文件的方法。 实现步骤 下载 pdfjs 并引入项目中 到PDFJS官网 http://mozilla.github.io/pdf.js/getting_started/#download 下载pdfjs插件包,注意下载Stable稳定版的 ......

前言

  本文介绍在html中使用 pdfjs插件在线预览pdf文件的方法。

实现步骤

  • 下载 pdfjs 并引入项目中

  到pdfjs官网 http://mozilla.github.io/pdf.js/getting_started/#download 下载pdfjs插件包,注意下载stable稳定版的包。将下载的压缩包解压并放入项目中。

 

  • 使用方法

  在 iframe 标签中使用。假设 pdfjs 包放在目录 ../static 下。则写法如下:

<iframe id="previewpdf" src=""../static/pdfjs/web/viewer.html?file="+url+"#page=1" width="100%" frameborder="0"></iframe>

其中,src中的url是pdf文件的预览地址、page是设置pdf打开时从第一页开始显示。

 

  • 样例
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>pdf文件预览</title>
        <script src="../javascript/jquery-2.2.4.js"></script>
        <script type="text/javascript">
            $(function () {
                var url = getpdfpreviewurl(); //获取pdf预览地址
                $("#pdfcontainer").attr("src", "../static/pdfjs/web/viewer.html?file="+url+"#page=1");
            });
        </script>
    </head>
    <body>
        <div id="showpdf">
            <iframe id="pdfcontainer" src="" width="100%" frameborder="0"></iframe>
        </div>
    </body>
</html>