doc-data-extract
v1.0.1
Published
Extract text and structured fields from PDF and text files from the command line
Maintainers
Readme
doc-data-extract
A small, script-friendly CLI and Node.js library for extracting text and structured fields from PDFs and text files.
Quick start
npx doc-data-extract report.pdf
npx doc-data-extract notes.txt --format json --pretty
npx doc-data-extract "documents/**/*.{pdf,txt}" --format ndjson > documents.ndjsonJSON output includes page-by-page text and extracted fields:
{
"source": "/path/to/invoice.pdf",
"type": "pdf",
"text": "Complete document text...",
"pages": [
{
"number": 1,
"text": "Text from page one...",
"fields": { "invoice": "INV-42" }
}
],
"metadata": { "pages": 1 },
"fields": { "invoice": "INV-42" }
}For plain-text inputs, pages contains one page.
Extract structured values with JavaScript regular expressions:
npx doc-data-extract invoice.pdf --format json \
--field 'invoice=Invoice(?: No)?:\s*(\S+)' \
--field 'total=Total:\s*\$?([0-9,.]+)'The first capture group is returned. You can instead use a named capture group called value. Missing fields are null.
Inputs
Pass one or more files, directories, or glob patterns. Directories are searched recursively for .pdf, .txt, .text, .md, .markdown, .csv, and .log files. Use - to read PDF or text data from stdin.
cat report.pdf | npx doc-data-extract -
npx doc-data-extract ./documents --format json --output extracted.json
npx doc-data-extract report.pdf --pages 1,3-5Run npx doc-data-extract --help for every option.
Node.js API
import { extractFile, extractFields, extractPdf, extractText } from 'doc-data-extract';
const document = await extractFile('./invoice.pdf', {
pages: [1, 2],
fields: [{ name: 'invoice', pattern: 'Invoice: (\\S+)' }],
});
console.log(document.text, document.fields);Requires Node.js 20.16 or newer.
Limitations
Scanned/image-only PDFs need OCR and are not supported. PDF text order depends on how text is encoded in the source document.
License
MIT
