@shuji-bonji/pdf-reader-mcp 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +61 -0
- package/README.ja.md +37 -7
- package/README.md +40 -9
- package/dist/schemas/tier2.d.ts +18 -0
- package/dist/schemas/tier2.d.ts.map +1 -1
- package/dist/schemas/tier2.js +13 -0
- package/dist/schemas/tier2.js.map +1 -1
- package/dist/services/pdfjs-service.d.ts +77 -20
- package/dist/services/pdfjs-service.d.ts.map +1 -1
- package/dist/services/pdfjs-service.js +111 -161
- package/dist/services/pdfjs-service.js.map +1 -1
- package/dist/services/struct-tree-service.d.ts +167 -0
- package/dist/services/struct-tree-service.d.ts.map +1 -0
- package/dist/services/struct-tree-service.js +584 -0
- package/dist/services/struct-tree-service.js.map +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +2 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/tier1/read-text.d.ts.map +1 -1
- package/dist/tools/tier1/read-text.js +3 -1
- package/dist/tools/tier1/read-text.js.map +1 -1
- package/dist/tools/tier1/search-text.d.ts.map +1 -1
- package/dist/tools/tier1/search-text.js +12 -1
- package/dist/tools/tier1/search-text.js.map +1 -1
- package/dist/tools/tier2/extract-structured-text.d.ts +6 -0
- package/dist/tools/tier2/extract-structured-text.d.ts.map +1 -0
- package/dist/tools/tier2/extract-structured-text.js +82 -0
- package/dist/tools/tier2/extract-structured-text.js.map +1 -0
- package/dist/tools/tier2/extract-tables.d.ts.map +1 -1
- package/dist/tools/tier2/extract-tables.js +15 -9
- package/dist/tools/tier2/extract-tables.js.map +1 -1
- package/dist/tools/tier2/inspect-tags.js +1 -1
- package/dist/tools/tier2/inspect-tags.js.map +1 -1
- package/dist/types.d.ts +66 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/formatter.d.ts +20 -1
- package/dist/utils/formatter.d.ts.map +1 -1
- package/dist/utils/formatter.js +93 -1
- package/dist/utils/formatter.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structure tree walker.
|
|
3
|
+
*
|
|
4
|
+
* Walks the document's logical structure hierarchy from the catalog's
|
|
5
|
+
* `StructTreeRoot`, depth-first — which is exactly how ISO 32000-2 §14.8.2.5
|
|
6
|
+
* defines logical content order:
|
|
7
|
+
*
|
|
8
|
+
* > Logical content order – the ordering for semantic purposes – shall be
|
|
9
|
+
* > defined by a depth-first traversal of the document's logical structure
|
|
10
|
+
* > hierarchy.
|
|
11
|
+
*
|
|
12
|
+
* ## Why not `page.getStructTree()`?
|
|
13
|
+
*
|
|
14
|
+
* pdfjs only offers a per-page view. Merging those per page — which is what
|
|
15
|
+
* `extract_tables` and `inspect_tags` do — cannot produce logical content order,
|
|
16
|
+
* because §14.8.2.5 NOTE 2 allows a single logical object to span pages:
|
|
17
|
+
*
|
|
18
|
+
* > A logical object can extend over more than one PDF page …
|
|
19
|
+
*
|
|
20
|
+
* Measured on `tests/fixtures/structured.pdf` (one paragraph and one list, each
|
|
21
|
+
* split across two pages):
|
|
22
|
+
*
|
|
23
|
+
* | | this walker | per-page merge |
|
|
24
|
+
* |---|---|---|
|
|
25
|
+
* | `Document` | 1 | 2 (duplicated per page) |
|
|
26
|
+
* | `L` | 1 (with 2 `LI`) | 2 (with 1 `LI` each) |
|
|
27
|
+
*
|
|
28
|
+
* pdfjs's per-page nodes carry no element identity (`role` + `children` only),
|
|
29
|
+
* so once merged there is no way to recover that page 1's `L` and page 2's `L`
|
|
30
|
+
* are the same list. Walking the document tree keeps them whole.
|
|
31
|
+
*
|
|
32
|
+
* ## Division of labour
|
|
33
|
+
*
|
|
34
|
+
* This module uses pdf-lib and returns *structure* plus the marked-content
|
|
35
|
+
* references that locate the text. It does not read text — resolving MCIDs to
|
|
36
|
+
* strings needs pdfjs, and lives in `pdfjs-service.ts`. Structure from pdf-lib,
|
|
37
|
+
* text from pdfjs.
|
|
38
|
+
*/
|
|
39
|
+
import { PDFArray, PDFDict, PDFHexString, PDFName, PDFNumber, PDFRef, PDFString, } from 'pdf-lib';
|
|
40
|
+
import { resolvePageNumbers } from '../utils/pdf-helpers.js';
|
|
41
|
+
import { buildDocumentIdToTextMap, compactCellText, LINE_BREAK, loadDocument, resolveLineBreaks, } from './pdfjs-service.js';
|
|
42
|
+
import { loadWithPdfLib } from './pdflib-service.js';
|
|
43
|
+
/**
|
|
44
|
+
* Build the pdfjs marked-content id for a `/Pg` + `/MCID` pair.
|
|
45
|
+
*
|
|
46
|
+
* pdfjs names marked content `p{pageObjectNumber}R_mc{mcid}` — verified against
|
|
47
|
+
* pdfjs-dist in `tests/tier1/struct-tree-service.test.ts`. Note it drops the
|
|
48
|
+
* generation number (`p7R`, not `p7_0R`).
|
|
49
|
+
*
|
|
50
|
+
* This format is pdfjs's internal convention, not a published contract, so the
|
|
51
|
+
* test asserts it against real pdfjs output: if a pdfjs upgrade renames these,
|
|
52
|
+
* the test fails rather than the tool silently returning empty text. (D-2 was
|
|
53
|
+
* caused by hardcoding pdfjs constants without such a guard.)
|
|
54
|
+
*/
|
|
55
|
+
export function pdfjsMarkedContentId(ref) {
|
|
56
|
+
return `p${ref.pageObjNum}R_mc${ref.mcid}`;
|
|
57
|
+
}
|
|
58
|
+
/** Resolve a value that may be an indirect reference. */
|
|
59
|
+
function deref(doc, obj) {
|
|
60
|
+
return obj instanceof PDFRef ? doc.context.lookup(obj) : obj;
|
|
61
|
+
}
|
|
62
|
+
/** Read a text-ish value (`PDFString` / `PDFHexString`) from a dictionary. */
|
|
63
|
+
function textEntry(doc, dict, key) {
|
|
64
|
+
const value = deref(doc, dict.get(PDFName.of(key)));
|
|
65
|
+
if (value instanceof PDFString || value instanceof PDFHexString)
|
|
66
|
+
return value.decodeText();
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
/** Normalise `/K` — it may be absent, a single object, or an array. */
|
|
70
|
+
function kidsOf(doc, dict) {
|
|
71
|
+
const k = deref(doc, dict.get(PDFName.of('K')));
|
|
72
|
+
if (k === undefined || k === null)
|
|
73
|
+
return [];
|
|
74
|
+
if (k instanceof PDFArray)
|
|
75
|
+
return k.asArray();
|
|
76
|
+
return [k];
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Walk one structure element.
|
|
80
|
+
*
|
|
81
|
+
* `/K` is polymorphic (§14.7.2 Table 355) and every form has to be handled:
|
|
82
|
+
*
|
|
83
|
+
* - **integer** — an MCID on the element's own (possibly inherited) `/Pg`
|
|
84
|
+
* - **MCR dict** — an MCID with its own `/Pg`, which is how an element points
|
|
85
|
+
* at content on a page other than its own. This is the page-spanning case.
|
|
86
|
+
* - **OBJR dict** — a reference to an annotation or form field. Skipped: it
|
|
87
|
+
* owns no page text.
|
|
88
|
+
* - **StructElem dict** — a child element, recursed into
|
|
89
|
+
*
|
|
90
|
+
* `/Pg` is inherited: an element without one uses its nearest ancestor's.
|
|
91
|
+
*/
|
|
92
|
+
function walkElement(doc, dict, inheritedPg) {
|
|
93
|
+
const s = deref(doc, dict.get(PDFName.of('S')));
|
|
94
|
+
if (!(s instanceof PDFName))
|
|
95
|
+
return null;
|
|
96
|
+
const pgValue = dict.get(PDFName.of('Pg'));
|
|
97
|
+
const pg = pgValue instanceof PDFRef ? pgValue : inheritedPg;
|
|
98
|
+
const element = {
|
|
99
|
+
role: s.decodeText(),
|
|
100
|
+
actualText: textEntry(doc, dict, 'ActualText'),
|
|
101
|
+
alt: textEntry(doc, dict, 'Alt'),
|
|
102
|
+
lang: textEntry(doc, dict, 'Lang'),
|
|
103
|
+
contentRefs: [],
|
|
104
|
+
children: [],
|
|
105
|
+
};
|
|
106
|
+
for (const kid of kidsOf(doc, dict)) {
|
|
107
|
+
// An MCID written directly, on this element's page.
|
|
108
|
+
if (kid instanceof PDFNumber) {
|
|
109
|
+
if (pg)
|
|
110
|
+
element.contentRefs.push({ pageObjNum: pg.objectNumber, mcid: kid.asNumber() });
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const resolved = deref(doc, kid);
|
|
114
|
+
if (!(resolved instanceof PDFDict))
|
|
115
|
+
continue;
|
|
116
|
+
const type = resolved.get(PDFName.of('Type'));
|
|
117
|
+
const typeName = type instanceof PDFName ? type.decodeText() : null;
|
|
118
|
+
if (typeName === 'MCR') {
|
|
119
|
+
const mcid = deref(doc, resolved.get(PDFName.of('MCID')));
|
|
120
|
+
const mcrPgValue = resolved.get(PDFName.of('Pg'));
|
|
121
|
+
const mcrPg = mcrPgValue instanceof PDFRef ? mcrPgValue : pg;
|
|
122
|
+
if (mcid instanceof PDFNumber && mcrPg) {
|
|
123
|
+
element.contentRefs.push({ pageObjNum: mcrPg.objectNumber, mcid: mcid.asNumber() });
|
|
124
|
+
}
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
// OBJR points at an annotation or form field, which carries no page text.
|
|
128
|
+
if (typeName === 'OBJR')
|
|
129
|
+
continue;
|
|
130
|
+
// Anything with an /S is a child structure element. Checking /S rather than
|
|
131
|
+
// Type == StructElem on purpose: Type is optional in practice and plenty of
|
|
132
|
+
// producers omit it.
|
|
133
|
+
if (resolved.get(PDFName.of('S'))) {
|
|
134
|
+
const child = walkElement(doc, resolved, pg);
|
|
135
|
+
if (child)
|
|
136
|
+
element.children.push(child);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return element;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Walk the document's structure tree from the catalog.
|
|
143
|
+
*
|
|
144
|
+
* Returns the top-level structure elements in document order, or `null` when the
|
|
145
|
+
* catalog has no `StructTreeRoot` (an untagged document, or one whose structure
|
|
146
|
+
* tree is unreachable).
|
|
147
|
+
*
|
|
148
|
+
* Note this reads only `StructTreeRoot`; it needs neither `/ParentTree` nor
|
|
149
|
+
* `/StructParents`, which pdfjs's per-page `getStructTree()` does require.
|
|
150
|
+
*/
|
|
151
|
+
export function walkStructTree(doc) {
|
|
152
|
+
const root = deref(doc, doc.catalog.get(PDFName.of('StructTreeRoot')));
|
|
153
|
+
if (!(root instanceof PDFDict))
|
|
154
|
+
return null;
|
|
155
|
+
const elements = [];
|
|
156
|
+
for (const kid of kidsOf(doc, root)) {
|
|
157
|
+
const resolved = deref(doc, kid);
|
|
158
|
+
if (resolved instanceof PDFDict && resolved.get(PDFName.of('S'))) {
|
|
159
|
+
const element = walkElement(doc, resolved, undefined);
|
|
160
|
+
if (element)
|
|
161
|
+
elements.push(element);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return elements;
|
|
165
|
+
}
|
|
166
|
+
/** Collect every content reference under an element, in document order. */
|
|
167
|
+
export function collectContentRefs(element, into = []) {
|
|
168
|
+
into.push(...element.contentRefs);
|
|
169
|
+
for (const child of element.children)
|
|
170
|
+
collectContentRefs(child, into);
|
|
171
|
+
return into;
|
|
172
|
+
}
|
|
173
|
+
// ─── Structured text (M-8) ───────────────────────────────
|
|
174
|
+
/**
|
|
175
|
+
* Roles whose children are structural scaffolding rather than prose, and whose
|
|
176
|
+
* text therefore should not be rolled up from descendants.
|
|
177
|
+
*
|
|
178
|
+
* `Table` gets `rows` instead (it is two-dimensional; `depth` cannot express
|
|
179
|
+
* "row 2, column 3"), and `L` / `LI` have their items emitted separately.
|
|
180
|
+
*/
|
|
181
|
+
const CONTAINER_ROLES = new Set([
|
|
182
|
+
'Document',
|
|
183
|
+
'Part',
|
|
184
|
+
'Art',
|
|
185
|
+
'Sect',
|
|
186
|
+
'Div',
|
|
187
|
+
'Table',
|
|
188
|
+
'L',
|
|
189
|
+
'LI',
|
|
190
|
+
'TR',
|
|
191
|
+
]);
|
|
192
|
+
/** `Lbl` is a list label ("•", "1."), not body text — reported separately. */
|
|
193
|
+
const LABEL_ROLE = 'Lbl';
|
|
194
|
+
/**
|
|
195
|
+
* Assemble the text of an element from its marked content.
|
|
196
|
+
*
|
|
197
|
+
* `/ActualText` wins when present: ISO 32000-2 §14.9.4 says it "shall be used as
|
|
198
|
+
* a **replacement**, not a description, for the content". `/Alt` is deliberately
|
|
199
|
+
* NOT consulted here — it is a description of content that has no text (§14.9.3),
|
|
200
|
+
* so treating it as text would inject a caption into the body.
|
|
201
|
+
*
|
|
202
|
+
* When an element's content continues on another page, the page boundary is a
|
|
203
|
+
* line break in the original layout and is treated as one. pdfjs emits no EOL
|
|
204
|
+
* marker at the start of a page, so without this a paragraph split across pages
|
|
205
|
+
* extracts as "…page oneand continues…" — and for Japanese the same boundary
|
|
206
|
+
* must produce no space at all, which `resolveLineBreaks` decides.
|
|
207
|
+
*/
|
|
208
|
+
function textOf(element, idToText, includeDescendants) {
|
|
209
|
+
if (element.actualText !== null)
|
|
210
|
+
return element.actualText;
|
|
211
|
+
const refs = includeDescendants ? collectContentRefs(element) : element.contentRefs;
|
|
212
|
+
let assembled = '';
|
|
213
|
+
let previousPage;
|
|
214
|
+
for (const ref of refs) {
|
|
215
|
+
if (assembled !== '' && previousPage !== undefined && previousPage !== ref.pageObjNum) {
|
|
216
|
+
assembled += LINE_BREAK;
|
|
217
|
+
}
|
|
218
|
+
assembled += idToText.get(pdfjsMarkedContentId(ref)) ?? '';
|
|
219
|
+
previousPage = ref.pageObjNum;
|
|
220
|
+
}
|
|
221
|
+
return resolveLineBreaks(assembled);
|
|
222
|
+
}
|
|
223
|
+
/** Collapse whitespace runs and trim. Line breaks were already resolved upstream. */
|
|
224
|
+
function tidy(text) {
|
|
225
|
+
return text.replace(/[\s ]+/g, ' ').trim();
|
|
226
|
+
}
|
|
227
|
+
/** Pages an element's content lives on, in order of first appearance. */
|
|
228
|
+
function pagesOf(element, pageNumByObjNum) {
|
|
229
|
+
const seen = [];
|
|
230
|
+
for (const ref of collectContentRefs(element)) {
|
|
231
|
+
const pageNum = pageNumByObjNum.get(ref.pageObjNum);
|
|
232
|
+
if (pageNum !== undefined && !seen.includes(pageNum))
|
|
233
|
+
seen.push(pageNum);
|
|
234
|
+
}
|
|
235
|
+
return seen.sort((a, b) => a - b);
|
|
236
|
+
}
|
|
237
|
+
/** Build `rows` for a `Table` element by walking TR → TH/TD. */
|
|
238
|
+
function tableRows(element, idToText) {
|
|
239
|
+
const rows = [];
|
|
240
|
+
const visitRow = (row) => {
|
|
241
|
+
const cells = [];
|
|
242
|
+
for (const cell of row.children) {
|
|
243
|
+
if (cell.role === 'TH' || cell.role === 'TD') {
|
|
244
|
+
cells.push({ text: tidy(textOf(cell, idToText, true)), isHeader: cell.role === 'TH' });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (cells.length > 0)
|
|
248
|
+
rows.push(cells);
|
|
249
|
+
};
|
|
250
|
+
const visit = (node) => {
|
|
251
|
+
for (const child of node.children) {
|
|
252
|
+
if (child.role === 'TR')
|
|
253
|
+
visitRow(child);
|
|
254
|
+
// THead / TBody / TFoot wrap rows; TR may also sit directly under Table.
|
|
255
|
+
else
|
|
256
|
+
visit(child);
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
visit(element);
|
|
260
|
+
return rows;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Flatten the structure tree into logical content order.
|
|
264
|
+
*
|
|
265
|
+
* Flat + `depth` rather than nested: a depth-first pre-order plus depth encodes
|
|
266
|
+
* the tree exactly (it is an indented outline), the main consumer walks it
|
|
267
|
+
* linearly to emit Markdown, and it can be truncated at any point without
|
|
268
|
+
* producing broken JSON. `Table` is the exception — see `rows`.
|
|
269
|
+
*/
|
|
270
|
+
function flatten(element, depth, idToText, pageNumByObjNum, out) {
|
|
271
|
+
// A list label belongs to its LI, not to the flow of text.
|
|
272
|
+
if (element.role === LABEL_ROLE)
|
|
273
|
+
return;
|
|
274
|
+
const isContainer = CONTAINER_ROLES.has(element.role);
|
|
275
|
+
const isTable = element.role === 'Table';
|
|
276
|
+
const entry = {
|
|
277
|
+
role: element.role,
|
|
278
|
+
depth,
|
|
279
|
+
text: null,
|
|
280
|
+
pages: pagesOf(element, pageNumByObjNum),
|
|
281
|
+
};
|
|
282
|
+
if (isTable) {
|
|
283
|
+
entry.rows = tableRows(element, idToText);
|
|
284
|
+
}
|
|
285
|
+
else if (!isContainer) {
|
|
286
|
+
// A leaf rolls up its descendants' text (e.g. LBody under LI, Span under P).
|
|
287
|
+
const text = tidy(textOf(element, idToText, true));
|
|
288
|
+
entry.text = text === '' ? null : text;
|
|
289
|
+
}
|
|
290
|
+
if (element.role === 'LI') {
|
|
291
|
+
const label = element.children.find((c) => c.role === LABEL_ROLE);
|
|
292
|
+
if (label) {
|
|
293
|
+
const labelText = tidy(textOf(label, idToText, true));
|
|
294
|
+
if (labelText)
|
|
295
|
+
entry.label = labelText;
|
|
296
|
+
}
|
|
297
|
+
// The LI's text is its LBody; emit it here so consumers get one entry per item.
|
|
298
|
+
const body = element.children.filter((c) => c.role !== LABEL_ROLE);
|
|
299
|
+
if (body.length > 0) {
|
|
300
|
+
const text = tidy(body.map((b) => textOf(b, idToText, true)).join(''));
|
|
301
|
+
entry.text = text === '' ? null : text;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (element.alt !== null)
|
|
305
|
+
entry.alt = element.alt;
|
|
306
|
+
if (element.lang !== null)
|
|
307
|
+
entry.lang = element.lang;
|
|
308
|
+
out.push(entry);
|
|
309
|
+
// Table rows and LI bodies are already represented; don't emit them again.
|
|
310
|
+
if (isTable || element.role === 'LI')
|
|
311
|
+
return;
|
|
312
|
+
for (const child of element.children) {
|
|
313
|
+
flatten(child, depth + 1, idToText, pageNumByObjNum, out);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Turn a walked structure tree into the flattened, text-bearing result.
|
|
318
|
+
*
|
|
319
|
+
* `pageNumByObjNum` maps page object numbers (as seen in `/Pg`) to 1-based page
|
|
320
|
+
* numbers, so the output speaks in page numbers rather than object numbers.
|
|
321
|
+
*/
|
|
322
|
+
export function buildStructuredText(roots, idToText, pageNumByObjNum, options = {}) {
|
|
323
|
+
const out = [];
|
|
324
|
+
for (const root of roots)
|
|
325
|
+
flatten(root, 0, idToText, pageNumByObjNum, out);
|
|
326
|
+
let filtered = out;
|
|
327
|
+
if (options.roles && options.roles.length > 0) {
|
|
328
|
+
const wanted = new Set(options.roles);
|
|
329
|
+
filtered = filtered.filter((e) => wanted.has(e.role));
|
|
330
|
+
}
|
|
331
|
+
if (options.pages && options.pages.length > 0) {
|
|
332
|
+
const wanted = new Set(options.pages);
|
|
333
|
+
// An element that touches the range at all is kept whole — splitting a
|
|
334
|
+
// page-spanning element is precisely what this tool exists to avoid.
|
|
335
|
+
filtered = filtered.filter((e) => e.pages.length === 0 || e.pages.some((p) => wanted.has(p)));
|
|
336
|
+
}
|
|
337
|
+
return filtered;
|
|
338
|
+
}
|
|
339
|
+
// ─── inspect_tags (C-1) ──────────────────────────────────
|
|
340
|
+
/** Convert a walked structure element to a `TagNode` for inspect_tags. */
|
|
341
|
+
function toTagNode(element, roleCounts) {
|
|
342
|
+
roleCounts[element.role] = (roleCounts[element.role] ?? 0) + 1;
|
|
343
|
+
return {
|
|
344
|
+
role: element.role,
|
|
345
|
+
children: element.children.map((child) => toTagNode(child, roleCounts)),
|
|
346
|
+
contentCount: element.contentRefs.length,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
function tagDepth(node) {
|
|
350
|
+
if (node.children.length === 0)
|
|
351
|
+
return 1;
|
|
352
|
+
return 1 + Math.max(...node.children.map(tagDepth));
|
|
353
|
+
}
|
|
354
|
+
function tagCount(node) {
|
|
355
|
+
return 1 + node.children.reduce((sum, child) => sum + tagCount(child), 0);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Build the inspect_tags analysis from the document's structure tree.
|
|
359
|
+
*
|
|
360
|
+
* C-1: the previous implementation walked `page.getStructTree()` per page and
|
|
361
|
+
* hung all of them under a synthetic `StructTreeRoot` node — so a two-page
|
|
362
|
+
* document reported TWO `Document` roots, and any element spanning pages was
|
|
363
|
+
* duplicated. That is a merge artifact, not the document's structure, and
|
|
364
|
+
* "reporting the structure tree is a fact and reader's job" (family M-2) only
|
|
365
|
+
* holds if the reported tree is the real one. Here `StructTreeRoot` is the
|
|
366
|
+
* actual root of the actual tree: one `Document`, page-spanning elements whole.
|
|
367
|
+
*/
|
|
368
|
+
export function analyzeTagsFromStructTree(roots, isTagged) {
|
|
369
|
+
if (!isTagged || !roots || roots.length === 0) {
|
|
370
|
+
return { isTagged, rootTag: null, maxDepth: 0, totalElements: 0, roleCounts: {} };
|
|
371
|
+
}
|
|
372
|
+
const roleCounts = {};
|
|
373
|
+
const children = roots.map((root) => toTagNode(root, roleCounts));
|
|
374
|
+
const rootTag = { role: 'StructTreeRoot', children, contentCount: 0 };
|
|
375
|
+
return {
|
|
376
|
+
isTagged: true,
|
|
377
|
+
rootTag,
|
|
378
|
+
// The StructTreeRoot node itself is one level; tagDepth counts from a child.
|
|
379
|
+
maxDepth: children.length > 0 ? 1 + Math.max(...children.map(tagDepth)) : 1,
|
|
380
|
+
totalElements: children.reduce((sum, child) => sum + tagCount(child), 0),
|
|
381
|
+
roleCounts,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Analyze a Tagged PDF's structure tree for inspect_tags.
|
|
386
|
+
*
|
|
387
|
+
* Uses the document's `StructTreeRoot` (pdf-lib), not per-page trees — see
|
|
388
|
+
* `analyzeTagsFromStructTree`.
|
|
389
|
+
*/
|
|
390
|
+
export async function analyzeTags(filePath) {
|
|
391
|
+
const doc = await loadWithPdfLib(filePath);
|
|
392
|
+
const markInfo = deref(doc, doc.catalog.get(PDFName.of('MarkInfo')));
|
|
393
|
+
const marked = markInfo instanceof PDFDict ? deref(doc, markInfo.get(PDFName.of('Marked'))) : undefined;
|
|
394
|
+
const isTagged = String(marked) === 'true';
|
|
395
|
+
return analyzeTagsFromStructTree(walkStructTree(doc), isTagged);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Extract structured text — the `extract_structured_text` service entry point.
|
|
399
|
+
*
|
|
400
|
+
* Reads the document twice on purpose, once per library, because each answers a
|
|
401
|
+
* different question:
|
|
402
|
+
*
|
|
403
|
+
* - **pdf-lib** — the structure: `StructTreeRoot`, depth-first, with element
|
|
404
|
+
* identity intact across pages.
|
|
405
|
+
* - **pdfjs** — the text: marked content resolved to strings.
|
|
406
|
+
*
|
|
407
|
+
* Untagged documents return `isTagged: false` with a reason and no elements.
|
|
408
|
+
* Nothing is guessed: §14.8.2.5 NOTE 1 is explicit that page content order need
|
|
409
|
+
* not match logical content order, so clustering by coordinates would be an
|
|
410
|
+
* invention, not an observation — and reader reports observations.
|
|
411
|
+
*/
|
|
412
|
+
export async function extractStructuredText(filePath, options = {}) {
|
|
413
|
+
const libDoc = await loadWithPdfLib(filePath);
|
|
414
|
+
const markInfo = deref(libDoc, libDoc.catalog.get(PDFName.of('MarkInfo')));
|
|
415
|
+
const marked = markInfo instanceof PDFDict ? deref(libDoc, markInfo.get(PDFName.of('Marked'))) : undefined;
|
|
416
|
+
const isTagged = String(marked) === 'true';
|
|
417
|
+
const roots = walkStructTree(libDoc);
|
|
418
|
+
if (!isTagged || !roots || roots.length === 0) {
|
|
419
|
+
return {
|
|
420
|
+
isTagged: false,
|
|
421
|
+
lang: null,
|
|
422
|
+
elements: [],
|
|
423
|
+
note: 'This document is not tagged (no MarkInfo/Marked=true or no reachable StructTreeRoot), ' +
|
|
424
|
+
'so its logical content order cannot be determined. ISO 32000-2 §14.8.2.5 defines logical ' +
|
|
425
|
+
'content order as a depth-first traversal of the structure hierarchy; without one, any ' +
|
|
426
|
+
'ordering would be inferred from coordinates and could not be trusted. ' +
|
|
427
|
+
'To add a structure scaffold, use pdf-writer-mcp `ensure_tagged`, then retry.',
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
const langEntry = deref(libDoc, libDoc.catalog.get(PDFName.of('Lang')));
|
|
431
|
+
const lang = langEntry instanceof PDFString || langEntry instanceof PDFHexString
|
|
432
|
+
? langEntry.decodeText()
|
|
433
|
+
: null;
|
|
434
|
+
const jsDoc = await loadDocument(filePath);
|
|
435
|
+
try {
|
|
436
|
+
const idToText = await buildDocumentIdToTextMap(jsDoc);
|
|
437
|
+
// Map page object numbers (what /Pg holds) to 1-based page numbers.
|
|
438
|
+
const pageNumByObjNum = new Map();
|
|
439
|
+
const pages = libDoc.getPages();
|
|
440
|
+
for (let i = 0; i < pages.length; i++) {
|
|
441
|
+
pageNumByObjNum.set(pages[i].ref.objectNumber, i + 1);
|
|
442
|
+
}
|
|
443
|
+
const pageFilter = options.pages
|
|
444
|
+
? resolvePageNumbers(options.pages, jsDoc.numPages)
|
|
445
|
+
: undefined;
|
|
446
|
+
return {
|
|
447
|
+
isTagged: true,
|
|
448
|
+
lang,
|
|
449
|
+
elements: buildStructuredText(roots, idToText, pageNumByObjNum, {
|
|
450
|
+
roles: options.roles,
|
|
451
|
+
pages: pageFilter,
|
|
452
|
+
}),
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
finally {
|
|
456
|
+
await jsDoc.destroy();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
// ─── extract_tables (#14) ───────────────────────────────────────────────────
|
|
460
|
+
/**
|
|
461
|
+
* Collect every top-level `Table` element in logical content order.
|
|
462
|
+
*
|
|
463
|
+
* Nested tables are not emitted as separate tables (parity with the previous
|
|
464
|
+
* implementation) — their text still appears inside the enclosing cell.
|
|
465
|
+
*/
|
|
466
|
+
function collectTableElements(elements, into = []) {
|
|
467
|
+
for (const element of elements) {
|
|
468
|
+
if (element.role === 'Table') {
|
|
469
|
+
into.push(element);
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
collectTableElements(element.children, into);
|
|
473
|
+
}
|
|
474
|
+
return into;
|
|
475
|
+
}
|
|
476
|
+
/** Split a `Table` element's rows into THead / TBody / TFoot sections. */
|
|
477
|
+
function tableRowsBySection(table, idToText) {
|
|
478
|
+
const headerRows = [];
|
|
479
|
+
const bodyRows = [];
|
|
480
|
+
const footerRows = [];
|
|
481
|
+
const rowFrom = (tr) => {
|
|
482
|
+
const cells = tr.children
|
|
483
|
+
.filter((c) => c.role === 'TH' || c.role === 'TD')
|
|
484
|
+
.map((c) => ({
|
|
485
|
+
// Same cell treatment as before the walker swap: CJK-aware line-break
|
|
486
|
+
// resolution, whitespace collapse, kerning fold, Markdown escaping.
|
|
487
|
+
// textOf additionally honours /ActualText (§14.9.4), which the old
|
|
488
|
+
// per-page walk did not.
|
|
489
|
+
text: compactCellText(textOf(c, idToText, true)),
|
|
490
|
+
isHeader: c.role === 'TH',
|
|
491
|
+
}));
|
|
492
|
+
return cells.length === 0 ? null : { cells };
|
|
493
|
+
};
|
|
494
|
+
const appendRows = (section, into) => {
|
|
495
|
+
for (const child of section.children) {
|
|
496
|
+
if (child.role === 'TR') {
|
|
497
|
+
const row = rowFrom(child);
|
|
498
|
+
if (row)
|
|
499
|
+
into.push(row);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
for (const child of table.children) {
|
|
504
|
+
if (child.role === 'THead')
|
|
505
|
+
appendRows(child, headerRows);
|
|
506
|
+
else if (child.role === 'TBody')
|
|
507
|
+
appendRows(child, bodyRows);
|
|
508
|
+
else if (child.role === 'TFoot')
|
|
509
|
+
appendRows(child, footerRows);
|
|
510
|
+
else if (child.role === 'TR') {
|
|
511
|
+
// Tables sometimes omit THead/TBody and place TRs directly under <Table>.
|
|
512
|
+
const row = rowFrom(child);
|
|
513
|
+
if (row)
|
|
514
|
+
bodyRows.push(row);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return { headerRows, bodyRows, footerRows };
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Extract every `<Table>` subtree as structured rows/cells (#14).
|
|
521
|
+
*
|
|
522
|
+
* Walks the document's `StructTreeRoot` once — the same walker as
|
|
523
|
+
* `extract_structured_text` and `inspect_tags` (C-1) — so a Table StructElem
|
|
524
|
+
* that continues across a page break stays ONE table with `pages: [..]`.
|
|
525
|
+
*
|
|
526
|
+
* The previous implementation merged per-page `page.getStructTree()` trees.
|
|
527
|
+
* That sliced a page-spanning Table into per-page fragments and emitted
|
|
528
|
+
* "phantom" tables (a lone empty header cell) on pages that carried only the
|
|
529
|
+
* element's Figures — observed on ISO 32000-2 pp.383–386, where the per-page
|
|
530
|
+
* walk reported 7 tables for what the structure tree holds as 4.
|
|
531
|
+
*
|
|
532
|
+
* The `pages` argument filters by touch: a table that touches the range is
|
|
533
|
+
* returned whole, and `index` is assigned in document order before filtering
|
|
534
|
+
* so the same table keeps the same index whatever the filter.
|
|
535
|
+
*/
|
|
536
|
+
export async function extractTables(filePath, pages) {
|
|
537
|
+
const libDoc = await loadWithPdfLib(filePath);
|
|
538
|
+
const markInfo = deref(libDoc, libDoc.catalog.get(PDFName.of('MarkInfo')));
|
|
539
|
+
const marked = markInfo instanceof PDFDict ? deref(libDoc, markInfo.get(PDFName.of('Marked'))) : undefined;
|
|
540
|
+
const isTagged = String(marked) === 'true';
|
|
541
|
+
const roots = walkStructTree(libDoc);
|
|
542
|
+
if (!isTagged || !roots || roots.length === 0) {
|
|
543
|
+
return {
|
|
544
|
+
isTagged: false,
|
|
545
|
+
tables: [],
|
|
546
|
+
totalTables: 0,
|
|
547
|
+
pagesScanned: 0,
|
|
548
|
+
note: 'Document is not tagged. extract_tables relies on /MarkInfo /Marked true ' +
|
|
549
|
+
'and a StructTree. For untagged two-column PDFs, fall back to a ' +
|
|
550
|
+
'column-aware reading strategy (see pdf-reader-mcp Issue #3).',
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
const jsDoc = await loadDocument(filePath);
|
|
554
|
+
try {
|
|
555
|
+
const idToText = await buildDocumentIdToTextMap(jsDoc);
|
|
556
|
+
// Map page object numbers (what /Pg holds) to 1-based page numbers.
|
|
557
|
+
const pageNumByObjNum = new Map();
|
|
558
|
+
const libPages = libDoc.getPages();
|
|
559
|
+
for (let i = 0; i < libPages.length; i++) {
|
|
560
|
+
pageNumByObjNum.set(libPages[i].ref.objectNumber, i + 1);
|
|
561
|
+
}
|
|
562
|
+
const pageNumbers = resolvePageNumbers(pages, jsDoc.numPages);
|
|
563
|
+
const wanted = pages ? new Set(pageNumbers) : undefined;
|
|
564
|
+
const tables = [];
|
|
565
|
+
const tableElements = collectTableElements(roots);
|
|
566
|
+
for (const [i, element] of tableElements.entries()) {
|
|
567
|
+
const tablePages = pagesOf(element, pageNumByObjNum);
|
|
568
|
+
if (wanted && tablePages.length > 0 && !tablePages.some((p) => wanted.has(p)))
|
|
569
|
+
continue;
|
|
570
|
+
const { headerRows, bodyRows, footerRows } = tableRowsBySection(element, idToText);
|
|
571
|
+
tables.push({ pages: tablePages, index: i + 1, headerRows, bodyRows, footerRows });
|
|
572
|
+
}
|
|
573
|
+
return {
|
|
574
|
+
isTagged: true,
|
|
575
|
+
tables,
|
|
576
|
+
totalTables: tables.length,
|
|
577
|
+
pagesScanned: pageNumbers.length,
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
finally {
|
|
581
|
+
await jsDoc.destroy();
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
//# sourceMappingURL=struct-tree-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"struct-tree-service.js","sourceRoot":"","sources":["../../src/services/struct-tree-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,EACL,QAAQ,EACR,OAAO,EAEP,YAAY,EACZ,OAAO,EACP,SAAS,EACT,MAAM,EACN,SAAS,GACV,MAAM,SAAS,CAAC;AAWjB,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EACL,wBAAwB,EACxB,eAAe,EACf,UAAU,EACV,YAAY,EACZ,iBAAiB,GAClB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAsCrD;;;;;;;;;;;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;QACT,MAAM,KAAK,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAsBzE;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAuBxD"}
|
package/dist/tools/index.js
CHANGED
|
@@ -11,6 +11,7 @@ import { registerReadUrl } from './tier1/read-url.js';
|
|
|
11
11
|
import { registerSearchText } from './tier1/search-text.js';
|
|
12
12
|
import { registerSummarize } from './tier1/summarize.js';
|
|
13
13
|
// Tier 2: Structure analysis
|
|
14
|
+
import { registerExtractStructuredText } from './tier2/extract-structured-text.js';
|
|
14
15
|
import { registerExtractTables } from './tier2/extract-tables.js';
|
|
15
16
|
import { registerInspectAnnotations } from './tier2/inspect-annotations.js';
|
|
16
17
|
import { registerInspectFonts } from './tier2/inspect-fonts.js';
|
|
@@ -40,6 +41,7 @@ export function registerAllTools(server) {
|
|
|
40
41
|
registerInspectAnnotations(server);
|
|
41
42
|
registerInspectSignatures(server);
|
|
42
43
|
registerExtractTables(server);
|
|
44
|
+
registerExtractStructuredText(server);
|
|
43
45
|
// Tier 3: Validation & analysis
|
|
44
46
|
registerValidateTagged(server);
|
|
45
47
|
registerValidateMetadata(server);
|
package/dist/tools/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,mBAAmB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,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AAEpE;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAiB;IAChD,+BAA+B;IAC/B,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAC7B,mBAAmB,CAAC,MAAM,CAAC,CAAC;IAC5B,gBAAgB,CAAC,MAAM,CAAC,CAAC;IACzB,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC3B,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC3B,eAAe,CAAC,MAAM,CAAC,CAAC;IACxB,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAE1B,6BAA6B;IAC7B,wBAAwB,CAAC,MAAM,CAAC,CAAC;IACjC,mBAAmB,CAAC,MAAM,CAAC,CAAC;IAC5B,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAC7B,0BAA0B,CAAC,MAAM,CAAC,CAAC;IACnC,yBAAyB,CAAC,MAAM,CAAC,CAAC;IAClC,qBAAqB,CAAC,MAAM,CAAC,CAAC;IAC9B,6BAA6B,CAAC,MAAM,CAAC,CAAC;IAEtC,gCAAgC;IAChC,sBAAsB,CAAC,MAAM,CAAC,CAAC;IAC/B,wBAAwB,CAAC,MAAM,CAAC,CAAC;IACjC,wBAAwB,CAAC,MAAM,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAOzE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"read-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAOzE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAiExD"}
|
|
@@ -13,7 +13,9 @@ export function registerReadText(server) {
|
|
|
13
13
|
|
|
14
14
|
Text is extracted page by page, sorted by vertical position (top to bottom) then horizontal position (left to right), providing natural reading order.
|
|
15
15
|
|
|
16
|
-
For **
|
|
16
|
+
For **tagged** PDFs, prefer \`extract_structured_text\`: it returns text in logical content order (ISO 32000-2 §14.8.2.5) and resolves \`/ActualText\` replacements (§14.9.4), neither of which this tool does — read_text emits **raw glyphs** in coordinate order, so a word carried as ActualText (ligature substitutes, hyphenation fixes) appears here in its glyph form. Tables in tagged PDFs are best read with \`extract_tables\`.
|
|
17
|
+
|
|
18
|
+
For **untagged** multi-column PDFs (e.g. older 新旧対照表 PDFs that lack a structure tree), pass \`split_columns: 2\` or \`3\` to bucket items by X-coordinate left-to-right.
|
|
17
19
|
|
|
18
20
|
For Japanese form-style PDFs (帳票・様式) where U+3000 fullwidth spaces are used as visual indentation, pass \`compact_whitespace: true\` to collapse runs of whitespace to a single ASCII space. Cuts 20–40% of token consumption without losing content.
|
|
19
21
|
|