officeparser 5.1.1 → 5.2.1

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/officeParser.js CHANGED
@@ -6,7 +6,6 @@ const concat = require('concat-stream');
6
6
  const { DOMParser } = require('@xmldom/xmldom');
7
7
  const fileType = require('file-type');
8
8
  const fs = require('fs');
9
- const pdfjs = require('./pdfjs-dist-build/pdf.js');
10
9
  const yauzl = require('yauzl');
11
10
 
12
11
  /** Header for error messages */
@@ -271,8 +270,10 @@ function parseExcel(file, callback, config) {
271
270
  ? sharedStrings[value]
272
271
  : value;
273
272
  }
274
- // TODO: Add debug asserts for if we reach here which would mean we are filtering more items than we are processing.
275
- // Not the case now but it could happen and it is better to be safe.
273
+ // Should not reach here. If we do, it means we are not filtering out items that we are not ready to process.
274
+ // Not the case now but it could happen if we change the filtering logic without updating the processing logic.
275
+ // So, it is better to error out here.
276
+ handleError(`Invalid c node found in sheet xml content: ${cNode}`, callback, config.outputErrorToConsole);
276
277
  return '';
277
278
  })
278
279
  // Join each cell text within a sheet with a space.
@@ -442,14 +443,18 @@ function parseOpenOffice(file, callback, config) {
442
443
  * @param {string | Buffer} file File path or Buffers
443
444
  * @param {function} callback Callback function that returns value or error
444
445
  * @param {OfficeParserConfig} config Config Object for officeParser
445
- * @returns {void}
446
+ * @returns {Promise<void>}
446
447
  */
447
- function parsePdf(file, callback, config) {
448
- // Get the pdfjs document for the filepath or buffers.
449
- // @ts-ignore
450
- pdfjs.getDocument(file).promise
448
+ async function parsePdf(file, callback, config) {
449
+ // Wait for pdfjs module to be loaded once
450
+ // Lazy import pdfjs to avoid Node startup issues for environments that don't use PDF parsing
451
+ const pdfjs = await import('pdfjs-dist/legacy/build/pdf.mjs');
452
+
453
+ // Get the pdfjs document for the filepath or Uint8Array buffers.
454
+ // pdfjs does not accept Buffers directly, so we convert them to Uint8Array.
455
+ pdfjs.getDocument(file instanceof Buffer ? new Uint8Array(file) : file).promise
451
456
  // We go through each page and build our text content promise array.
452
- .then(document => Promise.all(Array.from({ length: document.numPages }, (_, index) => index + 1).map(pageNr => document.getPage(pageNr).then(page => page.getTextContent()))))
457
+ .then(document => Promise.all(Array.from({ length: document.numPages }, (_, index) => document.getPage(index + 1).then(page => page.getTextContent()))))
453
458
  // Each textContent item has property 'items' which is an array of objects.
454
459
  // Each object element in the array has text stored in their 'str' key.
455
460
  // The concatenation of str is what makes our pdf content.
@@ -462,17 +467,23 @@ function parsePdf(file, callback, config) {
462
467
  const responseText = textContentArray
463
468
  .map(textContent => textContent.items) // Get all the items
464
469
  .flat() // Flatten all the items object
465
- .filter(item => item.str != '') // Ignore the empty string items.
466
- .reduce((a, v) => (
467
- {
468
- text: a.text + (v.transform[5] != a.transform5 ? (config.newlineDelimiter ?? "\n") : '') + v.str,
469
- transform5: v.transform[5]
470
- }),
471
- {
472
- text: '',
473
- transform5: undefined
474
- }).text;
475
-
470
+ .reduce((a, v) => (
471
+ // the items could be TextItem or a TextMarkedContent.
472
+ // We are only interested in the TextItem which has a str property.
473
+ 'str' in v && v.str != ''
474
+ ? {
475
+ text: a.text + (v.transform[5] != a.transform5 ? (config.newlineDelimiter ?? "\n") : '') + v.str,
476
+ transform5: v.transform[5]
477
+ } : {
478
+ text: a.text,
479
+ transform5: a.transform5
480
+ }
481
+ ),
482
+ {
483
+ text: '',
484
+ transform5: undefined
485
+ }).text;
486
+
476
487
  callback(responseText, undefined);
477
488
  })
478
489
  .catch(e => callback(undefined, e));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "officeparser",
3
- "version": "5.1.1",
3
+ "version": "5.2.1",
4
4
  "description": "A Node.js library to parse text out of any office file. Currently supports docx, pptx, xlsx, odt, odp, ods, pdf files.",
5
5
  "main": "officeParser.js",
6
6
  "files": [
@@ -47,6 +47,7 @@
47
47
  "concat-stream": "^2.0.0",
48
48
  "file-type": "^16.5.4",
49
49
  "node-ensure": "^0.0.0",
50
+ "pdfjs-dist": "^5.3.31",
50
51
  "yauzl": "^3.1.3"
51
52
  },
52
53
  "devDependencies": {
@@ -18,16 +18,16 @@ export type OfficeParserConfig = {
18
18
  putNotesAtLast?: boolean;
19
19
  };
20
20
  /** Main async function with callback to execute parseOffice for supported files
21
- * @param {string | Buffer} file File path or file buffers
22
- * @param {function} callback Callback function that returns value or error
23
- * @param {OfficeParserConfig} [config={}] [OPTIONAL]: Config Object for officeParser
21
+ * @param {string | Buffer | ArrayBuffer} srcFile File path or file buffers or Javascript ArrayBuffer
22
+ * @param {function} callback Callback function that returns value or error
23
+ * @param {OfficeParserConfig} [config={}] [OPTIONAL]: Config Object for officeParser
24
24
  * @returns {void}
25
25
  */
26
- export function parseOffice(file: string | Buffer, callback: Function, config?: OfficeParserConfig): void;
26
+ export function parseOffice(srcFile: string | Buffer | ArrayBuffer, callback: Function, config?: OfficeParserConfig): void;
27
27
  /** Main async function that can be used with await to execute parseOffice. Or it can be used with promises.
28
- * @param {string | Buffer} file File path or file buffers
29
- * @param {OfficeParserConfig} [config={}] [OPTIONAL]: Config Object for officeParser
28
+ * @param {string | Buffer | ArrayBuffer} srcFile File path or file buffers or Javascript ArrayBuffer
29
+ * @param {OfficeParserConfig} [config={}] [OPTIONAL]: Config Object for officeParser
30
30
  * @returns {Promise<string>}
31
31
  */
32
- export function parseOfficeAsync(file: string | Buffer, config?: OfficeParserConfig): Promise<string>;
32
+ export function parseOfficeAsync(srcFile: string | Buffer | ArrayBuffer, config?: OfficeParserConfig): Promise<string>;
33
33
  //# sourceMappingURL=officeParser.d.ts.map