🌐
npm
npmjs.com › package › pdfjs-dist
pdfjs-dist - npm
Generic build of Mozilla's PDF.js library.. Latest version: 5.4.449, last published: 12 days ago. Start using pdfjs-dist in your project by running `npm i pdfjs-dist`. There are 2279 other projects in the npm registry using pdfjs-dist.
      » npm install pdfjs-dist
    
Published   Nov 29, 2025
Version   5.4.449
🌐
Mozilla
mozilla.github.io › pdf.js › getting_started
PDF.js - Getting Started
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.
People also ask

What is PDF.js used for?
PDF.js is used for viewing, parsing, and rendering PDF files directly in the browser, providing a seamless user experience.
🌐
dhiwise.com
dhiwise.com › post › how-to-integrate-pdfjs-dist-for-pdf-rendering
A Beginner’s Guide To Pdfjs-dist Integration
Is PDF.js free to use?
Yes, PDF.js is free and open-source, distributed under the Apache 2.0 license, allowing for both personal and commercial use.
🌐
dhiwise.com
dhiwise.com › post › how-to-integrate-pdfjs-dist-for-pdf-rendering
A Beginner’s Guide To Pdfjs-dist Integration
How to use PDF.js in React?
To use PDF.js in React, import the pdfjs-dist package and create a component that loads and displays PDF files using the library's API.
🌐
dhiwise.com
dhiwise.com › post › how-to-integrate-pdfjs-dist-for-pdf-rendering
A Beginner’s Guide To Pdfjs-dist Integration
🌐
GitHub
github.com › mozilla › pdfjs-dist
GitHub - mozilla/pdfjs-dist: Generic build of PDF.js library.
Generic build of PDF.js library. . Contribute to mozilla/pdfjs-dist development by creating an account on GitHub.
Starred by 1.3K users
Forked by 568 users
Languages   JavaScript 99.2% | CSS 0.8%
🌐
CodeSandbox
codesandbox.io › examples › package › pdfjs-dist
pdfjs-dist examples - CodeSandbox
Renders online/local documents. @react-pdf-viewer/examples · flexcomAplicacion para la comunicacion interna. pdf-viewer (forked) lex961Find more examples or templates · AboutGeneric build of Mozilla's PDF.js library.6,183,977Weekly Downloads · Latest version5.4.449 · LicenseApache-2.0 · External Links · mozilla.github.io/pdf.js/ github.com/mozilla/pdf.js · github.com/mozilla/pdf.js/issues · @pdfjs-distCollaborators
🌐
DhiWise
dhiwise.com › post › how-to-integrate-pdfjs-dist-for-pdf-rendering
A Beginner’s Guide To Pdfjs-dist Integration
December 6, 2024 - To use PDF.js, you can start by downloading the pre-built version from the pdfjs-dist package. This version serves as a good starting point for integrating PDF viewing capabilities into your application. For more control, consider building PDF.js from source using the npx gulp generic command.
🌐
GitHub
github.com › mozilla › pdf.js › wiki › setup-pdf.js-in-a-website
Setup PDF.js in a website
npm install pdfjs-dist --save · You can find a usage example at https://github.com/mozilla/pdf.js/blob/master/examples/node/getinfo.mjs. Install the PDF.js dependency in your project: npm install pdfjs-dist --save-dev · To use the library in your project add require('pdfjs-dist') to your file requires and build your project normally.
Author   mozilla
🌐
CloudDefense.ai
clouddefense.ai › code › javascript › example › pdfjs-dist
Top 10 Examples of pdfjs-dist code in Javascript
PDFJS.disableWorker = true; let doc; return new Promise((resolve, reject) => { PDFJS.getDocument(dataBuffer) .promise.then(document => { doc = document; ret.numpages = doc.numPages; let metaData; doc.getMetadata() .then(metadata => { metaData = metadata; ret.info = metaData ? metaData.info : undefined; ret.metadata = metaData ? metaData.metadata : undefined; ... init (timestamp) { const t = this; this.loadingTask = pdfJsLib.getDocument({ url: this.url, cMapUrl: '../../node_modules/pdfjs-dist/cmaps/', cMapPacked: true }); this.loadingTask.promise.then(async function(pdf) { console.time('PDF_Ren
🌐
Openbase
openbase.com › js › pdfjs-dist
pdfjs-dist: Docs, Community, Tutorials, Reviews | Openbase
pdfjs-dist documentation and community, including tutorials, reviews, alternatives, and more
Find elsewhere
🌐
Tabnine
tabnine.com › home page › code › javascript › pdfjs-dist
pdfjs-dist JavaScript and Node.js code examples | Tabnine
loadingTask.promise.then(function(doc) { const numPages = doc.numPages; lastPromise = doc.getMetadata().then(function (data) { if (data.metadata) { return doc.getPage(pageNum).then(function (page) { const viewport = page.getViewport({ scale: 1.0, }); return page.getTextContent({normalizeWhitespace:true}).then(function (content) { const strings = content.items.map(function(item, index, array) { let str = item.str; if (index > 0 && item.transform[5] !== array[index-1].transform[5]) { str = '\n' + str; }).then(function () { lastPromise = lastPromise.then(loadPage.bind(null, i)); }).then(function () { console.log('# End of Document');
Top answer
1 of 6
9

Here's a super ugly workaround for client-components, until something better is proposed (I hate this too, but it works):

npm i pdfjs-dist

Edit 1: A hook might be better for reusability:

Create Your Hook

"use client";
import {useEffect} from "react";
import * as PDFJS from "pdfjs-dist/types/src/pdf";

export const usePDFJS = (onLoad: (pdfjs: typeof PDFJS) => Promise<void>, deps: (string | number | boolean | undefined | null)[] = []) => {
  
  const [pdfjs, setPDFJS] = useState<typeof PDFJS>(null);
  
  // load the library once on mount (the webpack import automatically sets-up the worker)
  useEffect(() => {
    import("pdfjs-dist/webpack.mjs").then(setPDFJS)
  }, []);

  // execute the callback function whenever PDFJS loads (or a custom dependency-array updates)
  useEffect(() => {
    if(!pdfjs) return;
    (async () => await onLoad(pdfjs))();
  }, [ pdfjs, ...deps ]);
}

Typescript Fix

Your compiler might complain about a typescript issue; if so, I just added an index.d.ts (note the .d.ts) at the same level as my hook:

declare module 'pdfjs-dist/webpack.mjs' { export * from 'pdfjs-dist' }

Use Your Hook

"use client";
import {usePDFJS} from "...";


export default function Page() {
  usePDFJS(async (pdfjs) => {
    console.log(pdfjs)
  })
}
2 of 6
4

There's a pretty lengthy discussion over on Github issues where people have managed to get it working either on client:

I decided to follow a simple path, I downloaded the stable version from the official website. I put all the files in the public folder. Then I added this tag to my component:

<script src="/pdfjs/pdf.mjs" type="module" />

then adding code in useEffect:

  const pdfjs = window.pdfjsLib as typeof import('pdfjs-dist/types/src/pdf')
  const pdfjsWorker = await import('pdfjs-dist/build/pdf.worker.min.mjs');
  pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;

  const pdfDocument = pdfjs.getDocument('http://localhost:3000/pdf-files/myFile.pdf')

  console.log('pdfDocument', pdfDocument);

Or server-side, via appDir (e.g. app/api/pdf/route.js)

import * as pdfjs from 'pdfjs-dist/build/pdf.min.mjs';
await import('pdfjs-dist/build/pdf.worker.min.mjs');

export async function POST(req, res) {
  const pdf = await pdfjs.getDocument(
    'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'
  ).promise;
  const page = await pdf.getPage(1);
  const textContent = await page.getTextContent();
  return NextResponse.json({ message: textContent }, { status: 200 });
}

I've personally just tested this last API-one on Next.js 14.1.1 and it works just fine after a npm install pdfjs-dist

🌐
UNPKG
unpkg.com › pdfjs-dist
pdfjs-dist
Generic build of Mozilla's PDF.js library · mozilla.github.io/pdf.js/
🌐
Mozilla
mozilla.github.io › pdf.js
PDF.js - Home
A general-purpose, web standards-based platform for parsing and rendering PDFs.
🌐
Snyk
snyk.io › advisor › pdfjs-dist › functions › pdfjs-dist.getdocument
How to use the pdfjs-dist.getDocument function in pdfjs-dist | Snyk
private open (params) { let url = params.url let self = this this.setTitleUsingUrl(url) // Loading document. let loadingTask = pdfjsLib.getDocument({ url: url, withCredentials: true, maxImageSize: MAX_IMAGE_SIZE, cMapPacked: CMAP_PACKED }) this.pdfLoadingTask = loadingTask loadingTask.onProgress = function (progressData) { self.progress(progressData.loaded / progressData.total) } return loadingTask.promise.then(function (pdfDocument) { // Document loaded, specifying document for the viewer.
🌐
Snyk
snyk.io › advisor › pdfjs-dist › functions › pdfjs-dist.globalworkeroptions
How to use the pdfjs-dist.GlobalWorkerOptions function in pdfjs-dist | Snyk
// http://creativecommons.org/licenses/publicdomain/ // Hello world example for browserify. var pdfjsLib = require("pdfjs-dist"); var pdfPath = "../learning/helloworld.pdf"; // Setting worker path to worker bundle. pdfjsLib.GlobalWorkerOptions.workerSrc = "../../build/browserify/pdf.worker.bundle.js"; // Loading a document.
🌐
Snyk
snyk.io › advisor › pdfjs-dist › functions › pdfjs-dist.pdfjs.getdocument
How to use the pdfjs-dist.PDFJS.getDocument function in pdfjs-dist | Snyk
sitepoint-editors / aurelia-pdfjs / src / resources / elements / pdf-document.js View on Github · .then((pdf) => { if (pdf) { pdf.destroy(); } return PDFJS.getDocument({ url: newValue, worker: this.worker }); }) .then((pdf) => { Generic build of Mozilla's PDF.js library. GitHub · Apache-2.0 · Latest version published 3 days ago · 89 / 100 · Full package analysis · pdfjs-dist.disableWorker ·
🌐
Openbase
openbase.com › js › @bundled-es-modules › pdfjs-dist
@bundled-es-modules/pdfjs-dist: Docs & Community | Openbase
import pdfjs from "@bundled-es-modules/pdfjs-dist/build/pdf"; import viewer from "@bundled-es-modules/pdfjs-dist/web/pdf_viewer"; pdfjs.GlobalWorkerOptions.workerSrc = "@bundled-es-modules/pdfjs-dist/pdf.worker.js"; var url = "basicapi.pdf"; var loadingTask = pdfjs.getDocument(url); //...