Page-level text extraction for the tier-1 pdf reader — content-stream order, minimal graphics state.
Module oconvPdfTextExtract (oconvPdfTextExtract) | Source packages/front/office/oconv/src/read/pdf/text-extract.js | Deps pdfPageText, pdfFilterDispatch, oconvPdfImagePng | Worker-safe yes
@awacloud/pdf's page walk (pdfPageText.extractPage: the content streams decoded and parsed, the text and graphics state kept — CTM via q/Q/cm, text matrix via BT/Td/TD/Tm/T*/TL, current font via Tf, character spacing Tc, word spacing Tw, horizontal scaling Tz —, the Form XObjects executed, character decode and glyph widths by pdfFontDecoder), with this reader's own items built on its visit hook: the image draws and, under opts.rules, the rules, merged in content-stream order among the walk's text pieces and losses. Positioning is used only to place each piece — no layout inference.
Resolve
import { ModuleRuntime } from '@awacloud/fw/core/runtime.js';
import { fw_require, modules } from '@awacloud/oconv';
const runtime = new ModuleRuntime();
runtime.registerAll(fw_require);
runtime.registerAll(modules);
const { extractPage } = runtime.resolve('oconvPdfTextExtract');
API
| Member | Signature | Returns | Throws |
|---|---|---|---|
extractPage |
(page: object, resolve: (ref) => object, opts?: {formOpBudget?: number, rules?: boolean, images?: boolean, cellKerns?: boolean}) => {items, counts, losses} |
items — {kind:'text', x, y, fontSize, xEnd, text, mcid, bold}[] / {kind:'image', mcid}[] (with opts.images, a placeable picture's also resource, name, bytes, contentType, masked) / with opts.rules, {kind:'rule', x0, y0, x1, y1}[], content-stream order; counts — {operators, decoded, undecodable}; losses — {code, detail}[] |
oconv: bad form op budget |
Examples
Extract one page's positioned text
const pdf = runtime.resolve('pdf');
const doc = pdf.read(bytes);
const { items, counts } = extractPage(doc.pages[0], doc._raw.resolve);
items[0]; // { kind: 'text', x, y, fontSize, xEnd, text, mcid, bold }
counts; // { operators, decoded, undecodable }
Notes
x/yis where a text piece starts andxEndwhere it ends, both in the page's device space (after the CTM). Each show operator advances the text matrix by the displacement of the string it shows (ISO 32000-2 §9.4.4): Σ ((w0 / 1000) × Tfs + Tc + (Tw when the code is the single byte 32)) × Th, where w0 is the glyph width from the font (pdfFontDecoder'swidth), Tfs theTfsize and Th =Tz/ 100. Each number n in aTJarray moves the position by −n / 1000 × Tfs × Th. So aTjthat follows another without aTdstarts where the previous one ended.'moves to the next line before showing;"also sets Tw and Tc from its first two operands. The font and its size, the leading,Tc,TwandTzbelong to the graphics state:q/Qsave and restore them with the CTM (ISO 32000-2 §8.4.1), so text after aq … Tf … Qis shown in the font set before theq; aQwith nothing saved keeps the font in force.- A vertical-mode composite font (a
…-VCMap) and a show under an unresolved font advance nothing: the piece'sxEndequals itsx. - Inside one
TJarray, a number n with −n / 1000 > 0.15 (a move to the right of more than 0.15 em — the same threshold as the paragraph grouping's word gap between two pieces) is read as a word space: one' 'joins the strings on either side of it, unless the text before it already ends, or the text after it already starts, with whitespace. A space is never doubled. opts.cellKerns: true(opt-in) cuts a table row drawn as oneTJ: a number n with −n / 1000 > 1 (a move to the right of more than 1 em), with a drawn string before and after it, is a cell gap; an array with 2 such gaps or more gives one piece per part, cut at them, each starting where its first string is drawn. Without the option, or with fewer gaps, the array is one piece as above.- Between two separate pieces on one line, the word space is decided later, by
oconvPdfToIr's paragraph grouping, from thisxEndand the next piece'sx. opts.images: true(opt-in) carries a placeable picture's bytes on its item and records no loss for it: an indirect image XObject drawn byDo(inside a form too), not an/ImageMask, whose last filter isDCTDecodeorJPXDecode(abbreviations resolved) and no earlier one, whose/Decode(if any) is the default (0 1per pair), and whose decoded bytes — the filters before it applied, the last passed through — are a JPEG file (FF D8 FF) or a JP2 file (its signature box). The item gainsresource(the resource name),name(image-<obj>.jpg/image-<obj>.jp2,-<gen>before the extension when the generation is not 0),bytes,contentType(image/jpeg,image/jp2) andmasked(an/SMaskor a/Mask). A picture with no such last filter is placeable too whenoconvPdfImagePng.imageToPngwrites its samples as a PNG (contentTypeimage/png,nameimage-<obj>.png);maskedis then true only for a mask it could not apply. An inline image (BI … ID … EI) is placeable by the same rules: its dict's abbreviated keys (W,H,CS,BPC,F,DP,D,IM,I) and colour-space names (G,RGB,CMYK,I) are expanded, a colour-space name that is no device space is looked up in the resources'/ColorSpace, and it is namedimage-inline-<hash>.<ext>(the FNV-1a 32 of its bytes, 8 hex digits), so the same picture drawn twice is one name; itsresourceisnull. Every other picture keeps the bare item and itsimage/dropped.opts.rules: true(opt-in) adds the page's rules, forms included: when a path is stroked, each straight segment within 0.5 of horizontal or vertical in device space and longer than 5 (a stroked rectangle gives its four sides); when it is filled, a rectangle thinner than 2 and longer than 5, along its centre line. A rule item is{kind:'rule', x0, y0, x1, y1}(x0 <= x1,y0 <= y1, device space), pushed when its path is painted;ndiscards a path; curves, diagonals and large fills give none. Without the option the items are unchanged.boldon each item is the current font's (pdfFontDecoder'sbold: a bold weight in its name or descriptor).mcidon each item is the innermost marked-content id in scope (BDC … EMC), ornull— the tagged fast path (oconvPdfStruct+oconvPdfToIr) keys struct elements to text through it.- A Form XObject drawn by
Dois executed in place (ISO 32000-2 §8.10.1). The graphics state is saved, the form's/Matrix(identity by default) is concatenated onto the CTM, and fonts and XObjects resolve from the form's own/Resources, or from the page's when the form has none. The form's content stream then runs through the same operator walk, so its text and image draws enteritemsin content-stream order, placed and advanced under the form's matrix. The state is restored on return, and an unbalancedQorEMCinside a form never reaches past the form's entry state. Themcidin scope at theDocarries into the form. - A form's
/Resourcesresolves an indirect category through the document resolver. If the dictionary as a whole still fails to type, each category is typed on its own, so one bad category never drops the form's fonts. - Three guards keep form execution finite, and none throws: a form that is already being executed is not re-entered (
xobject/form-cycle); forms nest at most 12 deep, and a 13th level recordsxobject/form-depth; once a page has runformOpBudgetoperators inside forms (default 1,000,000, the named constantFORM_OPS_MAX), its later form draws are skipped andxobject/form-budgetis recorded once for the page. formOpBudgetis validated at entry, before any stream is decoded: absent (undefined) resolves to the default; present, it must satisfyNumber.isSafeInteger(v) && v >= 1, elseextractPagethrowsoconv: bad form op budget— there is no upper cap other than the safe-integer range.oconvPdfToIr.pdfToIrapplies the identical rule at entry (so a zero-page document still refuses a bad value) and forwards the option to every page'sextractPagecall.- A content stream that fails to decode or parse is skipped with loss
content/undecodable, detail =stream <objNum>: <cause>(the thrown error's message, collapsed to one line and capped at 160 characters) — the page's other streams still contribute. - A
Tfnaming a font absent from/Resourcesor whose dict fails to type leavescurDecodernull; the next show operator counts its whole run as undecodable and recordstext/font-unresolved. - Page resources come from
pdfResources.resolvePageResources(page.raw, resolve)with the document's own resolver, so an indirect/Resources, an indirect resource category (/Font 12 0 R,/ExtGState 9 0 R) and font dicts compressed in object streams all resolve. If the resource dictionary still fails to type (a category that is neither a dictionary nor a reference), the page has no resource map at all and every show run on it recordstext/font-unresolved— ledgered, never guessed. - Loss codes this module contributes:
text/undecodable(≥1 char code in a show run resolved to no Unicode, detail = the count),text/font-unresolved,text/width-approximated(a font's glyph widths could not be read from the font, so a declared fallback width placed its text — recorded once per font resource per page, detail = the resource name; the text is kept, only its end positions and the inferred word spaces are approximate),image/dropped(an image XObjectDoor inline imageBI, inside a form too — tier 1 keeps no images; a placeable picture underopts.imagesexcepted; one entry per draw, its detail the picture:<name> (<num> <gen> R, <w>x<h>)— the resource name, the object, the/Width×/Height(a direct object without the reference, the size left out when not integers) —inline (<w>x<h>)orinlinefor an inline image,xobjectfor aDowithout a name operand, the bare name when the resources lack it or it is no image),xobject/form-dropped(a Form XObject whose stream cannot be used, so its text is not extracted; detail =<name>: <reason>, the reason one ofnot a stream,undecodable stream,unparsable stream),xobject/form-cycle,xobject/form-depthandxobject/form-budget(a budget offormOpBudgetoperators, default 1,000,000; the three guards below; detail = the resource name).
See also
- docs/api/README.md — full module index +
exportsboundary note @awacloud/pdf'spdfPageTextandpdfFontDecoder— the page walk and the per-font decoder this module is built onoconvPdfStruct— the tagged-structure companion this page'smcids key intooconvPdfToIr— the reader composing the pdf pipeline- loss matrix