It appears that my issue was do to me not adding the link resource for locale.properties in my HTML. Turns out the first error I was getting was more of a red herring, and was able to solve this issue by focusing on the second error.

I added this:

<link rel="resource" type="application/l10n" href="[your path to]/locale/locale.properties">

Where all the HTML for the viewer is located and it loaded right up.

Answer from scapegoat17 on Stack Overflow
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ pdfjs โ€บ wagvs9Lf
PDF.js Previous/Next example - JSFiddle - Code Playground
The Code Completion will now also have the context of all panels before suggesting code to you - so if for example you have some CSS or JS, the HTML panel will suggest code based on the other two panels.
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ pdfjs โ€บ 9engc9mw
PDF.js 'Hello, world!' example - JSFiddle - Code Playground
The Code Completion will now also have the context of all panels before suggesting code to you - so if for example you have some CSS or JS, the HTML panel will suggest code based on the other two panels.
๐ŸŒ
Mozilla
mozilla.github.io โ€บ pdf.js โ€บ examples
PDF.js - Examples
This tutorial shows how PDF.js can be used as a library in a web browser.
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ 989zcpvf
PdfViewer - JSFiddle - Code Playground
JS Modules: import [name] from '@jsfiddle/[username]/[fiddle].js'
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ rishit_epari โ€บ uopyhe5L โ€บ 5
pdf.js iframe - JSFiddle - Code Playground
The Code Completion will now also have the context of all panels before suggesting code to you - so if for example you have some CSS or JS, the HTML panel will suggest code based on the other two panels.
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ Jeffreyvdh โ€บ atgtrj69
pdf.js - JSFiddle - Code Playground
JS Modules: import [name] from '@jsfiddle/[username]/[fiddle].js'
Find elsewhere
๐ŸŒ
PDF.js Express
pdfjs.express โ€บ samples
JavaScript PDF Viewer Demo & Samples
Shows how to call WebViewer constructor to instantiate and load document. You can load local/remote files of your choice
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ pdfjs โ€บ cq0asLqz
PDF.js 'Hello, base64!' example - JSFiddle - Code Playground
The Code Completion will now also have the context of all panels before suggesting code to you - so if for example you have some CSS or JS, the HTML panel will suggest code based on the other two panels.
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ vivin โ€บ RjqUf
Minimal text-selection example for pdf.js - JSFiddle - Code Playground
Console is avaialble for all users during beta, after that it'll be available for JSFiddle supporters.
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ avibodha โ€บ tKbAS
test 1 - pdf.js - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ seikichi โ€บ RuDvz โ€บ 2
Annotation example for PDF.js - JSFiddle - Code Playground
The Code Completion will now also have the context of all panels before suggesting code to you - so if for example you have some CSS or JS, the HTML panel will suggest code based on the other two panels.
๐ŸŒ
Mozilla
mozilla.github.io โ€บ pdf.js โ€บ web โ€บ viewer.html
PDF.js viewer
A general-purpose, web standards-based platform for parsing and rendering PDFs.
Top answer
1 of 2
51

There is documentation available on their github readme. They cite the following example code:

/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

//
// See README for overview
//

'use strict';

//
// Fetch the PDF document from the URL using promises
//
PDFJS.getDocument('helloworld.pdf').then(function(pdf) {
  // Using promise to fetch the page
  pdf.getPage(1).then(function(page) {
    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
    };
    page.render(renderContext);
  });
});

Code below might be more accurate regarding https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples

pdfjsLib.GlobalWorkerOptions.workerSrc = '/js/pdf.worker.js';

pdfjsLib.getDocument('helloworld.pdf')
    .promise
    .then(pdf => {
      pdf.getPage(1).then(page => {
        let outputScale = window.devicePixelRatio || 1;
        let transform = outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] : null;
        let scale = 1.5;
        let viewport = page.getViewport({scale});

        let canvas = document.getElementById('the-canvas');
        let context = canvas.getContext('2d');

        canvas.width = Math.floor(viewport.width * outputScale);
        canvas.height = Math.floor(viewport.height * outputScale);
        canvas.style.width = Math.floor(viewport.width) + 'px';
        canvas.style.height =  Math.floor(viewport.height) + 'px';

        let renderContext = {
          canvasContext: context,
          transform,
          viewport,
        };

        page.render(renderContext);
      });
    })
    .catch(console.error);
2 of 2
33

Try Google'ing pdf.js documentation

/* create the PDF document */

var doc = new pdf();
doc.text(20, 20, 'hello, I am PDF.');
doc.text(20, 30, 'i was created in the browser using javascript.');
doc.text(20, 40, 'i can also be created from node.js');

/* Optional - set properties on the document */
doc.setProperties({
  title: 'A sample document created by pdf.js',
  subject: 'PDFs are kinda cool, i guess',        
  author: 'Marak Squires',
  keywords: 'pdf.js, javascript, Marak, Marak Squires',
  creator: 'pdf.js'
});

doc.addPage();
doc.setFontSize(22);
doc.text(20, 20, 'This is a title');
doc.setFontSize(16); 
doc.text(20, 30, 'This is some normal sized text underneath.');

var fileName = "testFile"+new Date().getSeconds()+".pdf";
var pdfAsDataURI = doc.output('datauri', {"fileName":fileName});

NOTE: the "pdf.js" project mentioned here is https://github.com/Marak/pdf.js, and has been deprecated since this answer was posted. @Treffynnon's answer is about the still-active Mozilla project (https://github.com/mozilla/pdf.js) that most searchers will be looking for.

๐ŸŒ
Anvil
anvil.works โ€บ show and tell
Custom PDF Viewer using PDF.js - Show and Tell - Anvil Community Forum
April 3, 2023 - Custom PDF Viewer using PDF.js Hey guys, I spent a lot of time trying to figure out how to embed a custom pdf viewer in anvil. So I built a custom pdf viewer using PDF.js and feel free to use it. This is a custom PDF viewer built using the PDF.js library. As of the latest update, the project ...
๐ŸŒ
Viewerjs
viewerjs.org โ€บ getit
ViewerJS Get ViewerJS
The heavy lifting in ViewerJS is done by these awesome projects: PDF.js is a library created by Andreas Gal and others at Mozilla Labs. It is an HTML5 technology experiment that explores building a faithful and efficient Portable Document Format (PDF) renderer without native code assistance.