Skip to content

Latest commit

 

History

History
167 lines (126 loc) · 11.5 KB

text-output.md

File metadata and controls

167 lines (126 loc) · 11.5 KB

pdfintake text: the output format

pdfintake text gives back the text layer a PDF already has, one page at a time, so a search index or a script never has to pull the text out itself. It reads each page's text from its CropBox the same way verify's text_retention check does after every build, so it gives exactly what pdfintake verified. It never reads a page with OCR, never changes or writes a PDF, and writes no record. A PDF without a text layer gets one from pdfintake run first.

This page is the format's contract: every key, value and exit code below is checked against the code by tests/test_text.py. The code is src/pdfintake/pdf/layertext.py (the export) and cli._cmd_text (the command).

The command

pdfintake text INPUT [-o FILE|-] [--format jsonl|txt] [--pages N|N-M] [--record RECORD-ID] [--overwrite] [--json]
Flag What it does
INPUT One PDF. An image is refused (unsupported_input, exit 2): pdfintake run makes a searchable PDF of it first.
-o FILE, --out FILE Write the export to FILE instead of standard output. -o - is standard output.
--format jsonl|txt jsonl (the default): a header line, then one JSON line per page. txt: plain text with a form feed after each page.
--pages N|N-M Only page N, or pages N to M, counting from 1. A range the PDF doesn't have is bad_pages (exit 2).
--record RECORD-ID The pdfintake run that wrote this PDF. Its languages, packs, font-map outcome and page warnings are added.
--overwrite With -o FILE: replace FILE when it exists.
--json An error becomes one JSON object on standard output. With -o FILE, a summary is printed on success too.

JSON lines

--format jsonl, the default: one header line, then one line per page of the range, in page order. Every line is a JSON object in UTF-8, not ASCII-escaped, ending in \n.

{"format": "pdfintake-text", "format_version": 1, "source_sha256": "c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1", "tool_version": "0.5.1", "pages": 3, "range": [1, 3], "producer": {"tool": "pdfintake", "tool_version": "0.5.1", "route": "layer", "engines": {"pdfium": "156.0.8076.0", "pypdfium2": "5.14.0"}, "packs": []}, "layers": {"pdfintake": 2, "other": 0, "none": 1}, "font_maps": "record", "warnings": [], "record": {"id": "r-20261105-1a2b3c4d", "tool": {"version": "0.5.1", "platform": "darwin-arm64", "os": "macOS 15.4", "python": "3.12.7", "engines": {"tesseract": "5.5.4 (tesserocr 2.13.0)", "vision": "rev3 (macOS 15.4)", "rapidocr": "3.10.0"}}, "language": {"plan": "confident", "packs": [{"id": "de", "version": "1.0.0"}]}}}
{"page": 1, "text": "Rechnung Nr. 555-0123\nJane Doe", "chars": 30, "layer": "pdfintake", "flags": [], "lang": ["de"], "lang_basis": "detected", "pack": "de"}
{"page": 2, "text": "Seite 2", "chars": 7, "layer": "pdfintake", "flags": ["agreement_low"], "lang": ["de"], "lang_basis": "detected", "pack": "de"}
{"page": 3, "text": "", "chars": 0, "layer": "none", "flags": [], "lang": null, "lang_basis": null, "pack": null}

Header keys

Key Type Meaning
format string Always pdfintake-text.
format_version integer 1. See Compatibility for when it changes.
source_sha256 string The SHA-256 of the PDF as read, 64 lowercase hex digits. The file is sealed before the read and checked again after it, so the text is this file's.
tool_version string pdfintake's version.
pages integer The document's page count.
range array The pages exported, [first, last], counting from 1, both included.
producer object What turned the PDF's bytes into this text (below).
layers object How many exported pages have each layer value: {"pdfintake": n, "other": n, "none": n}.
font_maps string What decided which pages' fonts can't be mapped to text: record, checked or failed (below).
warnings array Document-level warnings, each {code, message, page}. Today only no_text_layer (below).
record object or null What --record added (below); null without it.

producer

Key Type Meaning
tool string Always pdfintake.
tool_version string pdfintake's version, as in the header.
route string Always layer: the text is the PDF's own layer, never OCR.
engines object The pdfium build that read the layer: {"pdfium": "…", "pypdfium2": "…"}.
packs array Always []: no language pack takes part in reading a layer.

A caller that keys its stored text by producer can hash this object as it is: a new pdfintake version or pdfium build gives a new producer.

Page keys

Key Type Meaning
page integer The page number, counting from 1.
text string The page's text (below). "" for a page with no non-space character.
chars integer The length of text, in Unicode code points.
layer string pdfintake for a layer pdfintake wrote, other for any other text (the document's own, or another tool's invisible layer), none for a page with no text.
flags array Codes saying the page's text may be missing or wrong (below), in a fixed order.
lang array or null The page's languages, from the record, when that run read the page with OCR; null otherwise.
lang_basis string or null How lang was decided: forced, hinted or detected; null when lang is.
pack string or null The language pack that run read the page with; null otherwise.

