h5实现移动端预览pdf文件

时间:2024-04-02 14:09:49

手机端预览显示pdf文件

需求:后台传递到前端页面的pdf文件路径、pdf.js文件、pdf.worker.js文件。这两个插件网上资源挺多,自行查找。
以下是实现预览功能的jsp页面的代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<%-- 具体js插件和css样式根据需求自行引入 --%>
    <link rel="stylesheet" href="../css/font-awesome.min.css"/>
    <link rel="stylesheet" href="../css/bootstrap.min.css"/>
    <script src=" ../jquery.min.js"></script>
    <script src="../pdf.js"></script>
    <script src="../pdf.worker.js"></script>
    
    <style type="text/css">
        .close-pdf{position:fixed; top:20px; right:20px; color:#1d69b3; }
        button{width:40px; border:none; background: none; color:#1d69b3; font-size: 20px; outline: none;}
        .control{
            position: fixed;
            bottom: 20px;
            left: 50%;
            margin-left: -140px;
        }
        .control span{color:#1d69b3; font-size: 16px; line-height: 30px;}
    </style>
</head>

<body>
<a href="javascript:;" onclick="history.go(-1)" class="close-pdf"> <i class="glyphicon glyphicon-remove"></i></a>
<%--  i标签中的class引入的是图片,作用返回上一级--%>
<div class="">
    <canvas id="the-canvas" style="width: 100%;height: 95%;"></canvas>
</div>
<div class="control">
    <button id="first"><i class="fa fa-angle-double-left"></i></button>
    <%--  i标签中的class引入的是图片:表示首页。一下i标签中内容同此--%>
    &nbsp; &nbsp;
    <button id="prev"><i class="fa fa-angle-left"></i></button>
    &nbsp; &nbsp;
    <span><span id="page_num"></span> / <span id="page_count"></span></span>
    &nbsp; &nbsp;
    <button id="next"><i class="fa fa-angle-right"></i></button>
    &nbsp; &nbsp;
    <button id="final"><i class="fa fa-angle-double-right"></i></button>
</div>


<script>
   
    var url = '${url}';//用户可访问到的pdf文件的路径

    var pdfDoc = null,
            pageNum = 1,
            pageRendering = false,
            pageNumPending = null,
            scale = 2.0,   //调节预览文件的清晰度
            canvas = document.getElementById('the-canvas'),
            ctx = canvas.getContext('2d');

    /**
     * Get page info from document, resize canvas accordingly, and render page.
     * @param num Page number.
     */
    function renderPage(num) {
        pageRendering = true;
        // Using promise to fetch the page
        pdfDoc.getPage(num).then(function(page) {
            var viewport = page.getViewport(scale);
            canvas.height = viewport.height;
            canvas.width = viewport.width;

            // Render PDF page into canvas context
            var renderContext = {
                canvasContext: ctx,
                viewport: viewport
            };
            var renderTask = page.render(renderContext);

            // Wait for rendering to finish
            renderTask.promise.then(function() {
                pageRendering = false;
                if (pageNumPending !== null) {
                    // New page rendering is pending
                    renderPage(pageNumPending);
                    pageNumPending = null;
                }
            });
        });

        // Update page counters
        document.getElementById('page_num').textContent = num;
    }

    /**
     * If another page rendering in progress, waits until the rendering is
     * finised. Otherwise, executes rendering immediately.
     */
    function queueRenderPage(num) {
        if (pageRendering) {
            pageNumPending = num;
        } else {
            renderPage(num);
        }
    }

    /**
     * Displays previous page.
     */
    function onPrevPage() {
        if (pageNum <= 1) {
            return;
        }
        pageNum--;
        queueRenderPage(pageNum);
    }
    document.getElementById('prev').addEventListener('click', onPrevPage);

    /**
     * Displays next page.
     */
    function onNextPage() {
        if (pageNum >= pdfDoc.numPages) {
            return;
        }
        pageNum++;
        queueRenderPage(pageNum);
    }
    document.getElementById('next').addEventListener('click', onNextPage);

    /**
     * Displays first page.
     */
    function onFirstPage() {
        if (pageNum <= 1) {
            return;
        }
        pageNum=1;
        queueRenderPage(pageNum);
    }
    document.getElementById('first').addEventListener('click', onFirstPage);

    /**
     * Displays final page.
     */
    function onFinalPage() {
        if (pageNum >= pdfDoc.numPages) {
            return;
        }
        pageNum=pdfDoc.numPages;
        queueRenderPage(pageNum);
    }
    document.getElementById('final').addEventListener('click', onFinalPage);

    /**
     * Asynchronously downloads PDF.
     */
    PDFJS.getDocument(url).then(function(pdfDoc_) {
        pdfDoc = pdfDoc_;
        document.getElementById('page_count').textContent = pdfDoc.numPages;

        // Initial/first page rendering
        renderPage(pageNum);
    });
</script>
</body>
</html>

在保证文件路径正确的情况下:如果文件路径包含中文时无法预览可以查看tomcat中的conf目录下的server.xml文件是否在

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>

中配置了 URIEncoding=“UTF-8” 。

查看页面大致样式如下图:
h5实现移动端预览pdf文件
需要增加其他功能可以查看官方文档,本文中不包括左右滑动翻页功能。