-
Notifications
You must be signed in to change notification settings - Fork 376
/
pdfparser.js
320 lines (278 loc) · 8.61 KB
/
pdfparser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import fs from "fs";
import nodeUtil from "util";
import { readFile } from "fs/promises";
import { EventEmitter } from "events";
import { Buffer } from "buffer";
import PDFJS from "./lib/pdf.js";
import { ParserStream, StringifyStream } from "./lib/parserstream.js";
import { kColors, kFontFaces, kFontStyles } from "./lib/pdfconst.js";
import { pkInfo, _PARSER_SIG } from "./lib/pkinfo.js";
import PDFUnit from "./lib/pdfunit.js";
/**
* Class representing a PDF Parser.
* @class PDFParser
* @extends EventEmitter
*/
export default class PDFParser extends EventEmitter {
/**
* Static method to retrieve color dictionary.
* @returns {object} Color dictionary
*/
static get colorDict() {
return kColors;
}
/**
* Static method to retrieve font face dictionary.
* @returns {object} Font face dictionary
*/
static get fontFaceDict() {
return kFontFaces;
}
/**
* Static method to retrieve font style dictionary.
* @returns {object} Font style dictionary
*/
static get fontStyleDict() {
return kFontStyles;
}
/**
* static property to expose PDFUnit class
* @returns {PDFUnit} PDFUnit class
*/
static get PDFUnit() {
return PDFUnit;
}
/**
* static property to expose ParserStream class
*/
static get ParserStream() {
return ParserStream;
}
/**
* static property to expose StringifyStream class
*/
static get StringifyStream() {
return StringifyStream;
}
/**
* static property to expose pkInfo function
*/
static get pkInfo() {
return pkInfo;
}
/**
* static property to expose _PARSER_SIG string
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
static get _PARSER_SIG() {
return _PARSER_SIG;
}
static #maxBinBufferCount = 10;
static #binBuffer = {};
#password = "";
#context = null; // service context object, only used in Web Service project; null in command line #pdfFilePath = null;
#pdfFilePath = null; //current PDF file to load and parse, null means loading/parsing not started #data = null;
#pdfFileMTime = null; // last time the current pdf was modified, used to recognize changes and ignore cache #PDFJS = null;
#data = null; //if file read success, data is PDF content; if failed, data is "err" object #processFieldInfoXML = false;
#PDFJS = null; //will be initialized in constructor
#processFieldInfoXML = false; //disable additional _fieldInfo.xml parsing and merging (do NOT set to true)
/**
* PDFParser constructor.
* @constructor PDFParser class.
* @param {object} context - The context object (only used in Web Service project); null in command line
* @param {boolean} needRawText - Whether raw text is needed or not
* @param {string} password - The password for PDF file
* @info Private methods accessible using the [funcName].call(this, ...) syntax
*/
constructor(context, needRawText, password) {
super();
this.#context = context;
this.#pdfFilePath = null; //current PDF file to load and parse, null means loading/parsing not started this.#pdfFileMTime = null;
this.#pdfFileMTime = null; // last time the current pdf was modified, used to recognize changes and ignore cache this.#data = null;
this.#data = null; //if file read success, data is PDF content; if failed, data is "err" object this.#processFieldInfoXML = false;
this.#processFieldInfoXML = false; //disable additional _fieldInfo.xml parsing and merging (do NOT set to true)
this.#PDFJS = new PDFJS(needRawText);
this.#password = password;
}
/**
* @private
* @param {object} data - The parsed data
*/
#onPDFJSParseDataReady(data) {
if (!data) {
nodeUtil.p2jinfo("PDF parsing completed.");
this.emit("pdfParser_dataReady", this.#data);
} else {
this.#data = { ...this.#data, ...data };
}
}
/**
* @private
* @param {Error} err - The error object
*/
#onPDFJSParserDataError(err) {
this.#data = null;
this.emit("pdfParser_dataError", { parserError: err });
}
/**
* @private
* @param {Buffer} buffer - The PDF buffer
*/
#startParsingPDF(buffer) {
this.#data = {};
this.#PDFJS.on("pdfjs_parseDataReady", (data) =>
this.#onPDFJSParseDataReady(data)
);
this.#PDFJS.on("pdfjs_parseDataError", (err) =>
this.#onPDFJSParserDataError(err)
);
//v1.3.0 the following Readable Stream-like events are replacement for the top two custom events
this.#PDFJS.on("readable", (meta) => this.emit("readable", meta));
this.#PDFJS.on("data", (data) => this.emit("data", data));
this.#PDFJS.on("error", (err) => this.#onPDFJSParserDataError(err));
this.#PDFJS.parsePDFData(
buffer || PDFParser.#binBuffer[this.binBufferKey],
this.#password
);
}
/**
* @private
* @returns {boolean}
*/
#processBinaryCache() {
if (this.binBufferKey in PDFParser.#binBuffer) {
this.#startParsingPDF();
return true;
}
const allKeys = Object.keys(PDFParser.#binBuffer);
if (allKeys.length > PDFParser.#maxBinBufferCount) {
const idx = this.id % PDFParser.#maxBinBufferCount;
const key = allKeys[idx];
PDFParser.#binBuffer[key] = null;
delete PDFParser.#binBuffer[key];
nodeUtil.p2jinfo(`re-cycled cache for ${key}`);
}
return false;
}
/**
* Getter for #data
* @returns {object|null} Data
*/
get data() {
return this.#data;
}
/**
* Getter for binBufferKey
* @returns {string} The binBufferKey
*/
get binBufferKey() {
return this.#pdfFilePath + this.#pdfFileMTime;
}
/**
* Creates a parser stream
* @returns {ParserStream} A new parser stream
*/
createParserStream() {
return new ParserStream(this, { objectMode: true, bufferSize: 64 * 1024 });
}
/**
* Asynchronously load a PDF from a file path.
* @param {string} pdfFilePath - Path of the PDF file
* @param {number} verbosity - Verbosity level
*/
async loadPDF(pdfFilePath, verbosity) {
nodeUtil.verbosity(verbosity || 0);
nodeUtil.p2jinfo(`about to load PDF file ${pdfFilePath}`);
this.#pdfFilePath = pdfFilePath;
try {
this.#pdfFileMTime = fs.statSync(pdfFilePath).mtimeMs;
if (this.#processFieldInfoXML) {
this.#PDFJS.tryLoadFieldInfoXML(pdfFilePath);
}
if (this.#processBinaryCache()) return;
PDFParser.#binBuffer[this.binBufferKey] = await readFile(pdfFilePath);
nodeUtil.p2jinfo(`Load OK: ${pdfFilePath}`);
this.#startParsingPDF();
} catch (err) {
nodeUtil.p2jerror(`Load Failed: ${pdfFilePath} - ${err}`);
this.emit("pdfParser_dataError", err);
}
}
/**
* Parse PDF buffer. Introduce a way to directly process buffers without the need to write it to a temporary file
* @param {Buffer} pdfBuffer - PDF buffer
* @param {number} verbosity - Verbosity level, ERRORS = 0, WARNINGS = 1, INFOS = 5;
*/
parseBuffer(pdfBuffer, verbosity) {
nodeUtil.verbosity(verbosity); // validated in util.js
if ((!pdfBuffer?.length) || (!pdfBuffer.buffer)) {
nodeUtil.p2jerror("Error: empty PDF buffer, nothing to parse.");
return;
}
let pdfBufferParse = pdfBuffer;
if (pdfBufferParse.buffer.byteLength !== pdfBufferParse.length) {
pdfBufferParse = Buffer.from(pdfBufferParse.buffer, 0, pdfBufferParse.byteLength);
}
this.#startParsingPDF(pdfBufferParse);
}
/**
* Retrieve raw text content from PDF.
* @returns {string} Raw text content
*/
getRawTextContent() {
return this.#PDFJS.getRawTextContent();
}
/**
* Retrieve raw text content stream.
* @returns {Stream} Raw text content stream
*/
getRawTextContentStream() {
return ParserStream.createContentStream(this.getRawTextContent());
}
/**
* Retrieve all field types.
* @returns {object[]} All field types
*/
getAllFieldsTypes() {
return this.#PDFJS.getAllFieldsTypes();
}
/**
* Retrieve all field types stream.
* @returns {Stream} All field types stream
*/
getAllFieldsTypesStream() {
return ParserStream.createContentStream(this.getAllFieldsTypes());
}
/**
* Retrieve merged text blocks if needed.
* @returns {object} Merged text blocks
*/
getMergedTextBlocksIfNeeded() {
return this.#PDFJS.getMergedTextBlocksIfNeeded();
}
/**
* Retrieve merged text blocks stream.
* @returns {Stream} Merged text blocks stream
*/
getMergedTextBlocksStream() {
return ParserStream.createContentStream(this.getMergedTextBlocksIfNeeded());
}
/**
* Destroy the PDFParser instance.
*/
destroy() {
// invoked with stream transform process
super.removeAllListeners();
//context object will be set in Web Service project, but not in command line utility
if (this.#context) {
this.#context.destroy();
this.#context = null;
}
this.#pdfFilePath = null;
this.#pdfFileMTime = null;
this.#data = null;
this.#processFieldInfoXML = false; //disable additional _fieldInfo.xml parsing and merging (do NOT set to true)
this.#PDFJS.destroy();
this.#PDFJS = null;
}
}