pdf.js javascript实现pdf阅读器

https://github.com/mozilla/pdf.js     可在该网站下载到pdf.js

pdf.js 升级至 1.0 版本,并且将成为未来 Firefox 内嵌的 PDF 阅读器。

和 Google Chrome 使用的源自 Foxit 的闭源 PDF 浏览插件不同,PDF.js 是基于开放的 HTML5 及 JavaScript 技术实现的开源产品


<html>
<body>
  <div>
    <button id="prev" οnclick="goPrevious()">Previous</button>
    <button id="next" οnclick="goNext()">Next</button>
       
    <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
  </div>

  <div>
    <canvas id="the-canvas" style="border:1px solid black"></canvas>
  </div>

  <!-- Use latest PDF.js build from Github -->
  <script type="text/javascript" src="https://raw.github.com/mozilla/pdf.js/gh-pages/build/pdf.js"></script>
  
  <script type="text/javascript">
    //
    // NOTE: 
    // Modifying the URL below to another server will likely *NOT* work. Because of browser
    // security restrictions, we have to use a file server with special headers
    // (CORS) - most servers don't support cross-origin browser requests.
    //
    var url = 'http://cdn.mozilla.net/pdfjs/tracemonkey.pdf';

    //
    // Disable workers to avoid yet another cross-origin issue (workers need the URL of
    // the script to be loaded, and currently do not allow cross-origin scripts)
    //
    PDFJS.disableWorker = true;

    var pdfDoc = null,
        pageNum = 1,
        scale = 0.8,
        canvas = document.getElementById('the-canvas'),
        ctx = canvas.getContext('2d');

    //
    // Get page info from document, resize canvas accordingly, and render page
    //
    function renderPage(num) {
      // 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
        };
        page.render(renderContext);
      });

      // Update page counters
      document.getElementById('page_num').textContent = pageNum;
      document.getElementById('page_count').textContent = pdfDoc.numPages;
    }

    //
    // Go to previous page
    //
    function goPrevious() {
      if (pageNum <= 1)
        return;
      pageNum--;
      renderPage(pageNum);
    }

    //
    // Go to next page
    //
    function goNext() {
      if (pageNum >= pdfDoc.numPages)
        return;
      pageNum++;
      renderPage(pageNum);
    }

    //
    // Asynchronously download PDF as an ArrayBuffer
    //
    PDFJS.getDocument(url).then(function getPdfHelloWorld(_pdfDoc) {
      pdfDoc = _pdfDoc;
      renderPage(pageNum);
    });
  </script>  
</body>
</html>


所有需要导入的js文件如下:

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>PDF.js viewer</title>

<!--#if FIREFOX || MOZCENTRAL-->
<!--#include viewer-snippet-firefox-extension.html-->
<!--#endif-->

    <link rel="stylesheet" href="viewer.css"/>
<!--#if !PRODUCTION-->
    <link rel="resource" type="application/l10n" href="locale.properties"/>
<!--#endif-->

<!--#if !(FIREFOX || MOZCENTRAL)-->
    <script type="text/javascript" src="compatibility.js"></script>
<!--#endif-->

<!--#if !PRODUCTION-->
    <script type="text/javascript" src="../external/webL10n/l10n.js"></script>
<!--#endif-->

<!--#if !PRODUCTION-->
    <script type="text/javascript" src="../src/core.js"></script>
    <script type="text/javascript" src="../src/util.js"></script>
    <script type="text/javascript" src="../src/api.js"></script>
    <script type="text/javascript" src="../src/metadata.js"></script>
    <script type="text/javascript" src="../src/canvas.js"></script>
    <script type="text/javascript" src="../src/obj.js"></script>
    <script type="text/javascript" src="../src/function.js"></script>
    <script type="text/javascript" src="../src/charsets.js"></script>
    <script type="text/javascript" src="../src/cidmaps.js"></script>
    <script type="text/javascript" src="../src/colorspace.js"></script>
    <script type="text/javascript" src="../src/crypto.js"></script>
    <script type="text/javascript" src="../src/evaluator.js"></script>
    <script type="text/javascript" src="../src/fonts.js"></script>
    <script type="text/javascript" src="../src/glyphlist.js"></script>
    <script type="text/javascript" src="../src/image.js"></script>
    <script type="text/javascript" src="../src/metrics.js"></script>
    <script type="text/javascript" src="../src/parser.js"></script>
    <script type="text/javascript" src="../src/pattern.js"></script>
    <script type="text/javascript" src="../src/stream.js"></script>
    <script type="text/javascript" src="../src/worker.js"></script>
    <script type="text/javascript" src="../external/jpgjs/jpg.js"></script>
    <script type="text/javascript" src="../src/jpx.js"></script>
    <script type="text/javascript" src="../src/jbig2.js"></script>
    <script type="text/javascript" src="../src/bidi.js"></script>
    <script type="text/javascript">PDFJS.workerSrc = '../src/worker_loader.js';</script>