text is pdfium's text with every line break as \n, pdfium's mark for a hyphen that ends a line written back as - and a line break, any lone surrogate as U+FFFD, and the whole in NFC.

record

With --record RECORD-ID, the record's output.sha256 must be this PDF's, or text stops with exit 5 (record_mismatch). The header's record then holds:

Key Type Meaning
id string The record id.
tool object The record's tool: the version, platform, OS, Python and engine versions of that run (decision-record.md).
language object {"plan": …, "packs": [{"id": …, "version": …}]}: that run's language plan and the packs it used.

The record also gives each page it read with OCR its lang, lang_basis and pack, decides which pages' fonts were only partly repaired, and adds its page warnings to flags.

Flags

flags lists codes from record.TEXT_FLAGS, in this order:

Code Where it comes from What it means
no_text_layer the PDF The page holds pictures and no text: run pdfintake run on the PDF first.
garbled_text the PDF Letters whose font has no map to text, so they read as something else.
font_map_partial --record That run's font-map repair mapped only some of the page's letters.
image_text_not_read the PDF Printed text over pictures; the pictures weren't read.
layer_dropped_chars --record That run reported this warning on the page.
agreement_low --record That run reported this warning on the page.
fallback_used --record That run reported this warning on the page.
no_layer_readers_failed --record That run reported this warning on the page.
language_uncertain --record That run reported this warning on the page.
no_pack_for_language --record That run reported this warning on the page.
pack_not_installed --record That run reported this warning on the page.
pack_broken --record That run reported this warning on the page.
mixed_language_page --record That run reported this warning on the page.
mixed_script_no_pack --record That run reported this warning on the page.
orientation_disagree --record That run reported this warning on the page.
not_turned_annotations --record That run reported this warning on the page.
render_capped --record That run reported this warning on the page.

The warnings a record carries are explained in decision-record.md.

font_maps

  • record: the record's fontmap step decided.
  • checked: without a record, text runs the check run starts its font-map repair from, and flags every page it finds garbled_text. A partial repair can't be told apart in the file, so font_map_partial needs the record.
  • failed: the check couldn't run on this file, and garbled_text then comes from the share of unreadable characters alone.

warnings

no_text_layer (as {"code": "no_text_layer", "message": "…", "page": null}) when no exported page has text and at least one holds pictures: the PDF needs pdfintake run first. A PDF of blank pages gets no warning. Without --json, warnings also go to standard error when the text goes to standard output.

Plain text

--format txt: each page's text, ending in a line break when it has any, then a form feed (\f), as pdftotext writes it. So page N of the range is the Nth piece of a split on \f.

Where the text goes

Standard output by default (-o - too), written only after every check has passed, so a failure never leaves half an export. -o FILE writes the export to a file the caller names. It's built in the cache and copied there by the same writer run uses: never over an existing file without --overwrite, never onto the input, never into the cache.

The --json summary

With -o FILE --json, success prints one JSON object on standard output. It never holds text.

Key Meaning
exit 0.
input {path, sha256, pages} of the PDF.
range The pages exported, [first, last].
format jsonl or txt.
output {path, sha256, bytes} of the file written.
layers As in the header.
font_maps As in the header.
flags Each flag code with the page numbers that carry it, {"garbled_text": [2, 5]}.
warnings As in the header.
record The record id given with --record, or null.
notes [{"code": "input_unchanged", "message": "…"}].

An error under --json is {"exit": N, "error": {"code": …, "message": …}}, with refusal ({code, message}) added for exit 4.

Exit codes

Exit Meaning Codes
0 Done, including a PDF without a text layer.
1 An unexpected error. Its message names only the kind of error, never a page's text. internal_error
2 Usage: the input is missing, isn't a regular file, or is an image or not a PDF at all; a page range the PDF doesn't have; an output that exists or would be the input; a bad or unknown record id. input_missing, input_not_file, unsupported_input, bad_pages, output_exists, output_is_input, bad_record_id, unknown_record
4 Refused: the PDF needs a password to open (run writes a searchable copy without one when a saved password opens it), or can't be read as a PDF. encrypted, damaged
5 Stale: the PDF isn't the record's output, or it changed while it was read. record_mismatch, input_changed

text never exits 3: it needs no language pack and no engine.

Compatibility

format_version 1's keys are never removed or renamed, and what they mean never changes. A new key can be added without changing the version, so a reader ignores keys it doesn't know. Removing a key, or changing what one means, is format version 2. A new flag code can be added to the list; a reader that meets one it doesn't know treats the page's text as doubtful.