[플러그인] PDF 뷰어 PDF.js > 정보공유

본문 바로가기

정보공유

일반글
JavaScript

[플러그인] PDF 뷰어 PDF.js

페이지 정보

게시물QR코드

본문

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


<h1>PDF.js Previous/Next example</h1>

aac8893d3737ddb7953bcb612a11c2cc_1557889013_03.png
 


<div>

  <button id="prev">Previous</button>

  <button id="next">Next</button>

  &nbsp; &nbsp;

  <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>

</div>


<canvas id="the-canvas"></canvas>


<script>

// If absolute URL from the remote server is provided, configure the CORS

// header on that server.

var url = 'https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.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';


var pdfDoc = null,

    pageNum = 1,

    pageRendering = false,

    pageNumPending = null,

    scale = 0.8,

    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: 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);


/**

 * Asynchronously downloads PDF.

 */

pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {

  pdfDoc = pdfDoc_;

  document.getElementById('page_count').textContent = pdfDoc.numPages;


  // Initial/first page rendering

  renderPage(pageNum);

});

</script>

댓글목록

등록된 댓글이 없습니다.

  • Addr.부산광역시 동구 중앙대로 319, 9층 L4호(초량동, 부산YMCA빌딩)
  • BR. 625-68-00172
  • TRC. 2019-부산해운대-1186
  • TEL. 0507-1382-2790
  • Email. gnuwiz@naver.com
All rights reserved.