js implements pdf online preview and printing (full version)
What I want to write today is because there are too few useful things on the Internet, so I realized it half-expletively.
1. Tell us your needs: click on the title to jump to the previewed pdf page, and the download function is optional [preferably].
2. Implementation result:
3. Code implementation:
Depends on pdf.js [Need to download the complete control]
Download official website: http://mozilla.github.io/pdf.js/
Click 'Download ' to go to the download page
git clone or download.
The file looks like this after downloading:
[The focus is later on how to deploy and assemble the project]
1. Create a new empty project and put the file Place it in the project root directory:
The red circle is downloaded from the official website. Just change the file name and drag it into the project without touching it at all. Remember any documents if necessary.
The green one is written by me [dowwn.html is a test file; static is a pdf file] Paste the code below:
list.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-touch-fullscreen" content="yes"> <meta name="full-screen" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="format-detection" content="telephone=no"> <meta name="format-detection" content="address=no"> <title>list</title> <style> *{ margin: 0; padding: 0; } .title{ background: #e50041; color: #ffffff; font-size: 16px; } .title{ padding:10px 10px ; } ul{ padding:0px 10px 10px 10px ; } li{ list-style: none; border-bottom: 1px solid #eeeeee; height: 50px; line-height:50px; } a{ text-decoration: none; color: #000; } .leftImg{ width: 30px; vertical-align: middle; } .next{ float: right; /*vertical-align: middle;*/ margin-top: 4.5%; } </style> </head> <body> <p class="title">产品说明书</p> <ul> <li dataSrc = 'KD-122LA火灾探测报警器说明书.pdf' onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">KD-122LA火灾探测报警器说明书ccc</span> <img class="next" src="img/next.png" alt=""> </li> <li dataSrc = 'KD-212LA可燃气体探测器说明书.pdf' onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">KD-212LA 可燃气体探测器说明书</span> <img class="next" src="img/next.png" alt=""> </li> <li dataSrc = 'KD-216LA可燃气体探测器说明书.pdf' onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">KD-216LA可燃气体探测器说明书</span> <img class="next" src="img/next.png" alt=""> </li> <li dataSrc = 'Kd-122LA_KD-601系统遥控器使用说明.pdf' onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">Kd-122LA KD-601系统遥控器使用说明</span> <img class="next" src="img/next.png" alt=""> </li> <li dataSrc = 'KD-602LA_SOS一键救助使用说明书.pdf' onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">KD-602LA SOS一键救助使用说明书</span> <img class="next" src="img/next.png" alt=""> </li> <li dataSrc = 'KD-701LA_溢水探测器使用说明书.pdf' onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">KD-701LA 溢水探测器使用说明书</span> <img class="next" src="img/next.png" alt=""> </li> <li dataSrc = 'KD-702LA红外人体移动探测器说明书.pdf' onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">KD-702LA红外人体移动探测器说明书</span> <img class="next" src="img/next.png" alt=""> </li> <li dataSrc = 'KD-703LA_门窗探测器使用说明书.pdf' onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">KD-703LA 门窗探测器使用说明书</span> <img class="next" src="img/next.png" alt=""> </li> <li dataSrc = 'KD-805A_WiFi系统主机使用说明书_V1.00.pdf' onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">KD-805A WiFi系统主机使用说明书_V1.00</span> <img class="next" src="img/next.png" alt=""> </li> <li dataSrc = 'WIFI智慧家庭安防系统操作说明书_v0.01.pdf'onclick="fun(this)"> <img class="leftImg" src="img/js implements pdf online preview and printing (full version)" alt=""> <span href="">WIFI智慧家庭安防系统操作说明书_v0.01</span> <img class="next" src="img/next.png" alt=""> </li> </ul> <script src="js/jquery.min.js"></script> <script> function fun(e){ // console.log(e); var dataSrc = $(e).attr('dataSrc'); // console.log(dataSrc); // sessionStorage.setItem('dataSrc',dataSrc); // window.location.href='index.html' var urlSrc = 'http://testweixin.kingdun.net.cn/pdf/static/'+dataSrc; $.ajax({ url: urlSrc, type: "get", success: function(xhr, data){ if (navigator.userAgent.indexOf('Android') > -1) { //判断移动端是android 还是ios ,若是android 则要借助pdf插件 window.location.href = "http://testweixin.kingdun.net.cn/pdf/pdfjs/web/viewer.html?file="+urlSrc; } else { //ios直接打开pdf //window.location.href = url; window.location.href = "http://testweixin.kingdun.net.cn/pdf/pdfjs/web/viewer.html?file="+urlSrc; } }, error: function(){ //window.location.href = '${ctx}/core/user.androidPdf.do?mid='+mid+"&name="+storagename+"&realname="+realname; window.location.href = "http://testweixin.kingdun.net.cn/pdf/js/web/viewer.html?file="+urlSrc; } }); } </script> </body> </html>
[Compatibility]: Apple mobile phone: Preview directly, it comes with the mobile phone system, but it cannot be downloaded (there are gains and losses), it can be opened in other applications such as wps.
Android: can be previewed, can be downloaded, can be opened in the default browser of the mobile phone, can support downloading, I have Xiaomi 8, uc browser downloads garbled code, but Xiaomi's own browser can Download pdf file.
Crossover.
Easter egg: down.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-touch-fullscreen" content="yes"> <meta name="full-screen" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="format-detection" content="telephone=no"> <meta name="format-detection" content="address=no"> <title>list</title> <style> *{ margin: 0; padding: 0; } .title{ background: #e50041; color: #ffffff; font-size: 16px; } .title{ padding:10px 10px ; } ul{ padding:0px 10px 10px 10px ; } li{ list-style: none; border-bottom: 1px solid #eeeeee; height: 50px; line-height:50px; } a{ text-decoration: none; color: #000; } .leftImg{ width: 30px; vertical-align: middle; } .next{ float: right; /*vertical-align: middle;*/ margin-top: 4.5%; } </style> </head> <body> <p class="title">产品说明书</p> <a href="static/1.pdf">00001</a> </body> </html>
Haha, directly use a tag href to jump to the pdf file; it is also ok! [iphone: direct preview;;; Android: download link]
Related recommendations:
JS printing function The code can realize print preview, print settings, etc.
JavaScript can realize printing, print preview, print settings
The above is the detailed content of js implements pdf online preview and printing (full version). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
