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
🌐
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%
🌐
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
Some files are hidden, click to show all files · https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.js · https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.js.map · https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js ...
🌐
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
🌐
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
🌐
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
🌐
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]/legacy/build/pdf.worker.min.js ·
Published   Dec 07, 2022
Find elsewhere
🌐
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
🌐
jsDelivr
cdn.jsdelivr.net › build
dw-mx-pdfjs CDN by jsDelivr - A free, fast, and reliable Open Source CDN
dw-mx-pdfjs CDN by jsDelivr - A free, fast, and reliable Open Source CDN for npm and GitHub
🌐
GitHub
github.com › eKoopmans › html2pdf.js
GitHub - eKoopmans/html2pdf.js: Client-side HTML-to-PDF rendering using pure JS.
html2pdf.js converts any webpage or element into a printable PDF entirely client-side using html2canvas and jsPDF. ... <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js" integrity="sha512-GsLlZN/3...
Starred by 4.7K users
Forked by 1.5K users
Languages   JavaScript 66.3% | HTML 33.6% | CSS 0.1%
🌐
GitHub
github.com › VadimDez › ng2-pdf-viewer › issues › 504
Shouldn't ng2-pdf-viewer use pdf.worker.min.js from pdfjs-dist library? · Issue #504 · VadimDez/ng2-pdf-viewer
August 7, 2019 - My project has to work without outside download requests, so we had to include our own pdf.worker.min.js.
Published   Aug 07, 2019
🌐
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
🌐
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 !.
🌐
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 › wojtekmaj › react-pdf › issues › 1855
Can't resolve 'pdfjs-dist/build/pdf.worker.min.mjs in Nextjs 14 app router · Issue #1855 · wojtekmaj/react-pdf
August 13, 2024 - If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo · import { Box, CircularProgress, Pagination, Stack, Typography } from '@mui/material'; import React, { useState } from 'react'; import { DocumentText } from 'iconsax-react'; import 'react-pdf/dist/esm/Page/AnnotationLayer.css'; import 'react-pdf/dist/esm/Page/TextLayer.css'; import { Document, Page, pdfjs } from 'react-pdf'; pdfjs.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString(); const options = { cMapUrl: '/cmaps/', standardFontDataUrl: '/stan
Published   Aug 13, 2024