search
HomeWeb Front-endJS Tutorialjs 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].

js implements pdf online preview and printing (full version)

2. Implementation result:

js implements pdf online preview and printing (full version)

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

js implements pdf online preview and printing (full version)

git clone or download.

The file looks like this after downloading:

js implements pdf online preview and printing (full version)

[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:

js implements pdf online preview and printing (full version)

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 = &#39;KD-122LA火灾探测报警器说明书.pdf&#39; 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 = &#39;KD-212LA可燃气体探测器说明书.pdf&#39; 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 = &#39;KD-216LA可燃气体探测器说明书.pdf&#39; 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 = &#39;Kd-122LA_KD-601系统遥控器使用说明.pdf&#39; 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 = &#39;KD-602LA_SOS一键救助使用说明书.pdf&#39; 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 = &#39;KD-701LA_溢水探测器使用说明书.pdf&#39; 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 = &#39;KD-702LA红外人体移动探测器说明书.pdf&#39; 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 = &#39;KD-703LA_门窗探测器使用说明书.pdf&#39; 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 = &#39;KD-805A_WiFi系统主机使用说明书_V1.00.pdf&#39; 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 = &#39;WIFI智慧家庭安防系统操作说明书_v0.01.pdf&#39;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(&#39;dataSrc&#39;);
     //    console.log(dataSrc);
     //    sessionStorage.setItem(&#39;dataSrc&#39;,dataSrc);
     //    window.location.href=&#39;index.html&#39;
        var urlSrc =  &#39;http://testweixin.kingdun.net.cn/pdf/static/&#39;+dataSrc;
        $.ajax({
            url: urlSrc,
            type: "get",
            success: function(xhr, data){
                if (navigator.userAgent.indexOf(&#39;Android&#39;) > -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 = &#39;${ctx}/core/user.androidPdf.do?mid=&#39;+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]

js implements pdf online preview and printing (full version)

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Replace String Characters in JavaScriptReplace String Characters in JavaScriptMar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup TutorialCustom Google Search API Setup TutorialMar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Build Your Own AJAX Web ApplicationsBuild Your Own AJAX Web ApplicationsMar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Example Colors JSON FileExample Colors JSON FileMar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

8 Stunning jQuery Page Layout Plugins8 Stunning jQuery Page Layout PluginsMar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

What is 'this' in JavaScript?What is 'this' in JavaScript?Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

Improve Your jQuery Knowledge with the Source ViewerImprove Your jQuery Knowledge with the Source ViewerMar 05, 2025 am 12:54 AM

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

10 Mobile Cheat Sheets for Mobile Development10 Mobile Cheat Sheets for Mobile DevelopmentMar 05, 2025 am 12:43 AM

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)