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

doc-extract

v1.0.4

Published

A Node.js library for reading and extracting text from various document formats (PDF, DOCX, DOC, PPT, PPTX, TXT)

Readme

Doc Extract

A powerful Node.js library for reading and extracting text from various document formats including PDF, DOCX, DOC, PPT, PPTX, and TXT files.

Features

  • 📄 Multiple Format Support: PDF, DOCX, DOC, PPT, PPTX, TXT
  • 🔍 Text Extraction: Extract clean text content from documents
  • 📊 Rich Metadata: Get document statistics (word count, character count, pages, etc.)
  • 💾 Buffer Support: Read documents from memory buffers
  • 🔧 TypeScript: Full TypeScript support with type definitions
  • 🚀 Promise-based: Modern async/await API
  • 🛡️ Error Handling: Comprehensive error handling with custom error types

Installation

npm install doc-extract

System Dependencies

This library depends on some system packages for full functionality:

For PDF support:

  • No additional dependencies required

For PowerPoint and DOC support:

# Ubuntu/Debian
sudo apt-get install antiword unrtf poppler-utils tesseract-ocr

# macOS
brew install antiword unrtf poppler tesseract

# Windows
# Install poppler and tesseract manually or use chocolatey:
choco install poppler tesseract

Quick Start

import DocumentReader, { readDocument } from "doc-extract";

// Simple usage
const content = await readDocument("./path/to/document.pdf");
console.log(content.text);
console.log(content.metadata);

// Using the class for more control
const reader = new DocumentReader({ debug: true });
const content = await reader.readDocument("./path/to/document.docx");

API Reference

Class: DocumentReader

Constructor

new DocumentReader(options?: { debug?: boolean })
  • options.debug: Enable debug logging (default: false)

Methods

readDocument(filePath: string): Promise

Read a document from file path.

const reader = new DocumentReader();
const content = await reader.readDocument("./document.pdf");
readDocumentFromBuffer(buffer: Buffer, fileName: string, mimeType?: string): Promise

Read a document from a Buffer.

const fs = require("fs");
const buffer = fs.readFileSync("./document.pdf");
const content = await reader.readDocumentFromBuffer(buffer, "document.pdf");
readMultipleDocuments(filePaths: string[]): Promise<DocumentContent[]>

Read multiple documents at once.

const contents = await reader.readMultipleDocuments([
  "./doc1.pdf",
  "./doc2.docx",
  "./doc3.pptx",
]);
readMultipleFromBuffers(buffers: Array<{buffer: Buffer, fileName: string, mimeType?: string}>): Promise<DocumentContent[]>

Read multiple documents from buffers.

const contents = await reader.readMultipleFromBuffers([
  { buffer: buffer1, fileName: "doc1.pdf" },
  { buffer: buffer2, fileName: "doc2.docx" },
]);
Specific Format Methods
// PDF specific
const pdfContent = await reader.readPdf("./document.pdf");

// DOCX specific (includes HTML conversion)
const docxContent = await reader.readDocx("./document.docx");
console.log(docxContent.html); // HTML version of the document

// PowerPoint specific
const pptContent = await reader.readPowerPoint("./presentation.pptx");
Utility Methods
// Check if format is supported
const isSupported = reader.isFormatSupported("./document.pdf"); // true

// Get all supported formats
const formats = reader.getSupportedFormats(); // ['pdf', 'docx', 'doc', 'pptx', 'ppt', 'txt']

// Validate file
await reader.validateFile("./document.pdf"); // throws error if invalid

Convenience Functions

import { readDocument, readDocumentFromBuffer } from "doc-extract";

// Quick read from file
const content = await readDocument("./document.pdf");

// Quick read from buffer
const content = await readDocumentFromBuffer(buffer, "document.pdf");

Types

DocumentContent

interface DocumentContent {
  text: string;
  metadata?: {
    pages?: number;
    words?: number;
    characters?: number;
    fileSize?: number;
    fileName?: string;
  };
}