<!--#endif-->

<!--#if GENERIC || CHROME-->
<!--#include viewer-snippet.html-->
<!--#endif-->

<!--#if B2G-->
<!--#include viewer-snippet-b2g.html-->
<!--#endif-->

    <script type="text/javascript" src="debugger.js"></script>
    <script type="text/javascript" src="viewer.js"></script>
  </head>

http://mozilla.github.com/pdf.js/web/viewer.html      一个更加完整的例子如下:

<!DOCTYPE html>
<html dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>PDF.js viewer</title>


    <link rel="stylesheet" href="viewer.css"/>

    <script type="text/javascript" src="compatibility.js"></script>



<!-- This snippet is used in production, see Makefile -->
<link rel="resource" type="application/l10n" href="locale.properties"/>
<script type="text/javascript" src="l10n.js"></script>
<script type="text/javascript" src="../build/pdf.js"></script>
<script type="text/javascript">
  // This specifies the location of the pdf.js file.
  PDFJS.workerSrc = "../build/pdf.js";
</script>



    <script type="text/javascript" src="debugger.js"></script>
    <script type="text/javascript" src="viewer.js"></script>
  </head>

  <body>
    <div id="outerContainer">

      <div id="sidebarContainer">
        <div id="toolbarSidebar" class="splitToolbarButton toggled">
          <button id="viewThumbnail" class="toolbarButton group toggled" title="Show Thumbnails" οnclick="PDFView.switchSidebarView('thumbs')" tabindex="1" data-l10n-id="thumbs">
             <span data-l10n-id="thumbs_label">Thumbnails</span>
          </button>
          <button id="viewOutline" class="toolbarButton group" title="Show Document Outline" οnclick="PDFView.switchSidebarView('outline')" tabindex="2" data-l10n-id="outline">
             <span data-l10n-id="outline_label">Document Outline</span>
          </button>
          <button id="viewSearch" class="toolbarButton group hidden" title="Search Document" οnclick="PDFView.switchSidebarView('search')" tabindex="3" data-l10n-id="search_panel">
             <span data-l10n-id="search_panel_label">Search Document</span>
          </button>
        </div>
        <div id="sidebarContent">
          <div id="thumbnailView">
          </div>
          <div id="outlineView" class="hidden">
          </div>
          <div id="searchView" class="hidden">
            <div id="searchToolbar">
              <input id="searchTermsInput" class="toolbarField" οnkeydοwn='if (event.keyCode == 13) PDFView.search()'>
              <button id="searchButton" class="textButton toolbarButton" οnclick='PDFView.search()' data-l10n-id="search">Find</button>
            </div>
            <div id="searchResults"></div>
          </div>
        </div>
      </div>  <!-- sidebarContainer -->

      <div id="mainContainer">
        <div class="toolbar">
          <div id="toolbarContainer">

            <div id="toolbarViewer">
              <div id="toolbarViewerLeft">
                <button id="sidebarToggle" class="toolbarButton" title="Toggle Sidebar" tabindex="4" data-l10n-id="toggle_slider">
                  <span data-l10n-id="toggle_slider_label">Toggle Sidebar</span>
                </button>
                <div class="toolbarButtonSpacer"></div>
                <div class="splitToolbarButton">
                  <button class="toolbarButton pageUp" title="Previous Page" οnclick="PDFView.page--" id="previous" tabindex="5" data-l10n-id="previous">
                    <span data-l10n-id="previous_label">Previous</span>
                  </button>
                  <div class="splitToolbarButtonSeparator"></div>
                  <button class="toolbarButton pageDown" title="Next Page" οnclick="PDFView.page++" id="next" tabindex="6" data-l10n-id="next">
                    <span data-l10n-id="next_label">Next</span>
                  </button>
                </div>
                <label id="pageNumberLabel" class="toolbarLabel" for="pageNumber" data-l10n-id="page_label">Page: </label>
                <input type="number" id="pageNumber" class="toolbarField pageNumber" οnchange="PDFView.page = this.value;" value="1" size="4" min="1" tabindex="7">
                </input>
                <span id="numPages" class="toolbarLabel"></span>
              </div>
              <div id="toolbarViewerRight">
                <input id="fileInput" class="fileInput" type="file" οncοntextmenu="return false;" style="visibility: hidden; position: fixed; right: 0; top: 0" />


                <button id="fullscreen" class="toolbarButton fullscreen" title="Fullscreen" tabindex="11" data-l10n-id="fullscreen" οnclick="PDFView.fullscreen();">
                  <span data-l10n-id="fullscreen_label">Fullscreen</span>
                </button>

                <button id="openFile" class="toolbarButton openFile" title="Open File" tabindex="12" data-l10n-id="open_file" οnclick="document.getElementById('fileInput').click()">
                   <span data-l10n-id="open_file_label">Open</span>
                </button>

                <button id="print" class="toolbarButton print" title="Print" tabindex="13" data-l10n-id="print" οnclick="window.print()">
                  <span data-l10n-id="print_label">Print</span>
                </button>

                <button id="download" class="toolbarButton download" title="Download" οnclick="PDFView.download();" tabindex="14" data-l10n-id="download">
                  <span data-l10n-id="download_label">Download</span>
                </button>
                <!-- <div class="toolbarButtonSpacer"></div> -->
                <a href="#" id="viewBookmark" class="toolbarButton bookmark" title="Current view (copy or open in new window)" tabindex="15" data-l10n-id="bookmark"><span data-l10n-id="bookmark_label">Current View</span></a>
              </div>
              <div class="outerCenter">
                <div class="innerCenter" id="toolbarViewerMiddle">
                  <div class="splitToolbarButton">
                    <button class="toolbarButton zoomOut" title="Zoom Out" οnclick="PDFView.zoomOut();" tabindex="8" data-l10n-id="zoom_out">
                      <span data-l10n-id="zoom_out_label">Zoom Out</span>
                    </button>
                    <div class="splitToolbarButtonSeparator"></div>
                    <button class="toolbarButton zoomIn" title="Zoom In" οnclick="PDFView.zoomIn();" tabindex="9" data-l10n-id="zoom_in">
                      <span data-l10n-id="zoom_in_label">Zoom In</span>
                     </button>
                  </div>
                  <span id="scaleSelectContainer" class="dropdownToolbarButton">
                     <select id="scaleSelect" οnchange="PDFView.parseScale(this.value);" title="Zoom" οncοntextmenu="return false;" tabindex="10" data-l10n-id="zoom">
                      <option id="pageAutoOption" value="auto" selected="selected" data-l10n-id="page_scale_auto">Automatic Zoom</option>
                      <option id="pageActualOption" value="page-actual" data-l10n-id="page_scale_actual">Actual Size</option>
                      <option id="pageFitOption" value="page-fit" data-l10n-id="page_scale_fit">Fit Page</option>
                      <option id="pageWidthOption" value="page-width" data-l10n-id="page_scale_width">Full Width</option>
                      <option id="customScaleOption" value="custom"></option>
                      <option value="0.5">50%</option>
                      <option value="0.75">75%</option>
                      <option value="1">100%</option>
                      <option value="1.25">125%</option>
                      <option value="1.5">150%</option>
                      <option value="2">200%</option>
                    </select>
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div id="viewerContainer">
          <div id="viewer"></div>
        </div>

        <div id="loadingBox">
          <div id="loading"></div>
          <div id="loadingBar"><div class="progress"></div></div>
        </div>

        <div id="errorWrapper" hidden='true'>
          <div id="errorMessageLeft">
            <span id="errorMessage"></span>
            <button id="errorShowMore" οnclick="" οncοntextmenu="return false;" data-l10n-id="error_more_info">
              More Information
            </button>
            <button id="errorShowLess" οnclick="" οncοntextmenu="return false;" data-l10n-id="error_less_info" hidden='true'>
              Less Information
            </button>
          </div>
          <div id="errorMessageRight">
            <button id="errorClose" οncοntextmenu="return false;" data-l10n-id="error_close">
              Close
            </button>
          </div>
          <div class="clearBoth"></div>
          <textarea id="errorMoreInfo" hidden='true' readonly="readonly"></textarea>
        </div>
      </div> <!-- mainContainer -->

    </div> <!-- outerContainer -->
    <div id="printContainer"></div>
  </body>
</html>



已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页