@shuji-bonji/pdf-reader-mcp 0.24.0 → 0.26.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 +96 -0
- package/README.ja.md +3 -0
- package/README.md +3 -0
- package/dist/errors.d.ts +5 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +3 -0
- package/dist/errors.js.map +1 -1
- package/dist/services/page-renderer.d.ts +23 -0
- package/dist/services/page-renderer.d.ts.map +1 -1
- package/dist/services/page-renderer.js +59 -15
- package/dist/services/page-renderer.js.map +1 -1
- package/dist/services/page-renderer.worker.mjs +140 -3
- package/dist/services/pdf-file-source.d.ts +63 -0
- package/dist/services/pdf-file-source.d.ts.map +1 -0
- package/dist/services/pdf-file-source.js +113 -0
- package/dist/services/pdf-file-source.js.map +1 -0
- package/dist/services/pdfjs-service.d.ts +17 -1
- package/dist/services/pdfjs-service.d.ts.map +1 -1
- package/dist/services/pdfjs-service.js +38 -9
- package/dist/services/pdfjs-service.js.map +1 -1
- package/dist/services/recover-service.d.ts +3 -0
- package/dist/services/recover-service.d.ts.map +1 -1
- package/dist/services/recover-service.js +6 -3
- package/dist/services/recover-service.js.map +1 -1
- package/dist/services/struct-tree-service.d.ts.map +1 -1
- package/dist/services/struct-tree-service.js +4 -2
- package/dist/services/struct-tree-service.js.map +1 -1
- package/dist/tools/index.d.ts +1 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +16 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/tier1/render-page.d.ts.map +1 -1
- package/dist/tools/tier1/render-page.js +3 -1
- package/dist/tools/tier1/render-page.js.map +1 -1
- package/dist/tools/tier1/summarize.d.ts.map +1 -1
- package/dist/tools/tier1/summarize.js +2 -1
- package/dist/tools/tier1/summarize.js.map +1 -1
- package/dist/utils/error-handler.d.ts.map +1 -1
- package/dist/utils/error-handler.js +6 -0
- package/dist/utils/error-handler.js.map +1 -1
- package/dist/utils/pdf-helpers.d.ts +26 -1
- package/dist/utils/pdf-helpers.d.ts.map +1 -1
- package/dist/utils/pdf-helpers.js +72 -4
- package/dist/utils/pdf-helpers.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,102 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.26.0] - 2026-12-05
|
|
9
|
+
|
|
10
|
+
Not 0.25.1: the optional dependency `@hyzyla/pdfium` moves to a new major version and the
|
|
11
|
+
`render_page` description changes.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- **`render_page` reads only the byte ranges the renderer asks for** (#59, stage 3). The
|
|
16
|
+
optional dependency `@hyzyla/pdfium` is now `^3.0.1` (was `^2.1.13`). This server opens the
|
|
17
|
+
file and passes the open file to the rendering worker, which gives PDFium a read callback
|
|
18
|
+
(`FPDF_LoadCustomDocument`); PDFium follows the cross-reference table from the end of the
|
|
19
|
+
file (ISO 32000-2 §7.5.5) and asks only for the objects the page needs. Previously the
|
|
20
|
+
whole file was read and copied into the renderer's memory. No output changes: the images
|
|
21
|
+
are byte-for-byte the same as when the whole file is passed. Measured at 150 dpi, before
|
|
22
|
+
(whole file) → after (ranges): S1000D Issue 7 (53.5 MiB), page 1,500: read 53.5 → 0.99
|
|
23
|
+
MiB, peak memory 303 → 222 MiB; a one-page file padded to 800 MiB: read 800 → 0.00 MiB,
|
|
24
|
+
peak memory 1,760 → 176 MiB, 0.43 → 0.09 s. The file is closed after the worker has
|
|
25
|
+
stopped. With a renderer that has no range-read entry point, the whole file is read as
|
|
26
|
+
before. The size limit (`PDF_READER_MAX_FILE_SIZE_MB`) still applies.
|
|
27
|
+
- **New error code `RENDERER_LIMIT_EXCEEDED` (`retryable: false`).** `render_page` refuses a
|
|
28
|
+
file of 4 GiB or more with it: the renderer's file length and offsets are 32-bit, so it
|
|
29
|
+
cannot address such a file. Raising `PDF_READER_MAX_FILE_SIZE_MB` does not help, which is
|
|
30
|
+
why this is not `FILE_TOO_LARGE` (`retryable: true`). The size limit is still checked first.
|
|
31
|
+
`detail` carries `size_bytes` and `limit_bytes` (4,294,967,295). The file is not read.
|
|
32
|
+
Before, a file over about 2 GiB could not be rendered either: the whole file was copied
|
|
33
|
+
into the renderer's memory, which is limited to 2 GiB. The PDF format itself has no such
|
|
34
|
+
limit (ISO 32000-2 Annex C). SPEC-READER-RENDER-PAGE-003, SPEC-READER-COMMON-ERRORS-002 and
|
|
35
|
+
004 (OpenSpec change `renderer-limit-exceeded`).
|
|
36
|
+
- **The example of a page that does not finish rendering changed** in the `render_page`
|
|
37
|
+
description and in the reason given for a page stopped by the time limit. `@hyzyla/pdfium`
|
|
38
|
+
3.x returns a tiling pattern whose `/YStep` is near zero (ISO 32000-2 §8.7.3.1) in a few
|
|
39
|
+
milliseconds, drawing nothing for it. The example is now a function-based shading
|
|
40
|
+
(§8.7.4.5.2) whose PostScript calculator function (§7.10.5) is evaluated for every pixel.
|
|
41
|
+
The time limit itself is unchanged.
|
|
42
|
+
- **The `render_page` description says that a rendered page may have a tiling pattern left
|
|
43
|
+
unpainted.** `@hyzyla/pdfium` 3.x skips a tiling pattern whose cell is so small that the
|
|
44
|
+
area would need an extremely large number of tiles (measured: `/YStep` 0.01 over a
|
|
45
|
+
500-point square is drawn, 0.001 or smaller is not). The page is still returned as rendered
|
|
46
|
+
and the response cannot tell; the description now says so and points to
|
|
47
|
+
`inspect_structure` / `locate_objects`.
|
|
48
|
+
|
|
49
|
+
### Fixed
|
|
50
|
+
|
|
51
|
+
- **An empty file given to `render_page` stays `INVALID_PDF` with `@hyzyla/pdfium` 3.x.**
|
|
52
|
+
3.x rejects an empty buffer with a new message ("The PDF data is empty"), which this server
|
|
53
|
+
did not classify, so the upgrade alone turned it into `INTERNAL_ERROR`
|
|
54
|
+
(SPEC-READER-COMMON-ERRORS-003). The message is now classified. With range reading the
|
|
55
|
+
renderer reports an empty file as not a PDF, as 2.x did.
|
|
56
|
+
|
|
57
|
+
## [0.25.0] - 2026-12-05
|
|
58
|
+
|
|
59
|
+
Proposed version: 0.25.0 (the maintainer decides). No tool output changes, but Node 20 is no
|
|
60
|
+
longer supported, which is why this is not 0.24.1.
|
|
61
|
+
|
|
62
|
+
### Removed
|
|
63
|
+
|
|
64
|
+
- **Node 20 is no longer supported.** `engines.node` is `>=22` (was `>=20`), and CI runs on
|
|
65
|
+
Node 22 and 24 (was 20 and 22). Node 20 reached its end of life in April 2026. The next
|
|
66
|
+
step of #59 — rendering a page without reading the whole file — needs `@hyzyla/pdfium`
|
|
67
|
+
3.x, which requires Node 22 or later.
|
|
68
|
+
|
|
69
|
+
### Fixed
|
|
70
|
+
|
|
71
|
+
- **A read that does not walk every page now reads only the byte ranges it needs** (#59,
|
|
72
|
+
stage 2b). The text extraction library can follow the cross-reference table from the
|
|
73
|
+
end of the file (ISO 32000-2 §7.5.4, §7.5.5, §7.5.8) and ask for the bytes of the objects
|
|
74
|
+
it needs; this server now answers those requests from the open file instead of reading
|
|
75
|
+
the whole file first. Applies to `get_page_count`, `get_metadata`, and to `read_text`,
|
|
76
|
+
`search_text`, `read_images` and `inspect_annotations` when `pages` is given. A read that
|
|
77
|
+
walks every page (no `pages`, `summarize`, `extract_structured_text`, `extract_tables`)
|
|
78
|
+
still reads the whole file: it needs every byte anyway, and reading in ranges was 1.5 to
|
|
79
|
+
2 s slower on a 3,700-page document. Measured on the 800 MiB file, before → after:
|
|
80
|
+
`get_page_count` 956 → 153 MiB (0.69 → 0.08 s), `read_images` of one page 957 → 155 MiB,
|
|
81
|
+
`get_metadata` 1,756 → 955 MiB, `read_text` of one page 1,758 → 957 MiB (the rest is the
|
|
82
|
+
object-level reader, which still reads the whole file). The size limit still applies.
|
|
83
|
+
The file is opened once and closed when the document is released; its size comes from
|
|
84
|
+
the open file, so a symbolic link is measured by its target.
|
|
85
|
+
|
|
86
|
+
- **A tool call read the file up to 10 times and copied it each time** (#61). Memory was two
|
|
87
|
+
to five times the size of the file: on a one-page file padded to 800 MiB, `read_text` of
|
|
88
|
+
one page used 4.1 GiB. Three changes, none of which changes any output:
|
|
89
|
+
- `readPdfFile` no longer copies what it read (`new Uint8Array(buffer)` made a second copy).
|
|
90
|
+
- Within one tool call, the reads of the object-level reader (`recover-service`) share one
|
|
91
|
+
read of the file. `summarize` read the file 5 times and now reads it twice; the text
|
|
92
|
+
extraction library still reads its own copy, because it detaches the bytes it is given.
|
|
93
|
+
`compare_structure` read each file 4 to 6 times and now reads each twice.
|
|
94
|
+
- `render_page` transfers the bytes to its worker instead of copying them twice.
|
|
95
|
+
- Measured on the 800 MiB file (Linux x64, 8 GiB of memory, one run each), 0.24.0 → this
|
|
96
|
+
version: `get_page_count` 1,749 → 954 MiB, `inspect_structure` 2,539 → 1,745 MiB,
|
|
97
|
+
`get_metadata` 2,552 → 1,754 MiB, `summarize` 3,355 → 1,758 MiB, `read_text` 4,139 →
|
|
98
|
+
1,758 MiB, `render_page` 4,179 → 1,815 MiB; every call also became faster (1.4–5.9 s →
|
|
99
|
+
0.6–1.5 s). On the 53.5 MiB, 3,700-page specification the peak memory fell by 3 to 37 %
|
|
100
|
+
for tools that read a few pages; tools that walk every page are dominated by the pages,
|
|
101
|
+
not the file, and changed little. The responses of 0.24.0 and this version were compared
|
|
102
|
+
byte for byte and are the same.
|
|
103
|
+
|
|
8
104
|
## [0.24.0] - 2026-12-05
|
|
9
105
|
|
|
10
106
|
Proposed version: 0.24.0 (the maintainer decides; `retryable` and `next_actions` of
|
package/README.ja.md
CHANGED
|
@@ -149,6 +149,8 @@ read_images({ file_path: "/path/to/scan.pdf", pages: "1", max_width: 1200, forma
|
|
|
149
149
|
|
|
150
150
|
## インストール
|
|
151
151
|
|
|
152
|
+
Node.js 22 以降が必要です(0.25.0 から。それより前の版は Node 20 でも動きました)。
|
|
153
|
+
|
|
152
154
|
### npx(推奨)
|
|
153
155
|
|
|
154
156
|
```bash
|
|
@@ -492,6 +494,7 @@ pdf-reader-mcp/
|
|
|
492
494
|
| `SOURCE_TIMEOUT` | `read_url` のタイムアウト | `true` |
|
|
493
495
|
| `SOURCE_UNAVAILABLE` | `read_url` で接続を拒まれた・切られた、またはホスト名を引けない | `true` |
|
|
494
496
|
| `RENDERER_NOT_AVAILABLE` | `render_page` の任意の部品 `@hyzyla/pdfium` が無い。入れてから再度呼べばよく、再起動は要らない | `true` |
|
|
497
|
+
| `RENDERER_LIMIT_EXCEEDED` | `render_page` に 4 GiB 以上のファイルを渡した。描画の部品はファイルの位置を 32 ビットで扱うので描けない。大きさの上限を上げても通らず、ほかのツールには当たらない。`detail` に `size_bytes`・`limit_bytes` | `false` |
|
|
495
498
|
| `INTERNAL_ERROR` | サーバー内部の故障 | `false` |
|
|
496
499
|
|
|
497
500
|
### 移行ノート (v0.5.x → v0.6.0)
|
package/README.md
CHANGED
|
@@ -150,6 +150,8 @@ answers yes for fonts whose dictionary has none.
|
|
|
150
150
|
|
|
151
151
|
## Installation
|
|
152
152
|
|
|
153
|
+
Requires Node.js 22 or later (0.25.0 and later; earlier versions also ran on Node 20).
|
|
154
|
+
|
|
153
155
|
### npx (recommended)
|
|
154
156
|
|
|
155
157
|
```bash
|
|
@@ -512,6 +514,7 @@ Every error carries `error` (an English sentence), `code` and `retryable`; `hint
|
|
|
512
514
|
| `SOURCE_TIMEOUT` | `read_url`: the request timed out | `true` |
|
|
513
515
|
| `SOURCE_UNAVAILABLE` | `read_url`: the host refused or closed the connection, or its name could not be resolved | `true` |
|
|
514
516
|
| `RENDERER_NOT_AVAILABLE` | `render_page`: the optional `@hyzyla/pdfium` is not installed. Install it and call again; no restart is needed | `true` |
|
|
517
|
+
| `RENDERER_LIMIT_EXCEEDED` | `render_page`: the file is 4 GiB or larger, which the renderer cannot address (its file offsets are 32-bit). Raising the size limit does not help; the other tools are not affected. `detail` carries `size_bytes` and `limit_bytes` | `false` |
|
|
515
518
|
| `INTERNAL_ERROR` | A failure inside this server | `false` |
|
|
516
519
|
|
|
517
520
|
### Migration note (v0.5.x → v0.6.0)
|
package/dist/errors.d.ts
CHANGED
|
@@ -21,11 +21,13 @@
|
|
|
21
21
|
* - 外部ソース由来: `SOURCE_API_ERROR` / `SOURCE_TIMEOUT` / `SOURCE_UNAVAILABLE`
|
|
22
22
|
* - PDF 固有: `FILE_TOO_LARGE`(大きさの上限を超えた。上限は環境変数で変えられる)
|
|
23
23
|
* - reader 固有: `RENDERER_NOT_AVAILABLE`(render_page の描画の部品が読み込めない)
|
|
24
|
+
* - reader 固有: `RENDERER_LIMIT_EXCEEDED`(render_page の描画の部品が扱えない大きさのファイル。
|
|
25
|
+
* 4 GiB 以上。部品のファイルの位置が 32 ビット。SPEC-READER-RENDER-PAGE-003)
|
|
24
26
|
* - システム: `INTERNAL_ERROR`
|
|
25
27
|
*
|
|
26
28
|
* 共通語彙と同じ意味の別名を作らない(pdf-agent-stack docs/specs/06 §2.3)。
|
|
27
29
|
*/
|
|
28
|
-
export type LawErrorCode = 'INVALID_ARGUMENT' | 'DOC_NOT_FOUND' | 'INVALID_PDF' | 'ENCRYPTED_PDF' | 'UNSUPPORTED_PDF_FEATURE' | 'SOURCE_API_ERROR' | 'SOURCE_TIMEOUT' | 'SOURCE_UNAVAILABLE' | 'FILE_TOO_LARGE' | 'RENDERER_NOT_AVAILABLE' | 'INTERNAL_ERROR';
|
|
30
|
+
export type LawErrorCode = 'INVALID_ARGUMENT' | 'DOC_NOT_FOUND' | 'INVALID_PDF' | 'ENCRYPTED_PDF' | 'UNSUPPORTED_PDF_FEATURE' | 'SOURCE_API_ERROR' | 'SOURCE_TIMEOUT' | 'SOURCE_UNAVAILABLE' | 'FILE_TOO_LARGE' | 'RENDERER_NOT_AVAILABLE' | 'RENDERER_LIMIT_EXCEEDED' | 'INTERNAL_ERROR';
|
|
29
31
|
/**
|
|
30
32
|
* 次に取るべきアクションの提案。
|
|
31
33
|
* LLM がこれを読んで自律的に次のツールを呼ぶことを想定。
|
|
@@ -87,6 +89,8 @@ export type FileSystemReason = 'PERMISSION_DENIED' | 'NOT_A_DIRECTORY' | 'IS_A_D
|
|
|
87
89
|
* - `SOURCE_API_ERROR` は false(HTTP の失敗の多くは URL の誤り。2026-11-05 決定)
|
|
88
90
|
* - `FILE_TOO_LARGE` は true(#56。上限を環境変数で変えられる。verify・writer は
|
|
89
91
|
* 変える手段が無いので false のまま)
|
|
92
|
+
* - `RENDERER_LIMIT_EXCEEDED` は false(大きさの上限を上げても、描画の部品が扱える
|
|
93
|
+
* 大きさは変わらない。#59 の段階 3)
|
|
90
94
|
*
|
|
91
95
|
* `Record` なので、code を足して値を書き忘れると型検査で落ちる。
|
|
92
96
|
* SPEC-READER-COMMON-ERRORS-004
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,YAAY,GAEpB,kBAAkB,GAElB,eAAe,GAEf,aAAa,GACb,eAAe,GACf,yBAAyB,GAEzB,kBAAkB,GAClB,gBAAgB,GAChB,oBAAoB,GAEpB,gBAAgB,GAChB,wBAAwB,GACxB,yBAAyB,GAEzB,gBAAgB,CAAC;AAErB;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,+BAA+B;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,gBAAgB;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,mBAAmB;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC;AAED;;GAEG;A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AAO,GAAG,KAAK,IAAI,eAAe,CAS1E;AAED;;;;;;GAMG;AACH,eAAO,MAAM,YAAY;aACvB,aAAa,cAAc,MAAM,KAAG,UAAU;aAK9C,eAAe,cAAc,MAAM,KAAG,UAAU;aAKhD,mBAAmB,cAAc,MAAM,KAAG,UAAU;IAMpD;;;OAGG;aACH,cAAc,YAAY,MAAM,QAAQ,MAAM,KAAG,UAAU;aAO3D,UAAU,QAAM,UAAU;aAI1B,QAAQ,SAAS,MAAM,KAAG,UAAU;CAK5B,CAAC"}
|
package/dist/errors.js
CHANGED
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
* - `SOURCE_API_ERROR` は false(HTTP の失敗の多くは URL の誤り。2026-11-05 決定)
|
|
23
23
|
* - `FILE_TOO_LARGE` は true(#56。上限を環境変数で変えられる。verify・writer は
|
|
24
24
|
* 変える手段が無いので false のまま)
|
|
25
|
+
* - `RENDERER_LIMIT_EXCEEDED` は false(大きさの上限を上げても、描画の部品が扱える
|
|
26
|
+
* 大きさは変わらない。#59 の段階 3)
|
|
25
27
|
*
|
|
26
28
|
* `Record` なので、code を足して値を書き忘れると型検査で落ちる。
|
|
27
29
|
* SPEC-READER-COMMON-ERRORS-004
|
|
@@ -36,6 +38,7 @@ export const RETRYABLE = {
|
|
|
36
38
|
INVALID_PDF: false,
|
|
37
39
|
ENCRYPTED_PDF: false,
|
|
38
40
|
UNSUPPORTED_PDF_FEATURE: false,
|
|
41
|
+
RENDERER_LIMIT_EXCEEDED: false,
|
|
39
42
|
SOURCE_API_ERROR: false,
|
|
40
43
|
INTERNAL_ERROR: false,
|
|
41
44
|
};
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;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CAAC,GAAY,EAAc,EAAE,CAAC,CAAC;QACvC,MAAM,EAAE,YAAY;QACpB,MAAM,EAAE,yEAAyE;QACjF,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,SAAS;KACnC,CAAC;CACM,CAAC"}
|
|
@@ -65,6 +65,16 @@ export interface RenderResult {
|
|
|
65
65
|
pages: RenderedPage[];
|
|
66
66
|
omitted: OmittedRender[];
|
|
67
67
|
totalEncodedBytes: number;
|
|
68
|
+
/**
|
|
69
|
+
* ファイルをどう読んだか(#59 の段階 3)。`range` は描画の部品が求めた範囲だけを読んだ、
|
|
70
|
+
* `whole` は全体を読んだ(範囲読みの入口が無い版の部品)。worker が最後まで走らずに
|
|
71
|
+
* 止まったときは null。応答には出さない。試験が読んだ量を確かめるためのもの。
|
|
72
|
+
*/
|
|
73
|
+
fileRead: {
|
|
74
|
+
mode: 'range' | 'whole';
|
|
75
|
+
reads: number;
|
|
76
|
+
bytes: number;
|
|
77
|
+
} | null;
|
|
68
78
|
}
|
|
69
79
|
export interface RenderOptions {
|
|
70
80
|
/** Rasterisation density. Default 150 — text is legible, files stay small. */
|
|
@@ -81,6 +91,11 @@ export interface RenderOptions {
|
|
|
81
91
|
quality?: number;
|
|
82
92
|
/** Response budget override, for tests. */
|
|
83
93
|
maxTotalBytes?: number;
|
|
94
|
+
/**
|
|
95
|
+
* 範囲読みの入口があっても、ファイル全体を読んで描画の部品に渡す。範囲読みと
|
|
96
|
+
* 同じ画像になることを試験で確かめるためのもの(#59 の段階 3)。ツールの引数には無い。
|
|
97
|
+
*/
|
|
98
|
+
readWholeFile?: boolean;
|
|
84
99
|
}
|
|
85
100
|
export declare const DEFAULT_RENDER_DPI = 150;
|
|
86
101
|
/**
|
|
@@ -124,4 +139,12 @@ export declare const RENDERER_MISSING_MESSAGE: string;
|
|
|
124
139
|
* reported in `omitted` with the reason, mirroring `read_images` (#22).
|
|
125
140
|
*/
|
|
126
141
|
export declare function renderPages(filePath: string, pages: string, options?: RenderOptions): Promise<RenderResult>;
|
|
142
|
+
/**
|
|
143
|
+
* 描画の部品のファイルの長さと位置は 32 ビットの符号なし整数(`FPDF_FILEACCESS` の
|
|
144
|
+
* `unsigned long`。この WASM は 32 ビット)。4 GiB 以上のファイルは位置を表せない。
|
|
145
|
+
* 断るときの code は `RENDERER_LIMIT_EXCEEDED`(`retryable: false`)。大きさの上限を
|
|
146
|
+
* 上げても通らないので、`FILE_TOO_LARGE`(`retryable: true`)にはしない
|
|
147
|
+
* (SPEC-READER-RENDER-PAGE-003)。
|
|
148
|
+
*/
|
|
149
|
+
export declare const MAX_RENDERER_FILE_BYTES = 4294967295;
|
|
127
150
|
//# sourceMappingURL=page-renderer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page-renderer.d.ts","sourceRoot":"","sources":["../../src/services/page-renderer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAIH,OAAO,EAAE,cAAc,
|
|
1
|
+
{"version":3,"file":"page-renderer.d.ts","sourceRoot":"","sources":["../../src/services/page-renderer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAIH,OAAO,EAAE,cAAc,EAAmB,MAAM,2BAA2B,CAAC;AAM5E,mDAAmD;AACnD,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,wCAAwC;IACxC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,oEAAoE;IACpE,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,6CAA6C;AAC7C,MAA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1D;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,IAAI,cAAc,CAU1D;AAED,eAAO,MAAM,wBAAwB,QAGgD,CAAC;AAgBtF;;;;;GAKG;AACH,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAAC,YAAY,CAAC,CA8BvB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,aAAc,CAAC"}
|
|
@@ -37,10 +37,11 @@
|
|
|
37
37
|
*/
|
|
38
38
|
import { Worker } from 'node:worker_threads';
|
|
39
39
|
import { DEFAULT_IMAGE_QUALITY, MAX_IMAGE_PIXELS, MAX_IMAGE_RESPONSE_BYTES } from '../constants.js';
|
|
40
|
-
import { PdfReaderError } from '../utils/error-handler.js';
|
|
41
|
-
import {
|
|
40
|
+
import { PdfReaderError, validatePdfPath } from '../utils/error-handler.js';
|
|
41
|
+
import { resolvePageNumbers } from '../utils/pdf-helpers.js';
|
|
42
42
|
import { encodeJpeg, encodePng } from './image-encoder.js';
|
|
43
43
|
import { downscale } from './image-resampler.js';
|
|
44
|
+
import { openPdfFile } from './pdf-file-source.js';
|
|
44
45
|
export const DEFAULT_RENDER_DPI = 150;
|
|
45
46
|
/**
|
|
46
47
|
* 1 ページの描画に許す時間。20 秒。
|
|
@@ -131,9 +132,44 @@ export async function renderPages(filePath, pages, options = {}) {
|
|
|
131
132
|
const fromEnv = Number(process.env.PDF_READER_RENDER_TIMEOUT_MS);
|
|
132
133
|
const timeoutMs = options.pageTimeoutMs ??
|
|
133
134
|
(Number.isFinite(fromEnv) && fromEnv > 0 ? fromEnv : DEFAULT_PAGE_TIMEOUT_MS);
|
|
134
|
-
// 🔴
|
|
135
|
-
//
|
|
136
|
-
|
|
135
|
+
// 🔴 ページ番号の解決とファイルを開くのは、ここで済ませる。Worker は src の
|
|
136
|
+
// module を import しない(冒頭の説明)。
|
|
137
|
+
// ファイルは開くだけで読まない。worker が記述子から、描画の部品が求める範囲だけを
|
|
138
|
+
// 読む(#59 の段階 3)。記述子はプロセスで共有されるので、worker からそのまま読める。
|
|
139
|
+
validatePdfPath(filePath);
|
|
140
|
+
const { handle, size } = await openPdfFile(filePath);
|
|
141
|
+
try {
|
|
142
|
+
if (size > MAX_RENDERER_FILE_BYTES)
|
|
143
|
+
throw rendererFileTooLargeError(size);
|
|
144
|
+
return await renderOpenedFile(handle.fd, size, filePath, pages, options, {
|
|
145
|
+
format,
|
|
146
|
+
quality,
|
|
147
|
+
budget,
|
|
148
|
+
timeoutMs,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
// 🔴 閉じるのは worker が止まった後(`renderOpenedFile` は worker の終了を待って返る)。
|
|
153
|
+
// 先に閉じると、同じ番号の記述子が別のファイルに使われ、worker が別のファイルを読みうる。
|
|
154
|
+
await handle.close().catch(() => undefined);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* 描画の部品のファイルの長さと位置は 32 ビットの符号なし整数(`FPDF_FILEACCESS` の
|
|
159
|
+
* `unsigned long`。この WASM は 32 ビット)。4 GiB 以上のファイルは位置を表せない。
|
|
160
|
+
* 断るときの code は `RENDERER_LIMIT_EXCEEDED`(`retryable: false`)。大きさの上限を
|
|
161
|
+
* 上げても通らないので、`FILE_TOO_LARGE`(`retryable: true`)にはしない
|
|
162
|
+
* (SPEC-READER-RENDER-PAGE-003)。
|
|
163
|
+
*/
|
|
164
|
+
export const MAX_RENDERER_FILE_BYTES = 0xffff_ffff;
|
|
165
|
+
function rendererFileTooLargeError(size) {
|
|
166
|
+
return new PdfReaderError(`The file is ${size.toLocaleString()} bytes. render_page can address at most ` +
|
|
167
|
+
`${MAX_RENDERER_FILE_BYTES.toLocaleString()} bytes (4 GiB - 1).`, 'RENDERER_LIMIT_EXCEEDED', 'This is a limit of the renderer this server uses (its file offsets are 32-bit), not a ' +
|
|
168
|
+
'limit of the PDF format and not a setting of this server. The other tools are not ' +
|
|
169
|
+
'affected by it.', 'RENDERER_LIMIT_EXCEEDED', undefined, { size_bytes: size, limit_bytes: MAX_RENDERER_FILE_BYTES });
|
|
170
|
+
}
|
|
171
|
+
async function renderOpenedFile(fd, size, filePath, pages, options, settings) {
|
|
172
|
+
const { format, quality, budget, timeoutMs } = settings;
|
|
137
173
|
// 🔴 拡張子は .mjs 固定。src からも dist からも、Node がそのまま実行できる形。
|
|
138
174
|
const workerUrl = new URL('./page-renderer.worker.mjs', import.meta.url);
|
|
139
175
|
const rendered = [];
|
|
@@ -141,10 +177,14 @@ export async function renderPages(filePath, pages, options = {}) {
|
|
|
141
177
|
let totalEncodedBytes = 0;
|
|
142
178
|
let planned = null;
|
|
143
179
|
let inFlight = null;
|
|
180
|
+
let fileRead = null;
|
|
144
181
|
await new Promise((resolve, reject) => {
|
|
145
182
|
const worker = new Worker(workerUrl, {
|
|
146
183
|
workerData: {
|
|
147
|
-
|
|
184
|
+
fd,
|
|
185
|
+
size,
|
|
186
|
+
path: filePath,
|
|
187
|
+
readWholeFile: options.readWholeFile === true,
|
|
148
188
|
dpi: options.dpi ?? DEFAULT_RENDER_DPI,
|
|
149
189
|
maxWidth: options.maxWidth,
|
|
150
190
|
maxPixels: MAX_IMAGE_PIXELS,
|
|
@@ -157,11 +197,13 @@ export async function renderPages(filePath, pages, options = {}) {
|
|
|
157
197
|
return;
|
|
158
198
|
settled = true;
|
|
159
199
|
clearTimeout(timer);
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
200
|
+
// 🔴 worker が止まりきってから返す。呼び出し側はその後でファイルを閉じる。
|
|
201
|
+
void worker.terminate().finally(() => {
|
|
202
|
+
if (error)
|
|
203
|
+
reject(error);
|
|
204
|
+
else
|
|
205
|
+
resolve();
|
|
206
|
+
});
|
|
165
207
|
};
|
|
166
208
|
/**
|
|
167
209
|
* 🔴 時限はメッセージが届くたびに引き直す。1 ページごとの予算であって、
|
|
@@ -177,9 +219,10 @@ export async function renderPages(filePath, pages, options = {}) {
|
|
|
177
219
|
omitted.push({
|
|
178
220
|
page: stuck,
|
|
179
221
|
reason: `rendering did not finish within ${timeoutMs} ms and was stopped. ` +
|
|
180
|
-
'A page can take unbounded time to rasterise — for example a
|
|
181
|
-
'
|
|
182
|
-
'
|
|
222
|
+
'A page can take unbounded time to rasterise — for example a ' +
|
|
223
|
+
'function-based shading (ISO 32000-2 §8.7.4.5.2) whose PostScript ' +
|
|
224
|
+
'calculator function (§7.10.5) runs a very long program for every pixel, ' +
|
|
225
|
+
'so the time grows with the program and with the pixels. Lower dpi ' +
|
|
183
226
|
'or pass max_width if the page is merely large; otherwise this page ' +
|
|
184
227
|
'cannot be rasterised here.',
|
|
185
228
|
});
|
|
@@ -260,6 +303,7 @@ export async function renderPages(filePath, pages, options = {}) {
|
|
|
260
303
|
omitted.push({ page: message.page, reason: message.reason });
|
|
261
304
|
return;
|
|
262
305
|
case 'done':
|
|
306
|
+
fileRead = message.reading;
|
|
263
307
|
finish();
|
|
264
308
|
return;
|
|
265
309
|
}
|
|
@@ -267,6 +311,6 @@ export async function renderPages(filePath, pages, options = {}) {
|
|
|
267
311
|
worker.on('error', (error) => finish(error));
|
|
268
312
|
worker.on('exit', () => finish());
|
|
269
313
|
});
|
|
270
|
-
return { requested: planned ?? [], pages: rendered, omitted, totalEncodedBytes };
|
|
314
|
+
return { requested: planned ?? [], pages: rendered, omitted, totalEncodedBytes, fileRead };
|
|
271
315
|
}
|
|
272
316
|
//# sourceMappingURL=page-renderer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page-renderer.js","sourceRoot":"","sources":["../../src/services/page-renderer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,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,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,CAAC;AAC7F,CAAC"}
|
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
*
|
|
18
18
|
* `worker.terminate()` は止まった PDFium を実際に止められる(3 秒で exit を実測)。
|
|
19
19
|
*
|
|
20
|
+
* 🔴 この例は @hyzyla/pdfium 2.x のもの。3.0.1 はこのページを何も描かずに 4 ミリ秒で
|
|
21
|
+
* 返す(タイルの数に上限があるらしい)。3.x でも、関数型シェーディング(§8.7.4.5.2)の
|
|
22
|
+
* PostScript 計算機関数(§7.10.5)が長いと、画素ごとに評価して数十秒以上かかる
|
|
23
|
+
* (`scripts/golden-specimens-render.mjs` の冒頭)。時限の仕組みは変わらず要る。
|
|
24
|
+
*
|
|
20
25
|
* ## 🔴 なぜ .mjs で、なぜ何も import しないか
|
|
21
26
|
*
|
|
22
27
|
* このファイルは Node が**そのまま**実行する。TypeScript で書くと、Node 20 には
|
|
@@ -29,9 +34,30 @@
|
|
|
29
34
|
* BGRA から RGB への変換・縮小・符号化は呼び出し側で行う。
|
|
30
35
|
* 画素は転送可能オブジェクトとして渡すので、複製は起きない。
|
|
31
36
|
*
|
|
37
|
+
* ## ファイルは要る範囲だけを読む(#59 の段階 3)
|
|
38
|
+
*
|
|
39
|
+
* 呼び出し側が開いたファイルの記述子(`workerData.fd`)と大きさ(`workerData.size`)を
|
|
40
|
+
* 受け取り、PDFium の `FPDF_LoadCustomDocument` に読み取りの関数を渡す。PDFium は
|
|
41
|
+
* 末尾の `startxref` から相互参照表を読み(ISO 32000-2 §7.5.5)、描くページに要る
|
|
42
|
+
* オブジェクトの範囲だけを求めてくる。読み取りの関数は、その範囲を `fs.readSync` で
|
|
43
|
+
* WASM のメモリーへ直接読む(複製を作らない)。
|
|
44
|
+
*
|
|
45
|
+
* 実測(2026-12-05。@hyzyla/pdfium 3.0.1、部品を直接呼んだ値): 53.5 MiB・3,700 ページの
|
|
46
|
+
* 文書の 1,500 ページ目で、読んだのは 0.99 MiB。全体を渡すと 53.5 MiB を WASM の
|
|
47
|
+
* メモリーに写す。描いた画素は同じ。
|
|
48
|
+
*
|
|
49
|
+
* 🔴 部品はこの入口を公開していない。`PDFiumLibrary` が内側に持つ module の
|
|
50
|
+
* `addFunction`(JavaScript の関数を WASM の関数の表に登録する)と
|
|
51
|
+
* `_FPDF_LoadCustomDocument` を直接使う。2.x には `addFunction` が無かった。
|
|
52
|
+
* どちらかが無い版では、これまでどおりファイル全体を読んで `loadDocument` に渡す。
|
|
53
|
+
*
|
|
54
|
+
* 🔴 `FPDF_FILEACCESS` の長さと位置は `unsigned long` で、この WASM(32 ビット)では
|
|
55
|
+
* 4 GiB 未満しか表せない。それ以上のファイルは呼び出し側が断る。
|
|
56
|
+
*
|
|
32
57
|
* `package.json` の `build` がこのファイルを `dist/services/` へ複製する。
|
|
33
58
|
*/
|
|
34
59
|
|
|
60
|
+
import { fstatSync, readSync } from 'node:fs';
|
|
35
61
|
import { parentPort, workerData } from 'node:worker_threads';
|
|
36
62
|
|
|
37
63
|
const post = (message, transfer) => parentPort?.postMessage(message, transfer);
|
|
@@ -48,7 +74,115 @@ function awaitPageNumbers() {
|
|
|
48
74
|
const specifier = '@hyzyla/pdfium';
|
|
49
75
|
const module_ = await import(specifier);
|
|
50
76
|
const library = await module_.PDFiumLibrary.init();
|
|
51
|
-
|
|
77
|
+
|
|
78
|
+
/** 読んだ量。呼び出し側へ返し、試験が「要る範囲だけを読んだ」ことを確かめる。 */
|
|
79
|
+
const reading = { mode: 'range', reads: 0, bytes: 0 };
|
|
80
|
+
/** 読み取りの関数の中で起きた失敗。PDFium には 0 を返すしか伝える手が無いので、ここに残す。 */
|
|
81
|
+
let readFailure = null;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* FPDF_GetLastError の値を、部品の `loadDocument` が投げるのと同じ文にする。
|
|
85
|
+
* エラーの分類(`error-handler.ts`)は、この文で分けている。
|
|
86
|
+
*/
|
|
87
|
+
function loadError(lastError) {
|
|
88
|
+
switch (lastError) {
|
|
89
|
+
case 2:
|
|
90
|
+
return new Error('File not found or could not be opened');
|
|
91
|
+
case 3:
|
|
92
|
+
return new Error('File not in PDF format or corrupted');
|
|
93
|
+
case 4:
|
|
94
|
+
return new Error('Password required or incorrect password');
|
|
95
|
+
case 5:
|
|
96
|
+
return new Error('Unsupported security scheme');
|
|
97
|
+
case 6:
|
|
98
|
+
return new Error('Page not found or content error');
|
|
99
|
+
default:
|
|
100
|
+
return new Error(lastError === 1 ? 'Unknown error' : `PDF Loading = ${lastError}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 読み取りの関数の中の失敗を、呼び出し側へ渡す例外にする。 */
|
|
105
|
+
function throwIfReadFailed() {
|
|
106
|
+
if (readFailure) throw readFailure;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 開いたファイルを、PDFium が求める範囲ごとに読ませて文書を開く。 */
|
|
110
|
+
function openByRange(pdfium) {
|
|
111
|
+
const { fd, size } = workerData;
|
|
112
|
+
const getBlock = (_param, position, buffer, length) => {
|
|
113
|
+
const at = position >>> 0;
|
|
114
|
+
const want = length >>> 0;
|
|
115
|
+
reading.reads++;
|
|
116
|
+
try {
|
|
117
|
+
// 🔴 HEAPU8 は毎回引き直す。WASM のメモリーが育つと、前の参照は使えなくなる。
|
|
118
|
+
const got = readSync(fd, pdfium.HEAPU8, buffer >>> 0, want, at);
|
|
119
|
+
reading.bytes += got;
|
|
120
|
+
if (got === want) return 1;
|
|
121
|
+
readFailure ??= new Error(
|
|
122
|
+
`The file "${workerData.path}" became shorter while it was being read ` +
|
|
123
|
+
`(asked for ${want} bytes at offset ${at}, got ${got}).`,
|
|
124
|
+
);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
readFailure ??= error instanceof Error ? error : new Error(String(error));
|
|
127
|
+
}
|
|
128
|
+
return 0;
|
|
129
|
+
};
|
|
130
|
+
const callback = pdfium.addFunction(getBlock, 'iiiii');
|
|
131
|
+
// FPDF_FILEACCESS { unsigned long m_FileLen; m_GetBlock; void* m_Param; }(32 ビットで 12 バイト)
|
|
132
|
+
const access = pdfium.wasmExports.malloc(12);
|
|
133
|
+
pdfium.HEAPU32[access >> 2] = size;
|
|
134
|
+
pdfium.HEAPU32[(access >> 2) + 1] = callback;
|
|
135
|
+
pdfium.HEAPU32[(access >> 2) + 2] = 0;
|
|
136
|
+
const documentIdx = pdfium._FPDF_LoadCustomDocument(access, 0);
|
|
137
|
+
if (!documentIdx) {
|
|
138
|
+
const lastError = pdfium._FPDF_GetLastError();
|
|
139
|
+
pdfium.wasmExports.free(access);
|
|
140
|
+
pdfium.removeFunction(callback);
|
|
141
|
+
throwIfReadFailed();
|
|
142
|
+
throw loadError(lastError);
|
|
143
|
+
}
|
|
144
|
+
// 🔴 `destroy()` が `documentPtr` を free する。ここでは FILEACCESS の構造体を渡す。
|
|
145
|
+
const document_ = new module_.PDFiumDocument({
|
|
146
|
+
module: pdfium,
|
|
147
|
+
documentIdx,
|
|
148
|
+
documentPtr: access,
|
|
149
|
+
});
|
|
150
|
+
return {
|
|
151
|
+
document_,
|
|
152
|
+
release() {
|
|
153
|
+
document_.destroy();
|
|
154
|
+
pdfium.removeFunction(callback);
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 範囲読みの入口が無い版の部品では、ファイル全体を読んで渡す。 */
|
|
160
|
+
async function openWhole() {
|
|
161
|
+
reading.mode = 'whole';
|
|
162
|
+
const size = fstatSync(workerData.fd).size;
|
|
163
|
+
const bytes = new Uint8Array(size);
|
|
164
|
+
let offset = 0;
|
|
165
|
+
while (offset < size) {
|
|
166
|
+
const got = readSync(workerData.fd, bytes, offset, size - offset, offset);
|
|
167
|
+
if (got === 0) break;
|
|
168
|
+
offset += got;
|
|
169
|
+
}
|
|
170
|
+
reading.reads = 1;
|
|
171
|
+
reading.bytes = offset;
|
|
172
|
+
// 部品は自分のメモリーに写しを取るので、ここでは複製しない。
|
|
173
|
+
const document_ = await library.loadDocument(bytes.subarray(0, offset));
|
|
174
|
+
return { document_, release: () => document_.destroy() };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const pdfium = library.module;
|
|
178
|
+
const opened =
|
|
179
|
+
!workerData.readWholeFile &&
|
|
180
|
+
typeof pdfium?.addFunction === 'function' &&
|
|
181
|
+
typeof pdfium?._FPDF_LoadCustomDocument === 'function' &&
|
|
182
|
+
typeof module_.PDFiumDocument === 'function'
|
|
183
|
+
? openByRange(pdfium)
|
|
184
|
+
: await openWhole();
|
|
185
|
+
const document_ = opened.document_;
|
|
52
186
|
|
|
53
187
|
try {
|
|
54
188
|
post({ t: 'pagecount', pageCount: document_.getPageCount() });
|
|
@@ -82,6 +216,9 @@ try {
|
|
|
82
216
|
}
|
|
83
217
|
|
|
84
218
|
const bitmap = await page.render({ scale, render: 'bitmap' });
|
|
219
|
+
// 🔴 読み取りに失敗すると、PDFium はそのオブジェクトを欠いたまま描き終えることがある。
|
|
220
|
+
// 欠けた画像を描けたページとして返さない。
|
|
221
|
+
throwIfReadFailed();
|
|
85
222
|
// 画素は転送する(複製しない)。BGRA のまま渡し、変換は呼び出し側で行う。
|
|
86
223
|
const data = new Uint8Array(bitmap.data);
|
|
87
224
|
post(
|
|
@@ -98,7 +235,7 @@ try {
|
|
|
98
235
|
);
|
|
99
236
|
}
|
|
100
237
|
|
|
101
|
-
post({ t: 'done' });
|
|
238
|
+
post({ t: 'done', reading });
|
|
102
239
|
} finally {
|
|
103
|
-
|
|
240
|
+
opened.release();
|
|
104
241
|
}
|