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 🙏

© 2025 – Pkg Stats / Ryan Hefner

pdfnano

v0.2.9

Published

A pure JavaScript/TypeScript PDF parser with no external dependencies

Readme

PDFNano

npm version MIT License

A lightweight, robust PDF parsing library for Node.js written in TypeScript. Extract text, images, and metadata from PDFs — even damaged ones — with no external dependencies.

Features

  • Parse PDF structure (XRef tables, trailer, object references)
  • Extract text with layout and positioning
  • Extract images (JPEG, PNG, JPX, raw) with smart packaging
  • Get document metadata (title, author, creation/modification dates, etc.)
  • Markdown and JSON output helpers (from file or buffer)
  • Automatic recovery from damaged PDFs
  • TypeScript typings included

Installation

npm install pdfnano

Quick Start

import { PDFParser } from 'pdfnano';

async function parsePDF(filePath: string) {
  const parser = new PDFParser();
  const result = await parser.parseFile(filePath);
  console.log('Full text:', result.text);
  console.log('Pages:', result.pages.length);
  console.log('Title:', result.metadata.title);
}

parsePDF('document.pdf');

What’s New (This Release)

  • Text decoding fixes
    • ToUnicode CMap parsing (bfchar, bfrange, array mappings)
    • UTF‑16BE ToUnicode BOM handling
    • Operator parsing fix for content streams (reliable Tj/TJ and quotes)
  • Image extraction improvements
    • Returns correct mimeType (e.g., image/jpeg, image/png)
    • Wraps raw 8‑bit DeviceGray/DeviceRGB/RGBA pixel data into a valid PNG automatically
    • Sets accurate pageNumber for images
  • New convenience APIs
    • Markdown output: parseFileToMarkdown, parseBufferToMarkdown
    • JSON output: parseFileToJSON, parseBufferToJSON (base64‑encodes image data)
    • Image extraction: extractImagesFromFile, extractImagesFromBuffer

API Reference

PDFParser methods

  • Core parsing

    • parseFile(filePath: string): Promise<PDFParseResult>
    • parseBuffer(buffer: Buffer): Promise<PDFParseResult>
  • Markdown and JSON helpers

    • parseFileToMarkdown(filePath: string): Promise<string>
    • parseBufferToMarkdown(buffer: Buffer): Promise<string>
    • parseFileToJSON(filePath: string): Promise<string>
    • parseBufferToJSON(buffer: Buffer): Promise<string>
  • Images only

    • extractImagesFromFile(filePath: string): Promise<PDFImage[]>
    • extractImagesFromBuffer(buffer: Buffer): Promise<PDFImage[]>

PDFParseResult

  • text: string — All text from the PDF
  • pages: PDFPage[] — Per-page text, images, and dimensions
  • images: PDFImage[] — All extracted images
  • metadata: PDFMetadata — Title, author, page count, etc.

Examples

Parse a file and print a Markdown summary

import { PDFParser } from 'pdfnano';

async function toMarkdown(filePath: string) {
  const parser = new PDFParser();
  const md = await parser.parseFileToMarkdown(filePath);
  console.log(md);
}

toMarkdown('document.pdf');

Parse a buffer to JSON (with images as base64)

import { PDFParser } from 'pdfnano';
import * as fs from 'fs';

async function toJSON(filePath: string) {
  const parser = new PDFParser();
  const buf = fs.readFileSync(filePath);
  const jsonStr = await parser.parseBufferToJSON(buf);
  const json = JSON.parse(jsonStr);
  console.log(json.pages.length, 'pages');
}

toJSON('document.pdf');

Extract images and save to disk

import { PDFParser } from 'pdfnano';
import * as fs from 'fs';
import * as path from 'path';

async function saveImages(filePath: string, outDir: string) {
  const parser = new PDFParser();
  const images = await parser.extractImagesFromFile(filePath);
  if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
  const ext = (m: string) => m === 'image/jpeg' ? 'jpg' : (m === 'image/png' ? 'png' : 'bin');
  images.forEach((img, i) => {
    const name = `image_${i + 1}_p${img.pageNumber}_${img.width}x${img.height}.${ext(img.mimeType)}`;
    fs.writeFileSync(path.join(outDir, name), img.data);
  });
  console.log(`Saved ${images.length} image(s) to`, outDir);
}

saveImages('document.pdf', './out');

Behavior With Damaged PDFs

PDFNano attempts best-effort recovery when it encounters malformed XRef tables or missing markers. This allows parsing many real-world PDFs that otherwise fail. If you prefer strict mode (reject clearly invalid inputs), add a pre-check for the %PDF- header before calling the parser.

Notes & Limitations

  • Image color space conversions (e.g., CMYK → RGB) are not performed. CMYK images may be returned as raw data.
  • Some advanced filters (LZW, JBIG2, CCITT) are placeholders; data is returned as-is.
  • Text layout reconstruction is heuristic; exact visual layout is not guaranteed.

Development

npm install
npm run build
npm test

License

MIT