码迷,mamicode.com
首页 > Web开发 > 详细

pdf转为html查看pdf.js

时间:2018-07-04 18:47:42      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:nbsp   options   1.5   cdn   specified   mis   canvas   ISE   height   

  

  

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script src="//mozilla.github.io/pdf.js/build/pdf.js"></script>

    <h1>PDF.js ‘Hello, world!‘ example</h1>

    <canvas id="the-canvas"></canvas>
</body>
<script>
    var url = ‘//cdn.mozilla.net/pdfjs/helloworld.pdf‘;

    // Loaded via <script> tag, create shortcut to access PDF.js exports.
    var pdfjsLib = window[‘pdfjs-dist/build/pdf‘];
    // The workerSrc property shall be specified.
    pdfjsLib.GlobalWorkerOptions.workerSrc = ‘//mozilla.github.io/pdf.js/build/pdf.worker.js‘;

    // Asynchronous download of PDF
    var loadingTask = pdfjsLib.getDocument(url);
    loadingTask.promise.then(function (pdf) {
        console.log(‘PDF loaded‘);

        // Fetch the first page
        var pageNumber = 1;
        pdf.getPage(pageNumber).then(function (page) {
            console.log(‘Page loaded‘);

            var scale = 1.5;
            var viewport = page.getViewport(scale);

            // Prepare canvas using PDF page dimensions
            var canvas = document.getElementById(‘the-canvas‘);
            var context = canvas.getContext(‘2d‘);
            canvas.height = viewport.height;
            canvas.width = viewport.width;

            // Render PDF page into canvas context
            var renderContext = {
                canvasContext: context,
                viewport: viewport
            };
            var renderTask = page.render(renderContext);
            renderTask.then(function () {
                console.log(‘Page rendered‘);
            });
        });
    }, function (reason) {
        // PDF loading error
        console.error(reason);
    });
</script>

</html>

  官网的demo是个html实现的pdf页面http://mozilla.github.io/pdf.js/web/viewer.html  也可以学习参考

  参考http://mozilla.github.io/pdf.js/

pdf转为html查看pdf.js

标签:nbsp   options   1.5   cdn   specified   mis   canvas   ISE   height   

原文地址:https://www.cnblogs.com/yiyi17/p/9264495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!