extract-text
PDF to text
Take the words out of a PDF, in the order they are read.
- Reads
- a PDF — application/pdf
- Writes
- one file — text files or JSON
- Tool page
- /pdf-to-text
Example
Read the words off a document so a script can search them.
Given
- report.pdf — a PDF of written text
From code
import { loadDocument, runOperation } from '@pagecraft/core';
const report = await loadDocument({ bytes, name: 'report.pdf' });
const [output] = await runOperation('extract-text', [report], {
format: 'plain',
});
From a shell
pagecraft extract-text report.pdf --format plain
What comes back
One text file, containing “Quarterly report”.
This example is run by the test suite. If it stopped being true, the build would fail.
Options
pages--pages
string, a page selection: '1-3, 7, 9-', 'even', 'last'
Pages
Which pages to take the text from.
Default: 'all'
format--format
string, one of
As
Default: 'plain'
plain— The words, page by page.json— Every run of text with the rectangle it occupies, for a tool to work with.
dehyphenate--dehyphenate
boolean
Rejoin split words
Put a word back together when the line it was set on broke it in half.
Default: true
What it cannot do
- A scanned page holds a picture of words, not words. Run OCR over it first, and this will read the layer OCR adds.
- Reading order is worked out from where the text sits on the page. It is right for columns, headings and tables, and it can be wrong on a page laid out like a poster.
- Columns are recognised from three lines or more of them. Two short columns side by side are read across, the way a table is.
- Text drawn sideways — a rotated watermark, a turned table header — is kept whole and read after the upright text, rather than being folded into a line it crosses.