MCPcopy Index your code
hub / github.com/electrovir/pdf-text-reader

github.com/electrovir/pdf-text-reader @v5.1.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v5.1.1 ↗ · + Follow
10 symbols 45 edges 13 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

PDF Text Reader

Dead simple PDF text reader for Node.js. Uses Mozilla's pdfjs-dist package.

Requires ESM and Node.js v22 or greater. (These are requirements from Mozilla's pdf-dist package itself.)

Install

npm install pdf-text-reader

Usage

  • Read all pages into a single string with readPdfText:

    ```TypeScript import {readPdfText} from 'pdf-text-reader';

    async function main() { const pdfText: string = await readPdfText({url: 'path/to/pdf/file.pdf'}); console.info(pdfText); }

    await main(); ```

  • Read a PDF into individual pages with readPdfPages:

    ```TypeScript import {readPdfPages} from 'pdf-text-reader';

    async function main() { const pages = await readPdfPages({url: 'path/to/pdf/file.pdf'}); console.info(pages[0]?.lines); }

    await main(); ```

See the types for detailed argument and return value types.

Details

This package simply reads the output of pdfjs.getDocument and sorts it into lines based on text position in the document. It also inserts spaces for text on the same line that is far apart horizontally and new lines in between lines that are far apart vertically.

Example:

The text below in a PDF will be read as having spaces in between them even if the space characters aren't in the PDF.

cell 1               cell 2                 cell 3

The number of spaces to insert is calculated by an extremely naive but very simple calculation of Math.ceil(distance-between-text/text-height).

Low Level Control

If you need lower level parsing control, you can also use the exported parsePageItems function. This only reads one page at a time as seen below. This function is used by readPdfPages so the output will be identical for the same pdf page.

You may need to independently install the pdfjs-dist npm package for this to work.

import * as pdfjs from 'pdfjs-dist';
import type {TextItem} from 'pdfjs-dist/types/src/display/api.js';
import {parsePageItems} from 'pdf-text-reader';

async function main() {
    const doc = await pdfjs.getDocument('myDocument.pdf').promise;
    const page = await doc.getPage(1); // 1-indexed
    const content = await page.getTextContent();
    const items: TextItem[] = content.items.filter((item): item is TextItem => 'str' in item);
    const parsedPage = parsePageItems(items);
    console.info(parsedPage.lines);
}

await main();

Core symbols most depended-on inside this repo

readPdfPages
called by 4
src/read-pdf.ts
readPdfText
called by 2
src/read-pdf.ts
parsePageItems
called by 2
src/read-pdf.ts
combinePagesIntoSingleString
called by 1
src/read-pdf.ts
parsePage
called by 1
src/read-pdf.ts
main
called by 1
src/readme-examples/lower-level-controls.example.ts
main
called by 1
src/readme-examples/read-pdf-text.example.ts
main
called by 1
src/readme-examples/read-pdf-pages.example.ts

Shape

Function 10

Languages

TypeScript100%

Modules by API surface

src/read-pdf.ts5 symbols
src/read-pdf.test.ts2 symbols
src/readme-examples/read-pdf-text.example.ts1 symbols
src/readme-examples/read-pdf-pages.example.ts1 symbols
src/readme-examples/lower-level-controls.example.ts1 symbols

For agents

$ claude mcp add pdf-text-reader \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page