PdfContent

interface PdfContent extends DocumentContent {
  metadata: DocumentContent["metadata"] & {
    pages: number;
    info?: any; // PDF metadata from pdf-parse
  };
}

DocxContent

interface DocxContent extends DocumentContent {
  html?: string; // HTML version of the document
  messages?: any[]; // Conversion messages from mammoth
}

SupportedFormats

enum SupportedFormats {
  PDF = "pdf",
  DOCX = "docx",
  DOC = "doc",
  PPTX = "pptx",
  PPT = "ppt",
  TXT = "txt",
}

Error Handling

The library uses custom error types for better error handling:

import { DocumentReaderError } from "doc-extract";

try {
  const content = await readDocument("./nonexistent.pdf");
} catch (error) {
  if (error instanceof DocumentReaderError) {
    console.log("Error code:", error.code);
    console.log("Error message:", error.message);
  }
}

Error Codes

  • UNSUPPORTED_FORMAT: File format not supported
  • READ_ERROR: General read error
  • PDF_READ_ERROR: PDF-specific read error
  • DOCX_READ_ERROR: DOCX-specific read error
  • TEXTRACT_READ_ERROR: Textract-related error
  • BUFFER_READ_ERROR: Buffer reading error
  • VALIDATION_ERROR: File validation error
  • INVALID_FILE_PATH: Invalid file path

Examples

Express.js Integration

import express from "express";
import multer from "multer";
import { DocumentReader } from "doc-extract";

const app = express();
const upload = multer();
const reader = new DocumentReader();

app.post("/upload", upload.single("document"), async (req, res) => {
  try {
    if (!req.file) {
      return res.status(400).json({ error: "No file uploaded" });
    }

    const content = await reader.readDocumentFromBuffer(
      req.file.buffer,
      req.file.originalname,
      req.file.mimetype
    );

    res.json({
      text: content.text,
      metadata: content.metadata,
    });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

Batch Processing

import { DocumentReader } from "doc-extract";
import { promises as fs } from "fs";
import path from "path";

async function processDocumentsInDirectory(dirPath: string) {
  const reader = new DocumentReader({ debug: true });

  const files = await fs.readdir(dirPath);
  const documentPaths = files
    .filter((file) => reader.isFormatSupportedByName(file))
    .map((file) => path.join(dirPath, file));

  const results = await reader.readMultipleDocuments(documentPaths);

  results.forEach((content, index) => {
    console.log(`Document ${documentPaths[index]}:`);
    console.log(`Words: ${content.metadata?.words}`);
    console.log(`Characters: ${content.metadata?.characters}`);
    console.log("---");
  });
}

Search in Documents

import { DocumentReader } from "doc-extract";

async function searchInDocument(filePath: string, searchTerm: string) {
  const reader = new DocumentReader();
  const content = await reader.readDocument(filePath);

  const lines = content.text.split("\n");
  const matchingLines = lines
    .map((line, index) => ({ line, lineNumber: index + 1 }))
    .filter(({ line }) =>
      line.toLowerCase().includes(searchTerm.toLowerCase())
    );

  return {
    totalMatches: matchingLines.length,
    matches: matchingLines,
    metadata: content.metadata,
  };
}

Performance Tips

  1. Reuse DocumentReader instances - The class can be reused for multiple operations
  2. Use batch methods - readMultipleDocuments() is more efficient than individual calls
  3. Enable debug mode only during development
  4. Clean up temporary files - The library handles this automatically, but ensure your temp directory has sufficient space

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development Setup

git clone https://github.com/HaiderNakara/doc-extract.git
cd doc-extract
npm install
npm run build
npm test

Running Tests

npm test          # Run tests once
npm run test:watch # Run tests in watch mode
npm run test:coverage # Run tests with coverage

License

This project is licensed under the MIT License - see the LICENSE file for details.