h5预览pdf

2022-12-07  本文已影响0人  drneilc

背景

在h5中预览pdf,原做法是直接location.href,但是内嵌网页在ios和android上的表现出入太大,因此选择使用组件实现,经过多次试错,最终选择了pdfh5官方文档

直接上代码

pdfview组件内容

<template lang="pug">
.pdf-container 
    #pdf
</template>

<script lang="ts">
    import { defineComponent, onMounted } from 'vue';
    import { Toast } from 'vant';

    export default defineComponent({
        props: {
            pdfurl: {
                type: String,
                default: 'https://swsdl.vivo.com.cn/appstore/cloudtest/pdf/20220621/202206212026012891400036.pdf'
            }
        },
        setup(props) {
            const initPdf = () => {
                console.log('init pdf begain,  pdf url===', props.pdfurl);
                if (!props.pdfurl || props.pdfurl === 'null') {
                    Toast('报告文件不存在');
                    return;
                }

                // @ts-ignore
                new Pdfh5('#pdf', {
                    pdfurl: props.pdfurl
                });
            };

            onMounted(() => {
                initPdf();
            });
        }
    });
</script>

<style>
    .pdfjs {
        width: 100%;
        height: 100%;
        overflow: hidden;
        background: #fff;
        position: relative;
    }

    .pdfjs .viewerContainer {
        position: relative;
        width: 100%;
        height: 100%;
        overflow: auto;
        -webkit-overflow-scrolling: touch;
        transition: all 0.3s;
    }

    .pdfjs .pdfViewer {
        position: relative;
        top: 0;
        left: 0;
        padding: 10px 8px;
    }

    .pdfjs .pdfViewer .pageContainer {
        width: 100%;
        margin: 0px auto 8px auto;
        position: relative;
        overflow: visible;
        -webkit-box-shadow: darkgrey 0px 1px 3px 0px;
        -moz-box-shadow: darkgrey 0px 1px 3px 0px;
        box-shadow: darkgrey 0px 1px 3px 0px;
        background-color: white;
        box-sizing: border-box;
    }

    .pdfjs .pdfViewer .pageContainer img {
        width: 100%;
        height: 100%;
        position: relative;
        z-index: 100;
        /* user-select:none; */
    }

    .pdfjs .pageNum {
        padding: 0px 7px;
        height: 26px;
        position: absolute;
        top: 20px;
        left: 15px;
        z-index: 997;
        border-radius: 8px;
        transition: all 0.3s;
        display: none;
    }

    .pdfjs .pageNum-bg,
    .pdfjs .pageNum-num {
        width: 100%;
        height: 100%;
        line-height: 26px;
        text-align: center;
        position: absolute;
        top: 0px;
        left: 0px;
        color: #fff;
        border-radius: 8px;
        font-size: 16px;
    }

    .pdfjs .pageNum-bg {
        background: rgba(0, 0, 0, 0.5);
    }

    .pdfjs .pageNum-num {
        position: relative;
    }

    .pdfjs .pageNum span {
        color: #fff;
        font-size: 16px;
    }

    .pdfjs .loadingBar {
        position: absolute;
        width: 100%;
        z-index: 99;
        background: #fff !important;
        height: 4px;
        top: 0px;
        left: 0px;
        transition: all 0.3s;
    }

    .pdfjs .loadingBar .progress {
        background: #fff !important;
        position: absolute;
        top: 0;
        left: 0;
        width: 0%;
        height: 100%;
        overflow: hidden;
        transition: width 200ms;
    }

    .pdfjs .loadingBar .progress .glimmer {
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: calc(100% + 150px);
        background: #7bcf34;
    }

    .pdfjs .backTop {
        width: 50px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        position: absolute;
        bottom: 90px;
        right: 15px;
        font-size: 18px;
        z-index: 999;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0.4)
            url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMAAAsSAAALEgHS3X78AAAA+klEQVRYw+2WUQ2DMBCG2TIBSJiESkACEpCAg83BcLBJmIQ5gClgDpiDby9tciGkoaUtZOESXuhdv7+X/pdm2dYC6IgX7Zh3THy+w9oN/rMASqBcE26iSA1XwCAEDIBKBc8F/KE/gB7IU8BbDXyJf2Z2tFFFAE8N6iRIi/jotXssuGn1FzhPrCu9BtCEhlcCrix5hbiYVSh46bKpELvcniO71Q51zWJ7ju3mUe9vzym7eR7Az57CbohTXBzAt9GknG9PoLY8KK4z6htLfeXTTXMZAfoZuWYWKC+YZWMAQuWZSP0k2wXsAnYB2xNwci1wGTKhO/COlLtu/ABVfTFsxwwYRgAAAABJRU5ErkJggg==)
            no-repeat center;
        background-size: 50% 50%;
        transition: all 0.3s;
        display: none;
    }

    .pdfjs .loadEffect {
        width: 100px;
        height: 100px;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -50px;
        margin-left: -50px;
        z-index: 99;
        background: url(data:image/gif;base64,R0lGODlhgACAAKIAAP///93d3bu7u5mZmQAA/wAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFBQAEACwCAAIAfAB8AAAD/0i63P4wygYqmDjrzbtflvWNZGliYXiubKuloivPLlzReD7al+7/Eh5wSFQIi8hHYBkwHUmD6CD5YTJLz49USuVYraRsZ7vtar7XnQ1Kjpoz6LRHvGlz35O4nEPP2O94EnpNc2sef1OBGIOFMId/inB6jSmPdpGScR19EoiYmZobnBCIiZ95k6KGGp6ni4wvqxilrqBfqo6skLW2YBmjDa28r6Eosp27w8Rov8ekycqoqUHODrTRvXsQwArC2NLF29UM19/LtxO5yJd4Au4CK7DUNxPebG4e7+8n8iv2WmQ66BtoYpo/dvfacBjIkITBE9DGlMvAsOIIZjIUAixliv9ixYZVtLUos5GjwI8gzc3iCGghypQqrbFsme8lwZgLZtIcYfNmTJ34WPTUZw5oRxdD9w0z6iOpO15MgTh1BTTJUKos39jE+o/KS64IFVmsFfYT0aU7capdy7at27dw48qdS7eu3bt480I02vUbX2F/JxYNDImw4GiGE/P9qbhxVpWOI/eFKtlNZbWXuzlmG1mv58+gQ4seTbq06dOoU6vGQZJy0FNlMcV+czhQ7SQmYd8eMhPs5BxVdfcGEtV3buDBXQ+fURxx8oM6MT9P+Fh6dOrH2zavc13u9JXVJb520Vp8dvC76wXMuN5Sepm/1WtkEZHDefnzR9Qvsd9+/wi8+en3X0ntYVcSdAE+UN4zs7ln24CaLagghIxBaGF8kFGoIYV+Ybghh841GIyI5ICIFoklJsigihmimJOLEbLYIYwxSgigiZ+8l2K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rDcTBo8v5fCZki6vCW33Oq4+0832O/at3+f7fICBdzsChgJGeoWHhkV0P4yMRG1BkYeOeECWl5hXQ5uNIAOjA1KgiKKko1CnqBmqqk+nIbCkTq20taVNs7m1vKAnurtLvb6wTMbHsUq4wrrFwSzDzcrLtknW16tI2tvERt6pv0fi48jh5h/U6Zs77EXSN/BE8jP09ZFA+PmhP/xvJgAMSGBgQINvEK5ReIZhQ3QEMTBLAAAh+QQFBQAEACwCAB8AMABXAAAD50i6DA4syklre87qTbHn4OaNYSmNqKmiqVqyrcvBsazRpH3jmC7yD98OCBF2iEXjBKmsAJsWHDQKmw571l8my+16v+CweEwum8+hgHrNbrvbtrd8znbR73MVfg838f8BeoB7doN0cYZvaIuMjY6PkJGSk2gClgJml5pjmp2YYJ6dX6GeXaShWaeoVqqlU62ir7CXqbOWrLafsrNctjIDwAMWvC7BwRWtNsbGFKc+y8fNsTrQ0dK3QtXAYtrCYd3eYN3c49/a5NVj5eLn5u3s6e7x8NDo9fbL+Mzy9/T5+tvUzdN3Zp+GBAAh+QQJBQAEACwCAAIAfAB8AAAD/0i63P4wykmrvTjrzbv/YCiOZGmeaKqubOu+cCzPdArcQK2TOL7/nl4PSMwIfcUk5YhUOh3M5nNKiOaoWCuWqt1Ou16l9RpOgsvEMdocXbOZ7nQ7DjzTaeq7zq6P5fszfIASAYUBIYKDDoaGIImKC4ySH3OQEJKYHZWWi5iZG0ecEZ6eHEOio6SfqCaqpaytrpOwJLKztCO2jLi1uoW8Ir6/wCHCxMG2x7muysukzb230M6H09bX2Nna29zd3t/g4cAC5OXm5+jn3Ons7eba7vHt2fL16tj2+QL0+vXw/e7WAUwnrqDBgwgTKlzIsKHDh2gGSBwAccHEixAvaqTYcFCjRoYeNyoM6REhyZIHT4o0qPIjy5YTTcKUmHImx5cwE85cmJPnSYckK66sSAAj0aNIkypdyrSp06dQo0qdSrWq1atYs2rdyrWr169gwxZJAAA7)
            no-repeat center;
        background-size: 30% 30%;
        transition: all 0.3s;
    }
    .pdfjs .pdfViewer .pageContainer img.pdfLogo {
        /* user-select:none; */
        position: absolute;
        z-index: 101;
    }
    .pdfjs .textLayer {
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        overflow: hidden;
        opacity: 0.2;
        line-height: 1;
        z-index: 101;
    }

    .pdfjs .textLayer > span {
        color: transparent;
        position: absolute;
        white-space: pre;
        cursor: text;
        transform-origin: 0% 0%;
        transform: translate(-99px, -80px) scaleX(1) !important;
    }

    .pdfjs .textLayer .highlight {
        margin: -1px;
        padding: 1px;
        background-color: rgba(180, 0, 170, 1);
        border-radius: 4px;
    }

    .pdfjs .textLayer .highlight.begin {
        border-radius: 4px 0px 0px 4px;
    }

    .pdfjs .textLayer .highlight.end {
        border-radius: 0px 4px 4px 0px;
    }

    .pdfjs .textLayer .highlight.middle {
        border-radius: 0px;
    }

    .pdfjs .textLayer .highlight.selected {
        background-color: rgba(0, 100, 0, 1);
    }

    .pdfjs .textLayer ::selection {
        background: rgba(0, 0, 255, 1);
    }

    .pdfjs .textLayer .endOfContent {
        display: block;
        position: absolute;
        left: 0px;
        top: 100%;
        right: 0px;
        bottom: 0px;
        z-index: -1;
        cursor: default;
        /* user-select: none; */
    }

    .pdfjs .textLayer .endOfContent.active {
        top: 0px;
    }
</style>

<style lang="less" scoped>
    .pdf-container {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
</style>

index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta http-equiv="Expires" content="0" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Cache-control" content="no-cache" />
        <meta http-equiv="Cache" content="no-cache" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, viewport-fit=cover" />
        <title>报告详情</title>
        <link rel="icon" href="/favicon.ico" />

        <script src="/pdf.js" type="text/javascript" charset="utf-8"></script>
        <script src="/pdf.worker.js" type="text/javascript" charset="utf-8"></script>
        <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
        <script src="/pdfh5.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        <div id="app"></div>
        <script type="module" src="./main.ts"></script>
    </body>
</html>

页面调用就很简单了

<template lang="pug">
div
    PdfViewer(:pdfurl="pdfurl")
</template>

结果

完美实现兼容,目前还未发现问题。

注意事项

详细文档和需要下载的文件请点击最上方的官方文档,这里仅做接入示例参考。
在index.html里引入,主要是因为文件过大,打包的时候会报警。
我的项目使用的是vite+vue3,如果用vue2的话记得注意区分。

上一篇下一篇

猜你喜欢

热点阅读