@codecloud-dev/pdf-lib-plus 1.18.2 → 1.18.3
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/README.md +53 -4
- package/README.zh.md +31 -2
- package/cjs/api/PDFPage.d.ts +12 -0
- package/cjs/api/PDFPage.d.ts.map +1 -1
- package/cjs/api/PDFPage.js +18 -0
- package/cjs/api/PDFPage.js.map +1 -1
- package/cjs/api/index.d.ts +1 -0
- package/cjs/api/index.d.ts.map +1 -1
- package/cjs/api/index.js +1 -0
- package/cjs/api/index.js.map +1 -1
- package/cjs/api/rotations.d.ts +1 -1
- package/cjs/api/text_extraction.d.ts +20 -0
- package/cjs/api/text_extraction.d.ts.map +1 -0
- package/cjs/api/text_extraction.js +494 -0
- package/cjs/api/text_extraction.js.map +1 -0
- package/cjs/core/index.d.ts +1 -0
- package/cjs/core/index.d.ts.map +1 -1
- package/cjs/core/index.js +2 -0
- package/cjs/core/index.js.map +1 -1
- package/cjs/core/operators/PDFOperator.d.ts +4 -0
- package/cjs/core/operators/PDFOperator.d.ts.map +1 -1
- package/cjs/core/operators/PDFOperator.js +8 -0
- package/cjs/core/operators/PDFOperator.js.map +1 -1
- package/cjs/core/parser/PDFContentStreamParser.d.ts +29 -0
- package/cjs/core/parser/PDFContentStreamParser.d.ts.map +1 -0
- package/cjs/core/parser/PDFContentStreamParser.js +97 -0
- package/cjs/core/parser/PDFContentStreamParser.js.map +1 -0
- package/cjs/core/structures/PDFContentStream.d.ts +2 -0
- package/cjs/core/structures/PDFContentStream.d.ts.map +1 -1
- package/cjs/core/structures/PDFContentStream.js +4 -0
- package/cjs/core/structures/PDFContentStream.js.map +1 -1
- package/cjs/core/structures/PDFPageLeaf.d.ts +2 -0
- package/cjs/core/structures/PDFPageLeaf.d.ts.map +1 -1
- package/cjs/core/structures/PDFPageLeaf.js +4 -0
- package/cjs/core/structures/PDFPageLeaf.js.map +1 -1
- package/cjs/utils/arrays.d.ts +1 -1
- package/dist/pdf-lib.esm.js +595 -1
- package/dist/pdf-lib.esm.js.map +1 -1
- package/dist/pdf-lib.esm.min.js +1 -1
- package/dist/pdf-lib.esm.min.js.map +1 -1
- package/dist/pdf-lib.js +598 -0
- package/dist/pdf-lib.js.map +1 -1
- package/dist/pdf-lib.min.js +1 -1
- package/dist/pdf-lib.min.js.map +1 -1
- package/docs/MIGRATION.md +80 -0
- package/docs/TEXT_EXTRACTION.md +102 -0
- package/es/api/PDFPage.d.ts +12 -0
- package/es/api/PDFPage.d.ts.map +1 -1
- package/es/api/PDFPage.js +18 -0
- package/es/api/PDFPage.js.map +1 -1
- package/es/api/index.d.ts +1 -0
- package/es/api/index.d.ts.map +1 -1
- package/es/api/index.js +1 -0
- package/es/api/index.js.map +1 -1
- package/es/api/text_extraction.d.ts +20 -0
- package/es/api/text_extraction.d.ts.map +1 -0
- package/es/api/text_extraction.js +490 -0
- package/es/api/text_extraction.js.map +1 -0
- package/es/core/index.d.ts +1 -0
- package/es/core/index.d.ts.map +1 -1
- package/es/core/index.js +1 -0
- package/es/core/index.js.map +1 -1
- package/es/core/operators/PDFOperator.d.ts +4 -0
- package/es/core/operators/PDFOperator.d.ts.map +1 -1
- package/es/core/operators/PDFOperator.js +8 -0
- package/es/core/operators/PDFOperator.js.map +1 -1
- package/es/core/parser/PDFContentStreamParser.d.ts +29 -0
- package/es/core/parser/PDFContentStreamParser.d.ts.map +1 -0
- package/es/core/parser/PDFContentStreamParser.js +95 -0
- package/es/core/parser/PDFContentStreamParser.js.map +1 -0
- package/es/core/structures/PDFContentStream.d.ts +2 -0
- package/es/core/structures/PDFContentStream.d.ts.map +1 -1
- package/es/core/structures/PDFContentStream.js +4 -0
- package/es/core/structures/PDFContentStream.js.map +1 -1
- package/es/core/structures/PDFPageLeaf.d.ts +2 -0
- package/es/core/structures/PDFPageLeaf.d.ts.map +1 -1
- package/es/core/structures/PDFPageLeaf.js +4 -0
- package/es/core/structures/PDFPageLeaf.js.map +1 -1
- package/package.json +2 -1
- package/src/api/PDFPage.ts +20 -0
- package/src/api/index.ts +1 -0
- package/src/api/text_extraction.ts +511 -0
- package/src/core/index.ts +1 -0
- package/src/core/operators/PDFOperator.ts +10 -0
- package/src/core/parser/PDFContentStreamParser.ts +106 -0
- package/src/core/structures/PDFContentStream.ts +5 -0
- package/src/core/structures/PDFPageLeaf.ts +5 -0
- package/ts3.4/cjs/api/PDFPage.d.ts +12 -0
- package/ts3.4/cjs/api/index.d.ts +1 -0
- package/ts3.4/cjs/api/rotations.d.ts +1 -1
- package/ts3.4/cjs/api/text_extraction.d.ts +20 -0
- package/ts3.4/cjs/core/index.d.ts +1 -0
- package/ts3.4/cjs/core/operators/PDFOperator.d.ts +4 -0
- package/ts3.4/cjs/core/parser/PDFContentStreamParser.d.ts +29 -0
- package/ts3.4/cjs/core/structures/PDFContentStream.d.ts +2 -0
- package/ts3.4/cjs/core/structures/PDFPageLeaf.d.ts +2 -0
- package/ts3.4/cjs/utils/arrays.d.ts +1 -1
- package/ts3.4/es/api/PDFPage.d.ts +12 -0
- package/ts3.4/es/api/index.d.ts +1 -0
- package/ts3.4/es/api/text_extraction.d.ts +20 -0
- package/ts3.4/es/core/index.d.ts +1 -0
- package/ts3.4/es/core/operators/PDFOperator.d.ts +4 -0
- package/ts3.4/es/core/parser/PDFContentStreamParser.d.ts +29 -0
- package/ts3.4/es/core/structures/PDFContentStream.d.ts +2 -0
- package/ts3.4/es/core/structures/PDFPageLeaf.d.ts +2 -0
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
import { Encodings } from '@pdf-lib/standard-fonts';
|
|
2
|
+
import PDFPage from 'src/api/PDFPage';
|
|
3
|
+
import PDFArray from 'src/core/objects/PDFArray';
|
|
4
|
+
import PDFDict from 'src/core/objects/PDFDict';
|
|
5
|
+
import PDFHexString from 'src/core/objects/PDFHexString';
|
|
6
|
+
import PDFName from 'src/core/objects/PDFName';
|
|
7
|
+
import PDFNumber from 'src/core/objects/PDFNumber';
|
|
8
|
+
import PDFObject from 'src/core/objects/PDFObject';
|
|
9
|
+
import PDFRef from 'src/core/objects/PDFRef';
|
|
10
|
+
import PDFStream from 'src/core/objects/PDFStream';
|
|
11
|
+
import PDFString from 'src/core/objects/PDFString';
|
|
12
|
+
import PDFOperator from 'src/core/operators/PDFOperator';
|
|
13
|
+
import PDFContentStream from 'src/core/structures/PDFContentStream';
|
|
14
|
+
import PDFContentStreamParser from 'src/core/parser/PDFContentStreamParser';
|
|
15
|
+
import { decodePDFRawStream } from 'src/core/streams/decode';
|
|
16
|
+
import { pdfDocEncodingDecode } from 'src/utils/pdfDocEncoding';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 从 PDF 页面提取可搜索文本(文本提取 / text extraction)。
|
|
20
|
+
*
|
|
21
|
+
* 上游 pdf-lib 明确不支持此能力(内容流是绘制指令,不是文本模型)。本实现
|
|
22
|
+
* 是「尽力而为」的还原器:解释内容流里的文本显示算子(Tj/TJ/'/"),按字体
|
|
23
|
+
* 编码把字节还原成 Unicode,并按文本定位算子(Td、TD、T-star、Tm)插入换行。
|
|
24
|
+
*
|
|
25
|
+
* 能力边界(诚实说明,不夸大):
|
|
26
|
+
* · 标准 14 字体(Helvetica/Times/Courier/Symbol/ZapfDingbats):按 WinAnsi/
|
|
27
|
+
* Symbol/ZapfDingbats 编码精确还原;
|
|
28
|
+
* · 嵌入字体:优先用其 ToUnicode CMap 还原;无 ToUnicode 时回退 PDFDocEncoding;
|
|
29
|
+
* · 不处理旋转文本、CID 无 ToUnicode 字体、字形替换等极复杂情况——这些会
|
|
30
|
+
* 尽力返回可读内容,但不保证与原文逐字一致。
|
|
31
|
+
*
|
|
32
|
+
* 返回的文本按内容流绘制顺序拼接,用换行分隔不同的文本行;调用方可按需
|
|
33
|
+
* 进一步做布局分析。
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
type Decoder = (bytes: Uint8Array) => string;
|
|
37
|
+
|
|
38
|
+
/** 为某一种标准字体编码建立 byte → 字符 的反查表。 */
|
|
39
|
+
const buildStandardDecoder = (encoding: typeof Encodings.WinAnsi): Decoder => {
|
|
40
|
+
const table: string[] = new Array(256).fill('');
|
|
41
|
+
for (const cp of encoding.supportedCodePoints) {
|
|
42
|
+
try {
|
|
43
|
+
const { code } = encoding.encodeUnicodeCodePoint(cp);
|
|
44
|
+
if (code >= 0 && code < 256 && !table[code]) {
|
|
45
|
+
table[code] = String.fromCodePoint(cp);
|
|
46
|
+
}
|
|
47
|
+
} catch {
|
|
48
|
+
/* 不可编码的码点跳过 */
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return (bytes: Uint8Array) => {
|
|
52
|
+
let out = '';
|
|
53
|
+
for (let i = 0; i < bytes.length; i++) out += table[bytes[i]] ?? '';
|
|
54
|
+
return out;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const winAnsiDecoder = buildStandardDecoder(Encodings.WinAnsi);
|
|
59
|
+
const symbolDecoder = buildStandardDecoder(Encodings.Symbol);
|
|
60
|
+
const zapfDecoder = buildStandardDecoder(Encodings.ZapfDingbats);
|
|
61
|
+
|
|
62
|
+
/** 根据字体字典的 BaseFont 名选择一个解码器。 */
|
|
63
|
+
const decoderForBaseFont = (baseFont: string): Decoder => {
|
|
64
|
+
const name = baseFont.replace(/^.*[+]/, ''); // 去掉 subset 前缀 "ABCDEF+"
|
|
65
|
+
if (name === 'Symbol') return symbolDecoder;
|
|
66
|
+
if (name === 'ZapfDingbats') return zapfDecoder;
|
|
67
|
+
if (
|
|
68
|
+
name.includes('Courier') ||
|
|
69
|
+
name.includes('Helvetica') ||
|
|
70
|
+
name.includes('Times')
|
|
71
|
+
) {
|
|
72
|
+
return winAnsiDecoder;
|
|
73
|
+
}
|
|
74
|
+
// 其它(嵌入字体且无 ToUnicode):回退 PDFDocEncoding
|
|
75
|
+
return (bytes: Uint8Array) => pdfDocEncodingDecode(bytes);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** 解析 ToUnicode CMap 的 codespace/bfchar/bfrange,构建 code → string 映射。 */
|
|
79
|
+
const decodeToUnicode = (cmapBytes: Uint8Array): Decoder => {
|
|
80
|
+
const text = new TextDecoder('latin1').decode(cmapBytes);
|
|
81
|
+
const map = new Map<string, string>();
|
|
82
|
+
const spaces: { bytes: number; lo: number; hi: number }[] = [];
|
|
83
|
+
let maxCodeBytes = 1;
|
|
84
|
+
|
|
85
|
+
/** 去掉尖括号与空白,保留原始 hex 位数(用于判断 1/2/4 字节 code)。 */
|
|
86
|
+
const parseHex = (s: string): string =>
|
|
87
|
+
s.trim().replace(/^<|>$/g, '').replace(/\s+/g, '');
|
|
88
|
+
const hexToCode = (h: string): number => parseInt(h, 16);
|
|
89
|
+
const codeKey = (hex: string, code: number): string =>
|
|
90
|
+
`${Math.ceil(hex.length / 2)}:${code}`;
|
|
91
|
+
const hexToUnicode = (h: string): string => {
|
|
92
|
+
const clean = h.replace(/\s+/g, '');
|
|
93
|
+
if (clean.length <= 4) return String.fromCodePoint(parseInt(clean, 16));
|
|
94
|
+
// ToUnicode 目标通常是 UTF-16BE 单元序列(4 位一组);超过 BMP 时按码点处理。
|
|
95
|
+
if (clean.length % 4 === 0) {
|
|
96
|
+
let out = '';
|
|
97
|
+
for (let i = 0; i + 4 <= clean.length; i += 4) {
|
|
98
|
+
out += String.fromCharCode(parseInt(clean.slice(i, i + 4), 16));
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
return String.fromCodePoint(parseInt(clean, 16));
|
|
103
|
+
};
|
|
104
|
+
const addCodeSpace = (loHex: string, hiHex: string) => {
|
|
105
|
+
const bytes = Math.ceil(loHex.length / 2);
|
|
106
|
+
const entry = { bytes, lo: hexToCode(loHex), hi: hexToCode(hiHex) };
|
|
107
|
+
spaces.push(entry);
|
|
108
|
+
if (bytes > maxCodeBytes) maxCodeBytes = bytes;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// codespace 决定一个 code 占几个字节。CJK 嵌入字体常见 <0000><ffff>,
|
|
112
|
+
// 必须按 2 字节读,逐字节解会把每个汉字拆成两个错误字符(实测踩到)。
|
|
113
|
+
const spaceRe = /begincodespacerange([\s\S]*?)endcodespacerange/g;
|
|
114
|
+
let m: RegExpExecArray | null;
|
|
115
|
+
m = spaceRe.exec(text);
|
|
116
|
+
while (m) {
|
|
117
|
+
for (const line of m[1].trim().split(/\r?\n/)) {
|
|
118
|
+
// fontkit 生成的是 `<0000><ffff>`(中间无空格),必须按尖括号取 token,
|
|
119
|
+
// 不能用 whitespace split,否则会把两个 hex 粘成一个无效值。
|
|
120
|
+
const tokens = line.match(/<[^>]*>/g);
|
|
121
|
+
if (tokens && tokens.length >= 2) {
|
|
122
|
+
addCodeSpace(parseHex(tokens[0]), parseHex(tokens[1]));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
m = spaceRe.exec(text);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// beginbfchar ... endbfchar
|
|
129
|
+
const bfcharRe = /beginbfchar([\s\S]*?)endbfchar/g;
|
|
130
|
+
m = bfcharRe.exec(text);
|
|
131
|
+
while (m) {
|
|
132
|
+
for (const line of m[1].trim().split(/\r?\n/)) {
|
|
133
|
+
const parts = line.trim().split(/\s+/);
|
|
134
|
+
if (parts.length < 2) continue;
|
|
135
|
+
const srcHex = parseHex(parts[0]);
|
|
136
|
+
map.set(
|
|
137
|
+
codeKey(srcHex, hexToCode(srcHex)),
|
|
138
|
+
hexToUnicode(parseHex(parts[1])),
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
m = bfcharRe.exec(text);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// beginbfrange ... endbfrange
|
|
145
|
+
const bfrangeRe = /beginbfrange([\s\S]*?)endbfrange/g;
|
|
146
|
+
m = bfrangeRe.exec(text);
|
|
147
|
+
while (m) {
|
|
148
|
+
for (const line of m[1].trim().split(/\r?\n/)) {
|
|
149
|
+
const parts = line.trim().split(/\s+/);
|
|
150
|
+
if (parts.length < 3) continue;
|
|
151
|
+
const loHex = parseHex(parts[0]);
|
|
152
|
+
const hiHex = parseHex(parts[1]);
|
|
153
|
+
const lo = hexToCode(loHex);
|
|
154
|
+
const hi = hexToCode(hiHex);
|
|
155
|
+
const bytes = Math.ceil(loHex.length / 2);
|
|
156
|
+
const targetTokens = parts
|
|
157
|
+
.slice(2)
|
|
158
|
+
.join(' ')
|
|
159
|
+
.match(/<[^>]*>/g);
|
|
160
|
+
if (targetTokens && targetTokens.length === hi - lo + 1) {
|
|
161
|
+
for (let c = lo; c <= hi; c++) {
|
|
162
|
+
map.set(
|
|
163
|
+
`${bytes}:${c}`,
|
|
164
|
+
hexToUnicode(parseHex(targetTokens[c - lo])),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
const dstHex = parseHex(parts[2]);
|
|
169
|
+
const base = hexToCode(dstHex);
|
|
170
|
+
for (let c = lo; c <= hi; c++) {
|
|
171
|
+
const target = (base + (c - lo))
|
|
172
|
+
.toString(16)
|
|
173
|
+
.padStart(dstHex.length, '0');
|
|
174
|
+
map.set(`${bytes}:${c}`, hexToUnicode(target));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
m = bfrangeRe.exec(text);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return (bytes: Uint8Array) => {
|
|
182
|
+
let out = '';
|
|
183
|
+
let i = 0;
|
|
184
|
+
while (i < bytes.length) {
|
|
185
|
+
let matched = false;
|
|
186
|
+
// 优先最长的合法 codespace(2 字节域不会误吞 1 字节域)。
|
|
187
|
+
for (let len = maxCodeBytes; len >= 1; len--) {
|
|
188
|
+
if (i + len > bytes.length) continue;
|
|
189
|
+
let code = 0;
|
|
190
|
+
for (let j = 0; j < len; j++) code = (code << 8) | bytes[i + j];
|
|
191
|
+
const inSpace = spaces.some(
|
|
192
|
+
(s) => s.bytes === len && code >= s.lo && code <= s.hi,
|
|
193
|
+
);
|
|
194
|
+
if (!inSpace) continue;
|
|
195
|
+
out += map.get(`${len}:${code}`) ?? '';
|
|
196
|
+
i += len;
|
|
197
|
+
matched = true;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
if (!matched) {
|
|
201
|
+
// codespace 缺失或写错时,退化为单字节,至少不吞掉后续内容。
|
|
202
|
+
out += map.get(`1:${bytes[i]}`) ?? '';
|
|
203
|
+
i += 1;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return out;
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
interface ExtractionState {
|
|
211
|
+
line: string;
|
|
212
|
+
lines: string[];
|
|
213
|
+
decoder: Decoder;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** 把显示文本的参数(PDFString/PDFHexString/PDFArray)解码成字符串。 */
|
|
217
|
+
const decodeShowArg = (arg: any, state: ExtractionState): string => {
|
|
218
|
+
if (arg instanceof PDFHexString || arg instanceof PDFString) {
|
|
219
|
+
return state.decoder((arg as any).asBytes());
|
|
220
|
+
}
|
|
221
|
+
if (arg instanceof PDFArray) {
|
|
222
|
+
// TJ:数组中字符串显示、数字调整字距(负数表示拉开,正数收紧)。
|
|
223
|
+
let out = '';
|
|
224
|
+
for (let i = 0; i < arg.size(); i++) {
|
|
225
|
+
const el = arg.get(i);
|
|
226
|
+
if (el instanceof PDFHexString || el instanceof PDFString) {
|
|
227
|
+
out += state.decoder((el as any).asBytes());
|
|
228
|
+
} else if (el instanceof PDFNumber) {
|
|
229
|
+
// 只在字距调整很大时插入空格,模拟词间停顿(启发式,不精确)。
|
|
230
|
+
if (el.asNumber() < -180) out += ' ';
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
235
|
+
return '';
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const flushLine = (state: ExtractionState) => {
|
|
239
|
+
if (state.line.length) {
|
|
240
|
+
state.lines.push(state.line.replace(/\s+$/, ''));
|
|
241
|
+
state.line = '';
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/** 解释一组算子,做文本提取。 */
|
|
246
|
+
const interpret = (
|
|
247
|
+
operators: PDFOperator[],
|
|
248
|
+
fonts: Map<string, Decoder>,
|
|
249
|
+
): string[] => {
|
|
250
|
+
const state: ExtractionState = {
|
|
251
|
+
line: '',
|
|
252
|
+
lines: [],
|
|
253
|
+
decoder: winAnsiDecoder,
|
|
254
|
+
};
|
|
255
|
+
for (const op of operators) {
|
|
256
|
+
const name = op.getName();
|
|
257
|
+
const args = op.getArgs();
|
|
258
|
+
switch (name) {
|
|
259
|
+
case 'Tf': {
|
|
260
|
+
const fontName = args[0] instanceof PDFName ? args[0].asString() : '';
|
|
261
|
+
state.decoder = fonts.get(fontName) ?? winAnsiDecoder;
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
case 'Tj':
|
|
265
|
+
state.line += decodeShowArg(args[0], state);
|
|
266
|
+
break;
|
|
267
|
+
case 'TJ':
|
|
268
|
+
state.line += decodeShowArg(args[0], state);
|
|
269
|
+
break;
|
|
270
|
+
case '\'': // 换行并显示
|
|
271
|
+
flushLine(state);
|
|
272
|
+
if (args[0]) state.line += decodeShowArg(args[0], state);
|
|
273
|
+
break;
|
|
274
|
+
case '"': // 设置字距/词距 + 换行 + 显示
|
|
275
|
+
flushLine(state);
|
|
276
|
+
if (args[2]) state.line += decodeShowArg(args[2], state);
|
|
277
|
+
break;
|
|
278
|
+
case 'Td':
|
|
279
|
+
case 'TD': {
|
|
280
|
+
// 只有纵向移动(第二个操作数非 0)才换行;同一行内的横向移动不换行。
|
|
281
|
+
const y = args[1] instanceof PDFNumber ? args[1].asNumber() : 0;
|
|
282
|
+
if (y !== 0) flushLine(state);
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
case 'T*':
|
|
286
|
+
flushLine(state);
|
|
287
|
+
break;
|
|
288
|
+
case 'Tm': {
|
|
289
|
+
// 文本矩阵:y 分量(索引 5)变化视为换行。
|
|
290
|
+
const y = args[5] instanceof PDFNumber ? args[5].asNumber() : 0;
|
|
291
|
+
if (
|
|
292
|
+
state.line &&
|
|
293
|
+
(state as any)._lastTmY !== undefined &&
|
|
294
|
+
(state as any)._lastTmY !== y
|
|
295
|
+
) {
|
|
296
|
+
flushLine(state);
|
|
297
|
+
}
|
|
298
|
+
(state as any)._lastTmY = y;
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
case 'ET':
|
|
302
|
+
flushLine(state);
|
|
303
|
+
break;
|
|
304
|
+
default:
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
flushLine(state);
|
|
309
|
+
return state.lines;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 内存文档里字体字典是懒嵌入的:embedFont 只分配 ref,真正 context.assign 发生在 save。
|
|
314
|
+
* 所以 save 前 context.lookup(ref) 为空;此时直接从 PDFFont 内部的 embedder/fontkit 对象
|
|
315
|
+
* 重建 glyphId → Unicode 映射,避免把 CJK 子集 CID 当 WinAnsi 解成乱码。
|
|
316
|
+
*/
|
|
317
|
+
const decoderFromEmbeddedFont = (pdfFont: any): Decoder | undefined => {
|
|
318
|
+
const embedder = pdfFont && pdfFont.embedder;
|
|
319
|
+
if (!embedder) return undefined;
|
|
320
|
+
const font = embedder.font;
|
|
321
|
+
if (!font || !font.characterSet || !font.glyphForCodePoint) return undefined;
|
|
322
|
+
|
|
323
|
+
const glyphToUnicode = new Map<number, string>();
|
|
324
|
+
for (const cp of font.characterSet) {
|
|
325
|
+
const glyph = font.glyphForCodePoint(cp);
|
|
326
|
+
if (!glyph) continue;
|
|
327
|
+
const id =
|
|
328
|
+
typeof embedder.glyphId === 'function'
|
|
329
|
+
? embedder.glyphId(glyph)
|
|
330
|
+
: glyph.id;
|
|
331
|
+
if (typeof id === 'number' && id >= 0 && !glyphToUnicode.has(id)) {
|
|
332
|
+
glyphToUnicode.set(id, String.fromCodePoint(cp));
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (glyphToUnicode.size === 0) return undefined;
|
|
336
|
+
|
|
337
|
+
return (bytes: Uint8Array) => {
|
|
338
|
+
let out = '';
|
|
339
|
+
// Identity-H 编码:每个 glyph id 固定 2 字节。
|
|
340
|
+
for (let i = 0; i + 1 < bytes.length; i += 2) {
|
|
341
|
+
const code = (bytes[i] << 8) | bytes[i + 1];
|
|
342
|
+
out += glyphToUnicode.get(code) ?? '';
|
|
343
|
+
}
|
|
344
|
+
return out;
|
|
345
|
+
};
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/** 从页面 Resources.Font 构建 fontName → Decoder 映射。 */
|
|
349
|
+
const buildFontDecoders = (page: PDFPage): Map<string, Decoder> => {
|
|
350
|
+
const map = new Map<string, Decoder>();
|
|
351
|
+
const context = page.doc.context;
|
|
352
|
+
const resources = page.node.Resources();
|
|
353
|
+
if (!resources) return map;
|
|
354
|
+
const fontsDict = resources.lookupMaybe(PDFName.of('Font'), PDFDict);
|
|
355
|
+
if (!fontsDict) return map;
|
|
356
|
+
|
|
357
|
+
for (const [nameObj, refObj] of fontsDict.entries()) {
|
|
358
|
+
const fontName = nameObj.asString();
|
|
359
|
+
let fontDict: PDFDict | undefined;
|
|
360
|
+
if (refObj instanceof PDFDict) {
|
|
361
|
+
fontDict = refObj;
|
|
362
|
+
} else if (refObj instanceof PDFRef) {
|
|
363
|
+
fontDict = context.lookupMaybe(refObj, PDFDict);
|
|
364
|
+
}
|
|
365
|
+
if (!fontDict) {
|
|
366
|
+
// save 前的内存字体:从 PDFFont 内部的 fontkit 对象重建解码表。
|
|
367
|
+
if (refObj instanceof PDFRef) {
|
|
368
|
+
const internalFonts: any[] = (page.doc as any).fonts || [];
|
|
369
|
+
const pdfFont = internalFonts.find((f) => f && f.ref === refObj);
|
|
370
|
+
const decoder = decoderFromEmbeddedFont(pdfFont);
|
|
371
|
+
if (decoder) {
|
|
372
|
+
map.set(fontName, decoder);
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// ToUnicode CMap 优先。
|
|
380
|
+
// 注意:同 context 里该值可能是 PDFRawStream 本体而非 PDFRef;
|
|
381
|
+
// 用 lookupMaybe(PDFRef) 会对非 ref 抛 UnexpectedObjectTypeError(CJK 嵌入字体实测抓到)。
|
|
382
|
+
const toUnicodeRaw = fontDict.get(PDFName.of('ToUnicode'));
|
|
383
|
+
let cmapStream: PDFStream | undefined;
|
|
384
|
+
if (toUnicodeRaw instanceof PDFStream) cmapStream = toUnicodeRaw;
|
|
385
|
+
else if (toUnicodeRaw instanceof PDFRef) {
|
|
386
|
+
cmapStream = context.lookupMaybe(toUnicodeRaw, PDFStream) as
|
|
387
|
+
| PDFStream
|
|
388
|
+
| undefined;
|
|
389
|
+
}
|
|
390
|
+
if (cmapStream) {
|
|
391
|
+
try {
|
|
392
|
+
// ToUnicode 由 context.flateStream(cmap) 写入,Filter=FlateDecode。
|
|
393
|
+
// 必须先按 PDF 流解码,否则拿到的只是 zlib 压缩字节,CMap 解析必为空。
|
|
394
|
+
const cmapBytes = decodePDFRawStream(cmapStream as any).decode();
|
|
395
|
+
map.set(fontName, decodeToUnicode(cmapBytes));
|
|
396
|
+
continue;
|
|
397
|
+
} catch {
|
|
398
|
+
/* CMap 解析失败,回退到编码推断 */
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const baseFont = fontDict.lookupMaybe(PDFName.of('BaseFont'), PDFName);
|
|
402
|
+
map.set(fontName, decoderForBaseFont(baseFont ? baseFont.asString() : ''));
|
|
403
|
+
}
|
|
404
|
+
return map;
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
/** 把页 Contents(单流或流数组)解析为算子。 */
|
|
408
|
+
const pageOperators = (page: PDFPage): PDFOperator[] => {
|
|
409
|
+
const contents = page.node.Contents();
|
|
410
|
+
if (!contents) return [];
|
|
411
|
+
const context = page.doc.context;
|
|
412
|
+
const streams: PDFObject[] =
|
|
413
|
+
contents instanceof PDFArray
|
|
414
|
+
? Array.from({ length: contents.size() }, (_, i) => contents.get(i))
|
|
415
|
+
: [contents];
|
|
416
|
+
|
|
417
|
+
const ops: PDFOperator[] = [];
|
|
418
|
+
for (const s of streams) {
|
|
419
|
+
const stream =
|
|
420
|
+
s instanceof PDFRef
|
|
421
|
+
? context.lookupMaybe(s, PDFStream)
|
|
422
|
+
: (s as PDFStream);
|
|
423
|
+
if (!stream) continue;
|
|
424
|
+
if (stream instanceof PDFContentStream) {
|
|
425
|
+
ops.push(...stream.getOperators());
|
|
426
|
+
} else {
|
|
427
|
+
try {
|
|
428
|
+
ops.push(
|
|
429
|
+
...PDFContentStreamParser.fromPDFRawStream(
|
|
430
|
+
stream as any,
|
|
431
|
+
context,
|
|
432
|
+
).parseOperators(),
|
|
433
|
+
);
|
|
434
|
+
} catch {
|
|
435
|
+
/* 单个流解析失败跳过,尽量提取其它流 */
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return ops;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
/** 提取页面文本,按行返回。 */
|
|
443
|
+
export const extractTextFromPage = (page: PDFPage): string[] =>
|
|
444
|
+
interpret(pageOperators(page), buildFontDecoders(page));
|
|
445
|
+
|
|
446
|
+
/** 提取页面文本,合并为单个字符串(行间以 \n 连接)。 */
|
|
447
|
+
export const extractText = (page: PDFPage): string =>
|
|
448
|
+
extractTextFromPage(page).join('\n');
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* 删除页面中「解码文本包含给定子串」的文本显示算子(文本删除 / redaction 基础)。
|
|
452
|
+
*
|
|
453
|
+
* 行为:解析页面内容流,找到 Tj / TJ / ' / " 算子中解码后含 `substring` 的,
|
|
454
|
+
* 连同其紧邻的换行算子(T* / ' / " 自带的换行语义)一并移除,其余绘图算子
|
|
455
|
+
* 原样保留,然后用一个新内容流整体替换本页内容。
|
|
456
|
+
*
|
|
457
|
+
* 边界(诚实说明):删除的是「整段显示算子」,不是逐字删除——若一段 Tj 里
|
|
458
|
+
* 只有部分文字匹配,整段都会被移除。这是内容流模型的固有限制,符合
|
|
459
|
+
* pdf-lib 一贯的「绘制指令而非文本模型」语义。
|
|
460
|
+
*
|
|
461
|
+
* @returns 被删除的显示算子数量(0 表示没有匹配,页面未改动)。
|
|
462
|
+
*/
|
|
463
|
+
export const removeTextContaining = (
|
|
464
|
+
page: PDFPage,
|
|
465
|
+
substring: string,
|
|
466
|
+
): number => {
|
|
467
|
+
if (!substring) return 0;
|
|
468
|
+
const fonts = buildFontDecoders(page);
|
|
469
|
+
const ops = pageOperators(page);
|
|
470
|
+
const context = page.doc.context;
|
|
471
|
+
|
|
472
|
+
// 第一遍:标记要删除的算子索引,同时跟踪 Tf 以正确解码后续文本。
|
|
473
|
+
const removeIdx = new Set<number>();
|
|
474
|
+
let activeDecoder: Decoder = winAnsiDecoder;
|
|
475
|
+
for (let i = 0; i < ops.length; i++) {
|
|
476
|
+
const op = ops[i];
|
|
477
|
+
const name = op.getName();
|
|
478
|
+
const args = op.getArgs();
|
|
479
|
+
if (name === 'Tf') {
|
|
480
|
+
const fn = args[0] instanceof PDFName ? args[0].asString() : '';
|
|
481
|
+
activeDecoder = fonts.get(fn) ?? winAnsiDecoder;
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
if (name === 'Tj' || name === 'TJ' || name === '\'' || name === '"') {
|
|
485
|
+
let arg: any;
|
|
486
|
+
if (name === 'Tj' || name === 'TJ') arg = args[0];
|
|
487
|
+
else arg = args[args.length - 1];
|
|
488
|
+
if (arg) {
|
|
489
|
+
const st: ExtractionState = {
|
|
490
|
+
line: '',
|
|
491
|
+
lines: [],
|
|
492
|
+
decoder: activeDecoder,
|
|
493
|
+
};
|
|
494
|
+
const text = decodeShowArg(arg, st);
|
|
495
|
+
if (text.includes(substring)) removeIdx.add(i);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
if (removeIdx.size === 0) return 0;
|
|
500
|
+
|
|
501
|
+
// 第二遍:保留未删除算子,重建内容流。
|
|
502
|
+
const kept = ops.filter((_, i) => !removeIdx.has(i));
|
|
503
|
+
const dict = context.obj({});
|
|
504
|
+
const newStream = PDFContentStream.of(dict, kept);
|
|
505
|
+
const ref = context.register(newStream);
|
|
506
|
+
page.node.setContentStream(ref);
|
|
507
|
+
// 清掉页面缓存的旧内存内容流引用,避免后续 drawText 追加到已废弃的流。
|
|
508
|
+
(page as any).contentStream = undefined;
|
|
509
|
+
(page as any).contentStreamRef = ref;
|
|
510
|
+
return removeIdx.size;
|
|
511
|
+
};
|
package/src/core/index.ts
CHANGED
|
@@ -59,6 +59,7 @@ export { default as PDFOperator } from 'src/core/operators/PDFOperator';
|
|
|
59
59
|
export { default as PDFOperatorNames } from 'src/core/operators/PDFOperatorNames';
|
|
60
60
|
|
|
61
61
|
export { default as PDFObjectParser } from 'src/core/parser/PDFObjectParser';
|
|
62
|
+
export { default as PDFContentStreamParser } from 'src/core/parser/PDFContentStreamParser';
|
|
62
63
|
export { default as PDFObjectStreamParser } from 'src/core/parser/PDFObjectStreamParser';
|
|
63
64
|
export { default as PDFParser } from 'src/core/parser/PDFParser';
|
|
64
65
|
export { default as PDFXRefStreamParser } from 'src/core/parser/PDFXRefStreamParser';
|
|
@@ -29,6 +29,16 @@ class PDFOperator {
|
|
|
29
29
|
this.args = args || [];
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/** 只读访问算子名(如 'Tj' / 'TJ'),供内容流解释与文本提取使用。 */
|
|
33
|
+
getName(): PDFOperatorNames {
|
|
34
|
+
return this.name;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 只读访问算子操作数(返回副本,防止外部改写内部状态)。 */
|
|
38
|
+
getArgs(): PDFOperatorArg[] {
|
|
39
|
+
return this.args.slice();
|
|
40
|
+
}
|
|
41
|
+
|
|
32
42
|
clone(context?: PDFContext): PDFOperator {
|
|
33
43
|
const args = new Array(this.args.length);
|
|
34
44
|
for (let idx = 0, len = args.length; idx < len; idx++) {
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import PDFObject from 'src/core/objects/PDFObject';
|
|
2
|
+
import PDFRawStream from 'src/core/objects/PDFRawStream';
|
|
3
|
+
import PDFOperator from 'src/core/operators/PDFOperator';
|
|
4
|
+
import PDFOperatorNames from 'src/core/operators/PDFOperatorNames';
|
|
5
|
+
import BaseParser from 'src/core/parser/BaseParser';
|
|
6
|
+
import ByteStream from 'src/core/parser/ByteStream';
|
|
7
|
+
import PDFContext from 'src/core/PDFContext';
|
|
8
|
+
import PDFObjectParser from 'src/core/parser/PDFObjectParser';
|
|
9
|
+
import CharCodes from 'src/core/syntax/CharCodes';
|
|
10
|
+
import { IsDelimiter } from 'src/core/syntax/Delimiters';
|
|
11
|
+
import { IsNumeric } from 'src/core/syntax/Numeric';
|
|
12
|
+
import { IsWhitespace } from 'src/core/syntax/Whitespace';
|
|
13
|
+
import { charFromCode } from 'src/utils';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* PDF 内容流(Content Stream)解析器。
|
|
17
|
+
*
|
|
18
|
+
* 上游 pdf-lib 1.x 只能解析「本库自己在内存里 push 的 PDFOperator」,
|
|
19
|
+
* 对从外部 PDF 加载进来的内容流(PDFRawStream 里的 Flate 压缩字节)没有
|
|
20
|
+
* 公开解析入口——这正是它无法实现文本提取的根因。本解析器补上这一能力:
|
|
21
|
+
* 把解码后的内容流字节还原成 PDFOperator[],供文本提取/内容分析复用。
|
|
22
|
+
*
|
|
23
|
+
* 内容流语法 = 若干「操作数 + 关键字」,关键字即算子名(Tj/TJ/BT/...)。
|
|
24
|
+
* 操作数对象(数字/字符串/十六进制串/名字/数组/字典)复用 PDFObjectParser
|
|
25
|
+
* 的对象解析,保证与本库其它解析器行为一致。
|
|
26
|
+
*/
|
|
27
|
+
class PDFContentStreamParser extends BaseParser {
|
|
28
|
+
static forBytes = (bytes: Uint8Array, context: PDFContext) =>
|
|
29
|
+
new PDFContentStreamParser(ByteStream.of(bytes), context);
|
|
30
|
+
|
|
31
|
+
static fromPDFRawStream = (stream: PDFRawStream, context: PDFContext) =>
|
|
32
|
+
new PDFContentStreamParser(ByteStream.fromPDFRawStream(stream), context);
|
|
33
|
+
|
|
34
|
+
private readonly objectParser: PDFObjectParser;
|
|
35
|
+
|
|
36
|
+
constructor(byteStream: ByteStream, context: PDFContext) {
|
|
37
|
+
super(byteStream);
|
|
38
|
+
this.objectParser = PDFObjectParser.forByteStream(byteStream, context);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 解析整个内容流为算子序列。遇到无法识别的 token 跳过而非抛错(容错:真实世界 PDF 很脏)。 */
|
|
42
|
+
parseOperators(): PDFOperator[] {
|
|
43
|
+
const operators: PDFOperator[] = [];
|
|
44
|
+
const args: PDFObject[] = [];
|
|
45
|
+
|
|
46
|
+
while (!this.bytes.done()) {
|
|
47
|
+
this.skipWhitespaceAndComments();
|
|
48
|
+
if (this.bytes.done()) break;
|
|
49
|
+
|
|
50
|
+
const byte = this.bytes.peek();
|
|
51
|
+
|
|
52
|
+
// 对象型操作数:数字、字符串、十六进制串、名字、数组。
|
|
53
|
+
if (
|
|
54
|
+
IsNumeric[byte] ||
|
|
55
|
+
byte === CharCodes.LeftParen ||
|
|
56
|
+
byte === CharCodes.LessThan ||
|
|
57
|
+
byte === CharCodes.ForwardSlash ||
|
|
58
|
+
byte === CharCodes.LeftSquareBracket
|
|
59
|
+
) {
|
|
60
|
+
try {
|
|
61
|
+
const obj = this.objectParser.parseObject();
|
|
62
|
+
args.push(obj);
|
|
63
|
+
continue;
|
|
64
|
+
} catch {
|
|
65
|
+
// 解析失败的坏 token:吞掉一个字节继续,避免死循环。
|
|
66
|
+
this.bytes.next();
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 其余按关键字(算子名)处理:读到分隔符/空白为止。
|
|
72
|
+
const keyword = this.parseKeyword();
|
|
73
|
+
if (!keyword) {
|
|
74
|
+
this.bytes.next();
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
// 布尔/空值在内容流里极少作为操作数出现,但 PDFObjectParser 能解析,
|
|
78
|
+
// 这里若遇到也当操作数(提升兼容性)。
|
|
79
|
+
if (keyword === 'true' || keyword === 'false' || keyword === 'null') {
|
|
80
|
+
// 回退到关键字起点不可行,这里直接丢弃(内容流里几乎不用布尔作算子)。
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
// 必须传副本:下面 args.length = 0 会清空同一数组引用,
|
|
84
|
+
// 若直接传引用,所有算子的操作数都会被抹成空(真跑单测抓到)。
|
|
85
|
+
operators.push(
|
|
86
|
+
PDFOperator.of(keyword as PDFOperatorNames, args.slice() as any),
|
|
87
|
+
);
|
|
88
|
+
args.length = 0;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return operators;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 读取一个关键字 token(字母/符号组成,非空白非分隔符)。 */
|
|
95
|
+
private parseKeyword(): string {
|
|
96
|
+
let value = '';
|
|
97
|
+
while (!this.bytes.done()) {
|
|
98
|
+
const byte = this.bytes.peek();
|
|
99
|
+
if (IsWhitespace[byte] || IsDelimiter[byte]) break;
|
|
100
|
+
value += charFromCode(this.bytes.next());
|
|
101
|
+
}
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default PDFContentStreamParser;
|
|
@@ -28,6 +28,11 @@ class PDFContentStream extends PDFFlateStream {
|
|
|
28
28
|
return PDFContentStream.of(dict.clone(context), operators, encode);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/** 只读访问解析后的算子序列(供文本提取/内容分析使用,返回副本防改写)。 */
|
|
32
|
+
getOperators(): PDFOperator[] {
|
|
33
|
+
return this.operators.slice();
|
|
34
|
+
}
|
|
35
|
+
|
|
31
36
|
getContentsString(): string {
|
|
32
37
|
let value = '';
|
|
33
38
|
for (let idx = 0, len = this.operators.length; idx < len; idx++) {
|
|
@@ -122,6 +122,11 @@ class PDFPageLeaf extends PDFDict {
|
|
|
122
122
|
Contents.push(contentStreamRef);
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
/** 用单个内容流整体替换本页 Contents(供文本编辑/内容流重写使用)。 */
|
|
126
|
+
setContentStream(contentStreamRef: PDFRef): void {
|
|
127
|
+
this.set(PDFName.Contents, contentStreamRef);
|
|
128
|
+
}
|
|
129
|
+
|
|
125
130
|
wrapContentStreams(startStream: PDFRef, endStream: PDFRef): boolean {
|
|
126
131
|
const Contents = this.Contents();
|
|
127
132
|
if (Contents instanceof PDFArray) {
|
|
@@ -534,6 +534,18 @@ export default class PDFPage {
|
|
|
534
534
|
* ```
|
|
535
535
|
* @returns The default position of the page.
|
|
536
536
|
*/
|
|
537
|
+
/**
|
|
538
|
+
* 提取本页的可搜索文本(尽力而为的文本还原)。
|
|
539
|
+
*
|
|
540
|
+
* 解释内容流里的文本显示算子(Tj/TJ/'/"),按字体编码还原为 Unicode,
|
|
541
|
+
* 并按文本定位算子插入换行。支持标准 14 字体与带 ToUnicode 的嵌入字体;
|
|
542
|
+
* 对无 ToUnicode 的 CID 字体/旋转文本等复杂情形不保证逐字一致。
|
|
543
|
+
*
|
|
544
|
+
* @returns 按绘制顺序排列的文本行数组(空页返回空数组)。
|
|
545
|
+
*/
|
|
546
|
+
extractTextLines(): string[];
|
|
547
|
+
/** 提取本页文本,行间以 `\n` 连接(extractTextLines 的字符串便捷版)。 */
|
|
548
|
+
extractText(): string;
|
|
537
549
|
getPosition(): {
|
|
538
550
|
x: number;
|
|
539
551
|
y: number;
|