You have to set GlobalWorkerOptions.workerSrc to /build/pdf.worker(.min).js of same version:

pdfjsLib.GlobalWorkerOptions.workerSrc =
  "https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.worker.min.js";  

pdfjsLib.getDocument('./ahmed.pdf').promise.then(doc => {
  console.log(`This document has ${doc._pdfInfo.numPages} pages.");
});

And, as @Pasi has mentioned, you have to promisify .getDocument() by chaining .promise on it. Without it, there is no .then().

Answer from tao on Stack Overflow
🌐
cdnjs
cdnjs.com › home › libraries › pdf.js › 2.6.347
pdf.js - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers
https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js · https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf_viewer.css · https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf_viewer.js · https://cdnjs.cloudfla...
🌐
jsDelivr
jsdelivr.com › package › npm › pdf.worker
pdf.worker CDN by jsDelivr - A CDN for npm and GitHub
October 10, 2023 - A free, fast, and reliable CDN for pdf.worker. This is a required file for pdf js to work
Published   Oct 10, 2023
🌐
GitHub
github.com › mozilla › pdf.js
GitHub - mozilla/pdf.js: PDF Reader in JavaScript
This will generate pdf.js and pdf.worker.js in the build/generic/build/ directory (respectively build/generic-legacy/build/). Both scripts are needed but only pdf.js needs to be included since pdf.worker.js will be loaded by pdf.js. The PDF.js files are large and should be minified for production. To use PDF.js in a web application you can choose to use a pre-built version of the library or to build it from source. We supply pre-built versions for usage with NPM under the pdfjs-dist name. For more information and examples please refer to the wiki page on this subject. PDF.js is hosted on several free CDNs: https://www.jsdelivr.com/package/npm/pdfjs-dist ·
Starred by 52.4K users
Forked by 10.5K users
Languages   JavaScript 71.3% | Fluent 25.5% | CSS 2.2%
🌐
Mozilla
mozilla.github.io › pdf.js › getting_started
PDF.js - Getting Started
$ git clone https://github.com/mozilla/pdf.js.git $ cd pdf.js ... Note that we only mention the most relevant files and folders. ├── build/ │ ├── pdf.mjs - display layer │ ├── pdf.mjs.map - display layer's source map │ ├── pdf.worker.mjs - core layer │ └── pdf.worker.mjs.map - core layer's source map ├── web/ │ ├── cmaps/ - character maps (required by core) │ ├── compressed.tracemonkey-pldi-09.pdf - PDF file for testing purposes │ ├── images/ - images for the viewer and annotation icons │ ├── locale/ - translation files │ ├── viewer.css - viewer style sheet │ ├── viewer.html - viewer layout │ ├── viewer.mjs - viewer layer │ └── viewer.mjs.map - viewer layer's source map └── LICENSE
🌐
jsDelivr
jsdelivr.com › package › npm › pdfjs-dist
pdfjs-dist CDN by jsDelivr - A CDN for npm and GitHub
September 22, 2014 - A free, fast, and reliable CDN for pdfjs-dist. Generic build of Mozilla's PDF.js library.
Published   Sep 22, 2014
🌐
CDNPKG
cdnpkg.com › home › pdf.js › pdf.worker.min.js
PDF.WORKER.MIN.JS: DOWNLOAD - CDNPKG
Download pdf.worker.min.js or get a CDN url for 1510 versions of pdf.js.
🌐
GitHub
github.com › mozilla › pdf.js › issues › 14900
CDN-hosted legacy version of pdfjs-dist has syntax errors in IE · Issue #14900 · mozilla/pdf.js
February 28, 2022 - pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/[email protected]/legacy/build/pdf.worker.min.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: scale}); // Prepare canvas using PDF page dimensions var canvas = document.getElementById('the-canvas'); var context = canvas.getContext('2d'); canvas.heigh
Published   May 10, 2022
🌐
Nutrient
nutrient.io › blog › sdk › implement pdf viewer pdf js
PDF.js viewer example: Implement a simple PDF viewer with PDF.js
October 17, 2024 - This HTML file includes the basic ... pdf.min.mjs to include PDF.js. First, we import the pdfjsLib from a CDN and set the worker script to handle rendering PDF files in the background....
Find elsewhere
🌐
CDNPKG
cdnpkg.com › home › pdf.js
PDF.JS CDN links [mozilla/pdf.js] - CDNPKG
1 CDN to use with PDF.JS (mozilla/pdf.js) . Find out the best CDN to use with pdf.js or use multiple CDN as fallback. Simply copy and paste one of these URL !.
🌐
CopyProgramming
copyprogramming.com › howto › how-to-import-pdf-js-using-cdn
Utilizing CDN for the Integration of pdf.js - Javascript
May 16, 2023 - pdfjsLib.GlobalWorkerOptions.workerSrc ... so will result in the absence of .then() . Solution 2: To begin, refer to the example titled "Hello World with document load error handling" available on this webpage: https://mozilla.github.io/pdf.js/examples/. After getDocument() , it ...
🌐
jsDelivr
cdn.jsdelivr.net › build
dw-mx-pdfjs CDN by jsDelivr - A free, fast, and reliable Open Source CDN
Free Open Source CDN for dw-mx-pdfjs · Looking for a nice landing page for your package? https://www.jsdelivr.com/package/npm/dw-mx-pdfjs
🌐
npm
npmjs.com › package › react-pdf › v › 5.3.0
react-pdf - npm
Alternatively, you could use the minified pdf.worker.min.js from an external CDN:
      » npm install react-pdf
    
Published   Aug 15, 2025
Version   5.3.0
Author   Wojciech Maj
🌐
npm
npmjs.com › package › @netless › app-pdfjs
@netless/app-pdfjs - npm
install(register, { appOptions: { pdfjsLib: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@latest/build/pdf.min.mjs', workerSrc: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@latest/build/pdf.worker.min.mjs', } }) If the URL is blocked by your CSP, you ...
      » npm install @netless/app-pdfjs
    
Published   Jun 10, 2025
Version   0.1.6
Author   netless
🌐
GitHub
github.com › mozilla › pdf.js › issues › 15790
Compressed pdf.worker.min.js works with errors · Issue #15790 · mozilla/pdf.js
October 8, 2022 - The problem begins when I use legacy/pdf.worker.min.js file instead of pdf.worker.js (downloaded from https://www.jsdelivr.com/package/npm/pdfjs-dist). Steps to reproduce the problem: Replace pretty code of build/pdf.worker.js with code from https://cdn.jsdelivr.net/npm/[email protected]/l...
Published   Dec 07, 2022
🌐
Snyk
snyk.io › advisor › pdfjs-dist › functions › pdfjs-dist.globalworkeroptions
How to use the pdfjs-dist.GlobalWorkerOptions function in pdfjs-dist | Snyk
import * as React from "react"; import * as CSSModules from "react-css-modules"; import * as styles from "./index.less"; import * as pdfjsLib from "pdfjs-dist"; const pdfjsViewer = require("../../../node_modules/pdfjs-dist/web/pdf_viewer.js"); // The workerSrc property shall be specified. pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.550/pdf.worker.js"; // default scale const DEFAULT_MIN_SCALE = 0.25; const DEFAULT_MAX_SCALE = 10.0; let USE_ONLY_CSS_ZOOM = true; let TEXT_LAYER_MODE = 0; // DISABLE let MAX_IMAGE_SIZE = 1024 * 1024; let CMAP_PACKED = true; let DEFAULT_URL = "/test.pdf"; let DEFAULT_SCALE_DELTA = 1.1; let MIN_SCALE = DEFAULT_MIN_SCALE; let MAX_SCALE = DEFAULT_MAX_SCALE; let DEFAULT_SCALE_VALUE: string|number = "auto"; // in order to be responsive interface IProps { url: string|object; page?: number|string; scale?: number|string;
🌐
PDF.js Express
pdfjs.community › technical support
Is there any CDN url for PDFJS express library - Technical Support - PDF.js Express
May 30, 2021 - Hi Team, We are giving the web-viewer folder library path for rendering the pdf. In local it is working. But in our server we are getting path issue. In our angular project we are using only main.js. Do you have CDN url for the library so that we don’t want to store the lib folder in our ...
🌐
GitHub
github.com › mozilla › pdf.js › issues › 5490
make pdf.js available on a cdn · Issue #5490 · mozilla/pdf.js
November 17, 2014 - Having pdf.js available on a public cdn can simplify installation and update workflows in some cases. cdnjs will likely host pdf.js for free if was ask them to. cdnjs/cdnjs#3824
Published   Nov 17, 2014
🌐
PDF-LIB
pdf-lib.js.org
PDF-LIB · Create and modify PDF documents in any JavaScript environment.
https://cdn.jsdelivr.net/npm/[email protected]/dist/pdf-lib.min.js ... import { PDFDocument, StandardFonts, rgb } from 'pdf-lib' async function createPdf() { const pdfDoc = await PDFDocument.create() const timesRomanFont = await pdfDoc.embedFon...