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
🌐
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
🌐
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
Content delivery at its finest. cdnjs is a free and open-source CDN service trusted by over 12.5% of all websites, serving over 200 billion requests each month, powered by Cloudflare.
🌐
Mozilla
mozilla.github.io › pdf.js › getting_started
PDF.js - Getting Started
PDF.js is hosted on several free CDNs: https://www.jsdelivr.com/package/npm/pdfjs-dist · https://cdnjs.com/libraries/pdf.js · https://unpkg.com/pdfjs-dist/ Note that we only mention the most relevant files and folders.
🌐
jsDelivr
cdn.jsdelivr.net › build
pdfjs-dist CDN by jsDelivr - A free, fast, and reliable Open Source CDN
pdfjs-dist CDN by jsDelivr - A free, fast, and reliable Open Source CDN for npm and GitHub
🌐
UNPKG
unpkg.com › pdfjs-dist
pdfjs-dist
The CDN for everything on npm
🌐
jsDelivr
jsdelivr.com › package › npm › pdfjs-dist-viewer-min
pdfjs-dist-viewer-min CDN by jsDelivr - A CDN for npm and GitHub
July 13, 2024 - A free, fast, and reliable CDN for pdfjs-dist-viewer-min. Generic minified build of Mozilla's PDF.js library including the viewer component.
Published   Jun 19, 2016
🌐
Mozilla
mozilla.github.io › pdf.js
PDF.js - Home
A general-purpose, web standards-based platform for parsing and rendering PDFs.
Find elsewhere
🌐
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
🌐
jsDelivr
jsdelivr.com › package › npm › pdfjs-dist-min
pdfjs-dist-min CDN by jsDelivr - A CDN for npm and GitHub
May 17, 2023 - A free, fast, and reliable CDN for pdfjs-dist-min. Generic build of Mozilla's PDF.js library.
Published   May 17, 2023
🌐
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
March 2, 2022 - <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>PDF.js &#39;Hello, world!&#39; example, legacy version</title> <style> #the-canvas { border: 1px solid black; direction: ltr; } </style> </head> <body> <h1>PDF.js 'Hello, world!' example, legacy version</h1> <p>This example is identical to <a href="https://mozilla.github.io/pdf.js/examples/index.html#hello-world-with-document-load-error-handling">https://mozilla.github.io/pdf.js/examples/index.html#hello-world-with-document-load-error-handling</a> except the two script URLs have been replaced with CDN-hosted legacy versions of the scripts.</p> <canvas id="the-canvas"></canvas> <script src="https://cdn.jsdelivr.net/npm/[email protected]/legacy/build/pdf.min.js"></script> <script> // If absolute URL from the remote server is provided, configure the CORS // header on that server.
Published   May 10, 2022
🌐
GitHub
github.com › mozilla › pdf.js › discussions › 17642
Using CDN links in "demo" `web/viewer.html` · mozilla/pdf.js · Discussion #17642
If you download the demo, you will have a file which looks like this: pdfjs-4.10.38-dist.zip · The library, pdf.mjs and some other files are hosted on different CDN's. One of these files is pdf_viewer.mjs. This is not the same file as viewer.mjs which is part of the demo and cannot be found on the CDN's!
Author   mozilla
🌐
jsDelivr
jsdelivr.com › package › npm › pdfjs-dist-legacy
pdfjs-dist-legacy CDN by jsDelivr - A CDN for npm and GitHub
December 2, 2022 - A free, fast, and reliable CDN for pdfjs-dist-legacy. a legacy implementation of pdfjs-dist
Published   Dec 02, 2022
🌐
jsDelivr
cdn.jsdelivr.net › build
pdfjs-dist-2 CDN by jsDelivr - A free, fast, and reliable Open Source CDN
pdfjs-dist-2 CDN by jsDelivr - A free, fast, and reliable Open Source CDN for npm and GitHub
🌐
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 !.
🌐
GitHub
github.com › mozilla › pdf.js
GitHub - mozilla/pdf.js: PDF Reader in JavaScript
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.
Starred by 52.4K users
Forked by 10.5K users
Languages   JavaScript 71.3% | Fluent 25.5% | CSS 2.2%
🌐
PSPDFKit
pspdfkit.com › blog › 2019 › implement-pdf-viewer-pdf-js
Implement a Simple PDF Viewer with PDF.js
October 17, 2024 - src="https://unpkg.com/pdfjs-d... page mode, and a viewport where the PDF will be rendered. We use a CDN version of pdf.min.mjs to include PDF.js....
🌐
CDNPKG
cdnpkg.com › home › pdfjs-dist › 2.2.228
PDFJS-DIST 2.2.228 CDN links - CDNPKG
1 CDN to use with PDFJS-DIST 2.2.228. Find out the best CDN to use with pdfjs-dist 2.2.228 or use multiple CDN as fallback. Simply copy and paste one of these URL !.
🌐
jsDelivr
cdn.jsdelivr.net › @types/[email protected]
@types/pdfjs-dist CDN by jsDelivr - A free, fast, and reliable Open Source CDN
@types/pdfjs-dist CDN by jsDelivr - A free, fast, and reliable Open Source CDN for npm and GitHub