Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
<script src="//" crossorigin="anonymous"></script>
<link href="//" rel="stylesheet" type="text/css" />
<style type="text/css">

#the-canvas {
  border: 1px solid black;
  direction: ltr;



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

  <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>

<canvas id="the-canvas"></canvas>
<div class="textLayer"></div>

// If absolute URL from the remote server is provided, configure the CORS
// header on that server.
var url = '//';

// 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 = '//';

var pdfDoc = null,
    pageNum = 1,
    pageRendering = false,
    pageNumPending = null,
    //scale = 0.8,
    scale = 2,
    canvas = document.getElementById('the-canvas'),
    textLayer = document.querySelector(".textLayer"),
    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;
    var displayWidth = 1; = `${(viewport.width * displayWidth) / scale}px`; = `${(viewport.height * displayWidth) / scale}px`; = `${(viewport.width * displayWidth) / scale}px`; = `${(viewport.height * displayWidth) / scale}px`;

    // 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
        pageNumPending = null;
    }).then(function() {
      // Returns a promise, on resolving it will return text contents of the page
      return page.getTextContent();
    }).then(function(textContent) {

      // Assign CSS to the textLayer elemen = canvas.offsetLeft + 'px'; = canvas.offsetTop + 'px'; = canvas.offsetHeight + 'px'; = canvas.offsetWidth + 'px';

      // Pass the data to the method for rendering of text over the pdf canvas.
        textContent: textContent,
        container: textLayer,
        viewport: viewport,
        textDivs: []

  // 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 {

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

 * Displays next page.
function onNextPage() {
  if (pageNum >= pdfDoc.numPages) {
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