使用PDF.JS插件在HTML中预览PDF文件的方法

Ona ·
更新时间:2024-09-20
· 951 次阅读

PDF.js是一款基于HTML5建立的PDF阅读器,兼容大部分主流的浏览器,使用起来,也相对简单。步骤如下:

1.下载PDF.js插件

下载地址:http://mozilla.github.io/pdf.js/getting_started/#download

2.文件部署

建新文件夹,将PDF.js插件文件放入新建的文件夹。

3.新建index.html

在HTML文件代码如下:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta name="google" content="notranslate"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>《使用指南》-柠檬课件</title> <script> function openFile () { var url = "pdf.pdf"; window.open("pdfjs/web/viewer.html?file=" + url); } </script> </head> <body> <input type="button" onclick="openFile()" value="打开"> </body> </html>

4.将pdf.pdf文件放到pdfjs/web/下

5.点击浏览即可。

注意:因为pdf.js需要依赖http,https协议。所以直接浏览index打开报错。搭个服务,或者用idea的浏览就可以了。



pdf js

需要 登录 后方可回复, 如果你还没有账号请 注册新账号