Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/electrovir/pdf-text-reader
Dead simple pdf text reader
https://github.com/electrovir/pdf-text-reader
npm pdf pdf-reader
Last synced: about 2 months ago
JSON representation
Dead simple pdf text reader
- Host: GitHub
- URL: https://github.com/electrovir/pdf-text-reader
- Owner: electrovir
- License: cc0-1.0
- Created: 2020-01-16T01:16:45.000Z (almost 5 years ago)
- Default Branch: dev
- Last Pushed: 2024-05-08T14:27:55.000Z (8 months ago)
- Last Synced: 2024-10-16T10:41:36.466Z (2 months ago)
- Topics: npm, pdf, pdf-reader
- Language: TypeScript
- Homepage: https://electrovir.github.io/pdf-text-reader/
- Size: 1.27 MB
- Stars: 30
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE-CC0
Awesome Lists containing this project
README
# PDF Text Reader
Dead simple PDF text reader for Node.js. Uses Mozilla's [`pdfjs-dist`](https://www.npmjs.com/package/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);
}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);
}main();
```See [the types](https://github.com/electrovir/pdf-text-reader/tree/master/src/read-pdf.ts) 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`](https://www.npmjs.com/package/pdfjs-dist) npm package for this to work.
```TypeScript
import * as pdfjs from 'pdfjs-dist';
import type {TextItem} from 'pdfjs-dist/types/src/display/api';
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);
}main();
```