npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pdfjs-serverless

v1.2.3

Published

PDF.js redistributed as a single bundle for edge and serverless runtimes

Readme

pdfjs-serverless

A redistribution of Mozilla's PDF.js as a single bundle for edge and serverless runtimes, like Cloudflare Workers. Use it as a drop-in replacement for pdfjs-dist to parse PDF documents and extract text content – no extra dependencies needed.

The whole export is about 1.6 MB (minified).

Installation

Run the following command to add pdfjs-serverless to your project.

# pnpm
pnpm add pdfjs-serverless

# npm
npm install pdfjs-serverless

Usage

[!TIP] For common operations, such as extracting text content or images from PDF files, you can use the unpdf package. It is a wrapper around pdfjs-serverless and provides a simple API for common use cases.

pdfjs-serverless provides the same API as the original PDF.js library. To use any of the PDF.js exports, rename the import to pdfjs-serverless instead of pdfjs-dist:

- import { getDocument } from 'pdfjs-dist'
+ import { getDocument } from 'pdfjs-serverless'

Here's a full Cloudflare Workers example that accepts a PDF via POST and returns the extracted text as JSON:

import { getDocument } from 'pdfjs-serverless'

export default {
  async fetch(request) {
    if (request.method !== 'POST')
      return new Response('Method Not Allowed', { status: 405 })

    // Get the PDF file from the POST request body as a buffer
    const data = await request.arrayBuffer()

    const document = await getDocument({
      data: new Uint8Array(data),
      useSystemFonts: true,
    }).promise

    // Get metadata and initialize output object
    const metadata = await document.getMetadata()
    const output = {
      metadata,
      pages: []
    }

    // Iterate through each page and fetch the text content
    for (let i = 1; i <= document.numPages; i++) {
      const page = await document.getPage(i)
      const textContent = await page.getTextContent()
      const contents = textContent.items.map(item => item.str).join(' ')

      // Add page content to output
      output.pages.push({
        pageNumber: i,
        content: contents
      })
    }

    // Return the results as JSON
    return new Response(JSON.stringify(output), {
      headers: { 'Content-Type': 'application/json' }
    })
  }
}

How It Works

[!NOTE] pdfjs-serverless is currently built from PDF.js v5.6.205.

Heart and soul of this package is the rollup.config.ts file. It uses Rollup to bundle PDF.js into a single file for serverless environments. The key techniques:

  • String replacements strip browser-specific references (e.g. typeof window) from the PDF.js source.
  • Node.js compatibility is enforced by mocking @napi-rs/canvas and setting isNodeJS to true – paradoxical, but it unlocks the right code paths.
  • Worker inlining embeds the PDF.js worker directly into the main bundle, since serverless runtimes can't load separate worker files.
  • Global polyfills provide missing APIs like FinalizationRegistry (unavailable in Cloudflare Workers).

Inspiration

  • pdf.mjs, a nodeless build of PDF.js v2.

License

MIT License © 2023-PRESENT Johann Schopplich