node-poppler 8.0.1 → 8.0.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.
Files changed (3) hide show
  1. package/LICENSE +1 -1
  2. package/package.json +2 -2
  3. package/src/index.js +382 -447
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019-2025 Frazer Smith
3
+ Copyright (c) 2019-present Frazer Smith
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-poppler",
3
- "version": "8.0.1",
3
+ "version": "8.0.3",
4
4
  "description": "Asynchronous Node.js wrapper for the Poppler PDF rendering library",
5
5
  "keywords": [
6
6
  "async",
@@ -51,6 +51,6 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "camelcase": "^6.3.0",
54
- "semver": "^7.6.3"
54
+ "semver": "^7.7.2"
55
55
  }
56
56
  }
package/src/index.js CHANGED
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
2
 
3
3
  const { execFile, spawn, spawnSync } = require("node:child_process");
4
+ const { normalize, resolve: pathResolve } = require("node:path");
5
+ const { platform } = require("node:process");
4
6
  const { promisify } = require("node:util");
5
7
  const camelCase = require("camelcase");
6
8
  const { lt } = require("semver");
7
- const { normalize, resolve: pathResolve } = require("node:path");
8
9
 
9
10
  const execFileAsync = promisify(execFile);
10
11
 
11
- const errorMessages = {
12
+ const ERROR_MSGS = {
12
13
  0: "No Error",
13
14
  1: "Error opening a PDF file",
14
15
  2: "Error opening an output file",
@@ -19,8 +20,9 @@ const errorMessages = {
19
20
  };
20
21
 
21
22
  // Cache immutable regex as they are expensive to create and garbage collect
22
- const popplerVersionRegex = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u;
23
- const pdfInfoFileSizesRegex = /(File\s+size:\s+)0(\s+)bytes/u;
23
+ const POPPLER_VERSION_REG = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u;
24
+ const PDF_INFO_FILE_SIZES_REG = /(File\s+size:\s+)0(\s+)bytes/u;
25
+ const PDF_INFO_PATH_REG = /(.+)pdfinfo/u;
24
26
 
25
27
  /**
26
28
  * @typedef {object} OptionDetails
@@ -50,10 +52,8 @@ function parseOptions(acceptedOptions, options, version) {
50
52
  const args = [];
51
53
  /** @type {string[]} */
52
54
  const invalidArgs = [];
53
- /**
54
- * Imperative loops are faster than functional loops.
55
- * @see {@link https://romgrk.com/posts/optimizing-javascript#3-avoid-arrayobject-methods || Optimizing JavaScript}
56
- */
55
+
56
+ // Imperative loops are faster than functional loops, see https://romgrk.com/posts/optimizing-javascript
57
57
  const entries = Object.entries(options);
58
58
  const entriesLength = entries.length;
59
59
  for (let i = 0; i < entriesLength; i += 1) {
@@ -63,7 +63,6 @@ function parseOptions(acceptedOptions, options, version) {
63
63
  const option = entries[i][1];
64
64
  const acceptedOption = acceptedOptions[key];
65
65
 
66
- // eslint-disable-next-line valid-typeof -- `type` is a string
67
66
  if (acceptedOption.type === typeof option) {
68
67
  // Skip boolean options if false
69
68
  if (acceptedOption.type !== "boolean" || option) {
@@ -122,12 +121,10 @@ class Poppler {
122
121
  /** @type {string|undefined} */
123
122
  this.#popplerPath = binPath;
124
123
  } else {
125
- const { platform } = process;
126
-
127
124
  const which = spawnSync(platform === "win32" ? "where" : "which", [
128
125
  "pdfinfo",
129
126
  ]).stdout.toString();
130
- const popplerPath = /(.+)pdfinfo/u.exec(which)?.[1];
127
+ const popplerPath = PDF_INFO_PATH_REG.exec(which)?.[1];
131
128
 
132
129
  if (popplerPath) {
133
130
  this.#popplerPath = popplerPath;
@@ -147,7 +144,7 @@ class Poppler {
147
144
  /* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
148
145
  if (!this.#popplerPath) {
149
146
  throw new Error(
150
- `Unable to find ${process.platform} Poppler binaries, please pass the installation directory as a parameter to the Poppler instance.`
147
+ `Unable to find ${platform} Poppler binaries, please pass the installation directory as a parameter to the Poppler instance.`
151
148
  );
152
149
  }
153
150
  this.#popplerPath = normalize(this.#popplerPath);
@@ -179,18 +176,14 @@ class Poppler {
179
176
  replace: { arg: "-replace", type: "boolean" },
180
177
  };
181
178
 
182
- try {
183
- const args = parseOptions(acceptedOptions, options);
184
- args.push(file, fileToAttach, outputFile);
179
+ const args = parseOptions(acceptedOptions, options);
180
+ args.push(file, fileToAttach, outputFile);
185
181
 
186
- const { stdout } = await execFileAsync(
187
- pathResolve(this.#popplerPath, "pdfattach"),
188
- args
189
- );
190
- return Promise.resolve(stdout);
191
- } catch (err) {
192
- return Promise.reject(err);
193
- }
182
+ const { stdout } = await execFileAsync(
183
+ pathResolve(this.#popplerPath, "pdfattach"),
184
+ args
185
+ );
186
+ return stdout;
194
187
  }
195
188
 
196
189
  /**
@@ -237,18 +230,14 @@ class Poppler {
237
230
  userPassword: { arg: "-upw", type: "string" },
238
231
  };
239
232
 
240
- try {
241
- const args = parseOptions(acceptedOptions, options);
242
- args.push(file);
233
+ const args = parseOptions(acceptedOptions, options);
234
+ args.push(file);
243
235
 
244
- const { stdout } = await execFileAsync(
245
- pathResolve(this.#popplerPath, "pdfdetach"),
246
- args
247
- );
248
- return Promise.resolve(stdout);
249
- } catch (err) {
250
- return Promise.reject(err);
251
- }
236
+ const { stdout } = await execFileAsync(
237
+ pathResolve(this.#popplerPath, "pdfdetach"),
238
+ args
239
+ );
240
+ return stdout;
252
241
  }
253
242
 
254
243
  /**
@@ -276,65 +265,61 @@ class Poppler {
276
265
  userPassword: { arg: "-upw", type: "string" },
277
266
  };
278
267
 
279
- try {
280
- const { stderr } = await execFileAsync(
281
- pathResolve(this.#popplerPath, "pdffonts"),
282
- ["-v"]
283
- );
268
+ const { stderr } = await execFileAsync(
269
+ pathResolve(this.#popplerPath, "pdffonts"),
270
+ ["-v"]
271
+ );
284
272
 
285
- // @ts-ignore: parseOptions checks if falsy
286
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
273
+ // @ts-ignore: parseOptions checks if falsy
274
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
287
275
 
288
- const args = parseOptions(acceptedOptions, options, versionInfo);
276
+ const args = parseOptions(acceptedOptions, options, versionInfo);
289
277
 
290
- return new Promise((resolve, reject) => {
291
- args.push(Buffer.isBuffer(file) ? "-" : file);
278
+ return new Promise((resolve, reject) => {
279
+ args.push(Buffer.isBuffer(file) ? "-" : file);
292
280
 
293
- const child = spawn(
294
- pathResolve(this.#popplerPath, "pdffonts"),
295
- args
296
- );
281
+ const child = spawn(
282
+ pathResolve(this.#popplerPath, "pdffonts"),
283
+ args
284
+ );
297
285
 
298
- if (Buffer.isBuffer(file)) {
299
- child.stdin.write(file);
300
- child.stdin.end();
301
- }
286
+ if (Buffer.isBuffer(file)) {
287
+ child.stdin.write(file);
288
+ child.stdin.end();
289
+ }
302
290
 
303
- let stdOut = "";
304
- let stdErr = "";
291
+ let stdOut = "";
292
+ let stdErr = "";
305
293
 
306
- child.stdout.on("data", (data) => {
307
- stdOut += data;
308
- });
294
+ child.stdout.on("data", (data) => {
295
+ stdOut += data;
296
+ });
309
297
 
310
- child.stderr.on("data", (data) => {
311
- stdErr += data;
312
- });
298
+ child.stderr.on("data", (data) => {
299
+ stdErr += data;
300
+ });
313
301
 
314
- child.on("close", (code) => {
315
- /* istanbul ignore else */
316
- if (stdOut !== "") {
317
- resolve(stdOut.trim());
318
- } else if (code === 0) {
319
- resolve(errorMessages[code]);
320
- } else if (stdErr !== "") {
321
- reject(new Error(stdErr.trim()));
322
- } else {
323
- reject(
324
- new Error(
325
- // @ts-ignore: Second operand used if code is not in errorMessages
326
- errorMessages[code] ||
327
- `pdffonts ${args.join(
328
- " "
329
- )} exited with code ${code}`
330
- )
331
- );
332
- }
333
- });
302
+ child.on("close", (code) => {
303
+ /* istanbul ignore else */
304
+ if (stdOut !== "") {
305
+ resolve(stdOut.trim());
306
+ } else if (code === 0) {
307
+ resolve(ERROR_MSGS[code]);
308
+ } else if (stdErr !== "") {
309
+ reject(new Error(stdErr.trim()));
310
+ } else {
311
+ reject(
312
+ new Error(
313
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
314
+ ERROR_MSGS[code] ||
315
+ `pdffonts ${args.join(
316
+ " "
317
+ )} exited with code ${code}`
318
+ )
319
+ );
320
+ }
334
321
  });
335
- } catch (err) {
336
- return Promise.reject(err);
337
- }
322
+ });
338
323
  }
339
324
 
340
325
  /**
@@ -379,69 +364,65 @@ class Poppler {
379
364
  userPassword: { arg: "-upw", type: "string" },
380
365
  };
381
366
 
382
- try {
383
- const { stderr } = await execFileAsync(
384
- pathResolve(this.#popplerPath, "pdfimages"),
385
- ["-v"]
386
- );
367
+ const { stderr } = await execFileAsync(
368
+ pathResolve(this.#popplerPath, "pdfimages"),
369
+ ["-v"]
370
+ );
387
371
 
388
- // @ts-ignore: parseOptions checks if falsy
389
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
372
+ // @ts-ignore: parseOptions checks if falsy
373
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
390
374
 
391
- const args = parseOptions(acceptedOptions, options, versionInfo);
375
+ const args = parseOptions(acceptedOptions, options, versionInfo);
392
376
 
393
- return new Promise((resolve, reject) => {
394
- args.push(Buffer.isBuffer(file) ? "-" : file);
377
+ return new Promise((resolve, reject) => {
378
+ args.push(Buffer.isBuffer(file) ? "-" : file);
395
379
 
396
- if (outputPrefix) {
397
- args.push(outputPrefix);
398
- }
380
+ if (outputPrefix) {
381
+ args.push(outputPrefix);
382
+ }
399
383
 
400
- const child = spawn(
401
- pathResolve(this.#popplerPath, "pdfimages"),
402
- args
403
- );
384
+ const child = spawn(
385
+ pathResolve(this.#popplerPath, "pdfimages"),
386
+ args
387
+ );
404
388
 
405
- if (Buffer.isBuffer(file)) {
406
- child.stdin.write(file);
407
- child.stdin.end();
408
- }
389
+ if (Buffer.isBuffer(file)) {
390
+ child.stdin.write(file);
391
+ child.stdin.end();
392
+ }
409
393
 
410
- let stdOut = "";
411
- let stdErr = "";
394
+ let stdOut = "";
395
+ let stdErr = "";
412
396
 
413
- child.stdout.on("data", (data) => {
414
- stdOut += data;
415
- });
397
+ child.stdout.on("data", (data) => {
398
+ stdOut += data;
399
+ });
416
400
 
417
- child.stderr.on("data", (data) => {
418
- stdErr += data;
419
- });
401
+ child.stderr.on("data", (data) => {
402
+ stdErr += data;
403
+ });
420
404
 
421
- child.on("close", (code) => {
422
- /* istanbul ignore else */
423
- if (stdOut !== "") {
424
- resolve(stdOut.trim());
425
- } else if (code === 0) {
426
- resolve(errorMessages[code]);
427
- } else if (stdErr !== "") {
428
- reject(new Error(stdErr.trim()));
429
- } else {
430
- reject(
431
- new Error(
432
- // @ts-ignore: Second operand used if code is not in errorMessages
433
- errorMessages[code] ||
434
- `pdfimages ${args.join(
435
- " "
436
- )} exited with code ${code}`
437
- )
438
- );
439
- }
440
- });
405
+ child.on("close", (code) => {
406
+ /* istanbul ignore else */
407
+ if (stdOut !== "") {
408
+ resolve(stdOut.trim());
409
+ } else if (code === 0) {
410
+ resolve(ERROR_MSGS[code]);
411
+ } else if (stdErr !== "") {
412
+ reject(new Error(stdErr.trim()));
413
+ } else {
414
+ reject(
415
+ new Error(
416
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
417
+ ERROR_MSGS[code] ||
418
+ `pdfimages ${args.join(
419
+ " "
420
+ )} exited with code ${code}`
421
+ )
422
+ );
423
+ }
441
424
  });
442
- } catch (err) {
443
- return Promise.reject(err);
444
- }
425
+ });
445
426
  }
446
427
 
447
428
  /**
@@ -500,103 +481,92 @@ class Poppler {
500
481
  userPassword: { arg: "-upw", type: "string" },
501
482
  };
502
483
 
503
- try {
504
- const { stderr } = await execFileAsync(
505
- pathResolve(this.#popplerPath, "pdfinfo"),
506
- ["-v"]
507
- );
484
+ const { stderr } = await execFileAsync(
485
+ pathResolve(this.#popplerPath, "pdfinfo"),
486
+ ["-v"]
487
+ );
508
488
 
509
- // @ts-ignore: parseOptions checks if falsy
510
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
489
+ // @ts-ignore: parseOptions checks if falsy
490
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
511
491
 
512
- const args = parseOptions(acceptedOptions, options, versionInfo);
492
+ const args = parseOptions(acceptedOptions, options, versionInfo);
513
493
 
514
- /**
515
- * Poppler does not set the "File size" metadata value if passed
516
- * a Buffer via stdin, so need to retrieve it from the Buffer.
517
- */
518
- /** @type {number} */
519
- let fileSize;
494
+ // Fetch file size if stdin input is a Buffer, as Poppler omits it
495
+ /** @type {number} */
496
+ let fileSize;
520
497
 
521
- return new Promise((resolve, reject) => {
522
- if (Buffer.isBuffer(file)) {
523
- args.push("-");
524
- fileSize = file.length;
525
- } else {
526
- args.push(file);
527
- }
498
+ return new Promise((resolve, reject) => {
499
+ if (Buffer.isBuffer(file)) {
500
+ args.push("-");
501
+ fileSize = file.length;
502
+ } else {
503
+ args.push(file);
504
+ }
528
505
 
529
- const child = spawn(
530
- pathResolve(this.#popplerPath, "pdfinfo"),
531
- args
532
- );
506
+ const child = spawn(
507
+ pathResolve(this.#popplerPath, "pdfinfo"),
508
+ args
509
+ );
533
510
 
534
- if (Buffer.isBuffer(file)) {
535
- child.stdin.write(file);
536
- child.stdin.end();
537
- }
511
+ if (Buffer.isBuffer(file)) {
512
+ child.stdin.write(file);
513
+ child.stdin.end();
514
+ }
538
515
 
539
- let stdOut = "";
540
- let stdErr = "";
516
+ let stdOut = "";
517
+ let stdErr = "";
541
518
 
542
- child.stdout.on("data", (data) => {
543
- stdOut += data;
544
- });
519
+ child.stdout.on("data", (data) => {
520
+ stdOut += data;
521
+ });
545
522
 
546
- child.stderr.on("data", (data) => {
547
- stdErr += data;
548
- });
523
+ child.stderr.on("data", (data) => {
524
+ stdErr += data;
525
+ });
549
526
 
550
- child.on("close", (code) => {
551
- /* istanbul ignore else */
552
- if (stdOut !== "") {
553
- if (fileSize) {
554
- stdOut = stdOut.replace(
555
- pdfInfoFileSizesRegex,
556
- `$1${fileSize}$2bytes`
557
- );
558
- }
527
+ child.on("close", (code) => {
528
+ /* istanbul ignore else */
529
+ if (stdOut !== "") {
530
+ if (fileSize) {
531
+ stdOut = stdOut.replace(
532
+ PDF_INFO_FILE_SIZES_REG,
533
+ `$1${fileSize}$2bytes`
534
+ );
535
+ }
559
536
 
560
- /**
561
- * Convert output to JSON.
562
- * @see {@link https://github.com/Fdawgs/node-poppler/issues/248#issuecomment-845948080 | Node-Poppler Issue #248}
563
- */
564
- if (options.printAsJson === true) {
565
- const info = {};
566
- const stdOutLines = stdOut.split("\n");
567
- const stdOutLinesLength = stdOutLines.length;
568
- for (let i = 0; i < stdOutLinesLength; i += 1) {
569
- const line = stdOutLines[i];
570
- const lines = line.split(": ");
571
- if (lines.length > 1) {
572
- // @ts-ignore: creating dynamic object keys
573
- info[camelCase(lines[0])] = lines[1].trim();
574
- }
537
+ if (options.printAsJson === true) {
538
+ const info = {};
539
+ const stdOutLines = stdOut.split("\n");
540
+ const stdOutLinesLength = stdOutLines.length;
541
+ for (let i = 0; i < stdOutLinesLength; i += 1) {
542
+ const line = stdOutLines[i];
543
+ const lines = line.split(": ");
544
+ if (lines.length > 1) {
545
+ // @ts-ignore: creating dynamic object keys
546
+ info[camelCase(lines[0])] = lines[1].trim();
575
547
  }
576
- resolve(info);
577
- } else {
578
- resolve(stdOut.trim());
579
548
  }
580
- } else if (code === 0) {
581
- resolve(errorMessages[code]);
582
- } else if (stdErr !== "") {
583
- reject(new Error(stdErr.trim()));
549
+ resolve(info);
584
550
  } else {
585
- reject(
586
- new Error(
587
- // @ts-ignore: Second operand used if code is not in errorMessages
588
- errorMessages[code] ||
589
- `pdfinfo ${args.join(
590
- " "
591
- )} exited with code ${code}`
592
- )
593
- );
551
+ resolve(stdOut.trim());
594
552
  }
595
- });
553
+ } else if (code === 0) {
554
+ resolve(ERROR_MSGS[code]);
555
+ } else if (stdErr !== "") {
556
+ reject(new Error(stdErr.trim()));
557
+ } else {
558
+ reject(
559
+ new Error(
560
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
561
+ ERROR_MSGS[code] ||
562
+ `pdfinfo ${args.join(
563
+ " "
564
+ )} exited with code ${code}`
565
+ )
566
+ );
567
+ }
596
568
  });
597
- } catch (err) {
598
- return Promise.reject(err);
599
- }
569
+ });
600
570
  }
601
571
 
602
572
  /**
@@ -624,26 +594,22 @@ class Poppler {
624
594
  printVersionInfo: { arg: "-v", type: "boolean" },
625
595
  };
626
596
 
627
- try {
628
- const { stderr } = await execFileAsync(
629
- pathResolve(this.#popplerPath, "pdfseparate"),
630
- ["-v"]
631
- );
597
+ const { stderr } = await execFileAsync(
598
+ pathResolve(this.#popplerPath, "pdfseparate"),
599
+ ["-v"]
600
+ );
632
601
 
633
- // @ts-ignore: parseOptions checks if falsy
634
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
602
+ // @ts-ignore: parseOptions checks if falsy
603
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
635
604
 
636
- const args = parseOptions(acceptedOptions, options, versionInfo);
637
- args.push(file, outputPattern);
605
+ const args = parseOptions(acceptedOptions, options, versionInfo);
606
+ args.push(file, outputPattern);
638
607
 
639
- const { stdout } = await execFileAsync(
640
- pathResolve(this.#popplerPath, "pdfseparate"),
641
- args
642
- );
643
- return Promise.resolve(stdout);
644
- } catch (err) {
645
- return Promise.reject(err);
646
- }
608
+ const { stdout } = await execFileAsync(
609
+ pathResolve(this.#popplerPath, "pdfseparate"),
610
+ args
611
+ );
612
+ return stdout;
647
613
  }
648
614
 
649
615
  /**
@@ -816,7 +782,7 @@ class Poppler {
816
782
  );
817
783
 
818
784
  // @ts-ignore: parseOptions checks if falsy
819
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
785
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
820
786
 
821
787
  const args = parseOptions(acceptedOptions, options, versionInfo);
822
788
 
@@ -859,14 +825,14 @@ class Poppler {
859
825
  if (stdOut !== "") {
860
826
  resolve(stdOut.trim());
861
827
  } else if (code === 0) {
862
- resolve(errorMessages[code]);
828
+ resolve(ERROR_MSGS[code]);
863
829
  } else if (stdErr !== "") {
864
830
  reject(new Error(stdErr.trim()));
865
831
  } else {
866
832
  reject(
867
833
  new Error(
868
- // @ts-ignore: Second operand used if code is not in errorMessages
869
- errorMessages[code] ||
834
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
835
+ ERROR_MSGS[code] ||
870
836
  `pdftocairo ${args.join(
871
837
  " "
872
838
  )} exited with code ${code}`
@@ -952,60 +918,53 @@ class Poppler {
952
918
  zoom: { arg: "-zoom", type: "number" },
953
919
  };
954
920
 
955
- try {
956
- const { stderr } = await execFileAsync(
957
- pathResolve(this.#popplerPath, "pdftohtml"),
958
- ["-v"]
959
- );
921
+ const { stderr } = await execFileAsync(
922
+ pathResolve(this.#popplerPath, "pdftohtml"),
923
+ ["-v"]
924
+ );
960
925
 
961
- // @ts-ignore: parseOptions checks if falsy
962
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
926
+ // @ts-ignore: parseOptions checks if falsy
927
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
963
928
 
964
- const args = parseOptions(acceptedOptions, options, versionInfo);
929
+ const args = parseOptions(acceptedOptions, options, versionInfo);
965
930
 
966
- return new Promise((resolve, reject) => {
967
- args.push(Buffer.isBuffer(file) ? "-" : file);
931
+ return new Promise((resolve, reject) => {
932
+ args.push(Buffer.isBuffer(file) ? "-" : file);
968
933
 
969
- if (outputFile) {
970
- args.push(outputFile);
971
- }
934
+ if (outputFile) {
935
+ args.push(outputFile);
936
+ }
972
937
 
973
- const child = spawn(
974
- pathResolve(this.#popplerPath, "pdftohtml"),
975
- args
976
- );
938
+ const child = spawn(
939
+ pathResolve(this.#popplerPath, "pdftohtml"),
940
+ args
941
+ );
977
942
 
978
- if (Buffer.isBuffer(file)) {
979
- child.stdin.write(file);
980
- child.stdin.end();
981
- }
943
+ if (Buffer.isBuffer(file)) {
944
+ child.stdin.write(file);
945
+ child.stdin.end();
946
+ }
982
947
 
983
- let stdOut = "";
984
- let stdErr = "";
948
+ let stdOut = "";
949
+ let stdErr = "";
985
950
 
986
- child.stdout.on("data", (data) => {
987
- stdOut += data;
988
- });
951
+ child.stdout.on("data", (data) => {
952
+ stdOut += data;
953
+ });
989
954
 
990
- child.stderr.on("data", (data) => {
991
- stdErr += data;
992
- });
955
+ child.stderr.on("data", (data) => {
956
+ stdErr += data;
957
+ });
993
958
 
994
- /**
995
- * PdfToHtml does not return an exit code so check output to see if it was successful.
996
- * @see {@link https://gitlab.freedesktop.org/poppler/poppler/-/blob/master/utils/pdftohtml.1 | Poppler pdftohtml man}
997
- */
998
- child.on("close", () => {
999
- if (stdOut !== "") {
1000
- resolve(stdOut.trim());
1001
- } else {
1002
- reject(new Error(stdErr ? stdErr.trim() : undefined));
1003
- }
1004
- });
959
+ // PdfToHtml has no exit code; check output for success
960
+ child.on("close", () => {
961
+ if (stdOut !== "") {
962
+ resolve(stdOut.trim());
963
+ } else {
964
+ reject(new Error(stdErr ? stdErr.trim() : undefined));
965
+ }
1005
966
  });
1006
- } catch (err) {
1007
- return Promise.reject(err);
1008
- }
967
+ });
1009
968
  }
1010
969
 
1011
970
  /**
@@ -1154,58 +1113,54 @@ class Poppler {
1154
1113
  userPassword: { arg: "-upw", type: "string" },
1155
1114
  };
1156
1115
 
1157
- try {
1158
- const { stderr } = await execFileAsync(
1159
- pathResolve(this.#popplerPath, "pdftoppm"),
1160
- ["-v"]
1161
- );
1116
+ const { stderr } = await execFileAsync(
1117
+ pathResolve(this.#popplerPath, "pdftoppm"),
1118
+ ["-v"]
1119
+ );
1162
1120
 
1163
- // @ts-ignore: parseOptions checks if falsy
1164
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
1121
+ // @ts-ignore: parseOptions checks if falsy
1122
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
1165
1123
 
1166
- const args = parseOptions(acceptedOptions, options, versionInfo);
1124
+ const args = parseOptions(acceptedOptions, options, versionInfo);
1167
1125
 
1168
- return new Promise((resolve, reject) => {
1169
- args.push(Buffer.isBuffer(file) ? "-" : file, outputPath);
1126
+ return new Promise((resolve, reject) => {
1127
+ args.push(Buffer.isBuffer(file) ? "-" : file, outputPath);
1170
1128
 
1171
- const child = spawn(
1172
- pathResolve(this.#popplerPath, "pdftoppm"),
1173
- args
1174
- );
1129
+ const child = spawn(
1130
+ pathResolve(this.#popplerPath, "pdftoppm"),
1131
+ args
1132
+ );
1175
1133
 
1176
- if (Buffer.isBuffer(file)) {
1177
- child.stdin.write(file);
1178
- child.stdin.end();
1179
- }
1134
+ if (Buffer.isBuffer(file)) {
1135
+ child.stdin.write(file);
1136
+ child.stdin.end();
1137
+ }
1180
1138
 
1181
- let stdErr = "";
1139
+ let stdErr = "";
1182
1140
 
1183
- child.stderr.on("data", (data) => {
1184
- stdErr += data;
1185
- });
1141
+ child.stderr.on("data", (data) => {
1142
+ stdErr += data;
1143
+ });
1186
1144
 
1187
- child.on("close", (code) => {
1188
- /* istanbul ignore else */
1189
- if (stdErr !== "") {
1190
- reject(new Error(stdErr.trim()));
1191
- } else if (code === 0) {
1192
- resolve(errorMessages[code]);
1193
- } else {
1194
- reject(
1195
- new Error(
1196
- // @ts-ignore: Second operand used if code is not in errorMessages
1197
- errorMessages[code] ||
1198
- `pdftoppm ${args.join(
1199
- " "
1200
- )} exited with code ${code}`
1201
- )
1202
- );
1203
- }
1204
- });
1145
+ child.on("close", (code) => {
1146
+ /* istanbul ignore else */
1147
+ if (stdErr !== "") {
1148
+ reject(new Error(stdErr.trim()));
1149
+ } else if (code === 0) {
1150
+ resolve(ERROR_MSGS[code]);
1151
+ } else {
1152
+ reject(
1153
+ new Error(
1154
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
1155
+ ERROR_MSGS[code] ||
1156
+ `pdftoppm ${args.join(
1157
+ " "
1158
+ )} exited with code ${code}`
1159
+ )
1160
+ );
1161
+ }
1205
1162
  });
1206
- } catch (err) {
1207
- return Promise.reject(err);
1208
- }
1163
+ });
1209
1164
  }
1210
1165
 
1211
1166
  /**
@@ -1388,68 +1343,61 @@ class Poppler {
1388
1343
  userPassword: { arg: "-upw", type: "string" },
1389
1344
  };
1390
1345
 
1391
- try {
1392
- const { stderr } = await execFileAsync(
1393
- pathResolve(this.#popplerPath, "pdftops"),
1394
- ["-v"]
1395
- );
1346
+ const { stderr } = await execFileAsync(
1347
+ pathResolve(this.#popplerPath, "pdftops"),
1348
+ ["-v"]
1349
+ );
1396
1350
 
1397
- // @ts-ignore: parseOptions checks if falsy
1398
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
1351
+ // @ts-ignore: parseOptions checks if falsy
1352
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
1399
1353
 
1400
- const args = parseOptions(acceptedOptions, options, versionInfo);
1354
+ const args = parseOptions(acceptedOptions, options, versionInfo);
1401
1355
 
1402
- return new Promise((resolve, reject) => {
1403
- args.push(
1404
- Buffer.isBuffer(file) ? "-" : file,
1405
- outputFile || "-"
1406
- );
1356
+ return new Promise((resolve, reject) => {
1357
+ args.push(Buffer.isBuffer(file) ? "-" : file, outputFile || "-");
1407
1358
 
1408
- const child = spawn(
1409
- pathResolve(this.#popplerPath, "pdftops"),
1410
- args
1411
- );
1359
+ const child = spawn(
1360
+ pathResolve(this.#popplerPath, "pdftops"),
1361
+ args
1362
+ );
1412
1363
 
1413
- if (Buffer.isBuffer(file)) {
1414
- child.stdin.write(file);
1415
- child.stdin.end();
1416
- }
1364
+ if (Buffer.isBuffer(file)) {
1365
+ child.stdin.write(file);
1366
+ child.stdin.end();
1367
+ }
1417
1368
 
1418
- let stdOut = "";
1419
- let stdErr = "";
1369
+ let stdOut = "";
1370
+ let stdErr = "";
1420
1371
 
1421
- child.stdout.on("data", (data) => {
1422
- stdOut += data;
1423
- });
1372
+ child.stdout.on("data", (data) => {
1373
+ stdOut += data;
1374
+ });
1424
1375
 
1425
- child.stderr.on("data", (data) => {
1426
- stdErr += data;
1427
- });
1376
+ child.stderr.on("data", (data) => {
1377
+ stdErr += data;
1378
+ });
1428
1379
 
1429
- child.on("close", (code) => {
1430
- /* istanbul ignore else */
1431
- if (stdOut !== "") {
1432
- resolve(stdOut.trim());
1433
- } else if (code === 0) {
1434
- resolve(errorMessages[code]);
1435
- } else if (stdErr !== "") {
1436
- reject(new Error(stdErr.trim()));
1437
- } else {
1438
- reject(
1439
- new Error(
1440
- // @ts-ignore: Second operand used if code is not in errorMessages
1441
- errorMessages[code] ||
1442
- `pdftops ${args.join(
1443
- " "
1444
- )} exited with code ${code}`
1445
- )
1446
- );
1447
- }
1448
- });
1380
+ child.on("close", (code) => {
1381
+ /* istanbul ignore else */
1382
+ if (stdOut !== "") {
1383
+ resolve(stdOut.trim());
1384
+ } else if (code === 0) {
1385
+ resolve(ERROR_MSGS[code]);
1386
+ } else if (stdErr !== "") {
1387
+ reject(new Error(stdErr.trim()));
1388
+ } else {
1389
+ reject(
1390
+ new Error(
1391
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
1392
+ ERROR_MSGS[code] ||
1393
+ `pdftops ${args.join(
1394
+ " "
1395
+ )} exited with code ${code}`
1396
+ )
1397
+ );
1398
+ }
1449
1399
  });
1450
- } catch (err) {
1451
- return Promise.reject(err);
1452
- }
1400
+ });
1453
1401
  }
1454
1402
 
1455
1403
  /**
@@ -1540,70 +1488,61 @@ class Poppler {
1540
1488
  userPassword: { arg: "-upw", type: "string" },
1541
1489
  };
1542
1490
 
1543
- try {
1544
- const { stderr } = await execFileAsync(
1545
- pathResolve(this.#popplerPath, "pdftotext"),
1546
- ["-v"]
1547
- );
1491
+ const { stderr } = await execFileAsync(
1492
+ pathResolve(this.#popplerPath, "pdftotext"),
1493
+ ["-v"]
1494
+ );
1548
1495
 
1549
- // @ts-ignore: parseOptions checks if falsy
1550
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
1496
+ // @ts-ignore: parseOptions checks if falsy
1497
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
1551
1498
 
1552
- const args = parseOptions(acceptedOptions, options, versionInfo);
1499
+ const args = parseOptions(acceptedOptions, options, versionInfo);
1553
1500
 
1554
- return new Promise((resolve, reject) => {
1555
- args.push(
1556
- Buffer.isBuffer(file) ? "-" : file,
1557
- outputFile || "-"
1558
- );
1501
+ return new Promise((resolve, reject) => {
1502
+ args.push(Buffer.isBuffer(file) ? "-" : file, outputFile || "-");
1559
1503
 
1560
- const child = spawn(
1561
- pathResolve(this.#popplerPath, "pdftotext"),
1562
- args
1563
- );
1504
+ const child = spawn(
1505
+ pathResolve(this.#popplerPath, "pdftotext"),
1506
+ args
1507
+ );
1564
1508
 
1565
- if (Buffer.isBuffer(file)) {
1566
- child.stdin.write(file);
1567
- child.stdin.end();
1568
- }
1509
+ if (Buffer.isBuffer(file)) {
1510
+ child.stdin.write(file);
1511
+ child.stdin.end();
1512
+ }
1569
1513
 
1570
- let stdOut = "";
1571
- let stdErr = "";
1514
+ let stdOut = "";
1515
+ let stdErr = "";
1572
1516
 
1573
- child.stdout.on("data", (data) => {
1574
- stdOut += data;
1575
- });
1517
+ child.stdout.on("data", (data) => {
1518
+ stdOut += data;
1519
+ });
1576
1520
 
1577
- child.stderr.on("data", (data) => {
1578
- stdErr += data;
1579
- });
1521
+ child.stderr.on("data", (data) => {
1522
+ stdErr += data;
1523
+ });
1580
1524
 
1581
- child.on("close", (code) => {
1582
- /* istanbul ignore else */
1583
- if (stdOut !== "") {
1584
- resolve(
1585
- options.maintainLayout ? stdOut : stdOut.trim()
1586
- );
1587
- } else if (code === 0) {
1588
- resolve(errorMessages[code]);
1589
- } else if (stdErr !== "") {
1590
- reject(new Error(stdErr.trim()));
1591
- } else {
1592
- reject(
1593
- new Error(
1594
- // @ts-ignore: Second operand used if code is not in errorMessages
1595
- errorMessages[code] ||
1596
- `pdftotext ${args.join(
1597
- " "
1598
- )} exited with code ${code}`
1599
- )
1600
- );
1601
- }
1602
- });
1525
+ child.on("close", (code) => {
1526
+ /* istanbul ignore else */
1527
+ if (stdOut !== "") {
1528
+ resolve(options.maintainLayout ? stdOut : stdOut.trim());
1529
+ } else if (code === 0) {
1530
+ resolve(ERROR_MSGS[code]);
1531
+ } else if (stdErr !== "") {
1532
+ reject(new Error(stdErr.trim()));
1533
+ } else {
1534
+ reject(
1535
+ new Error(
1536
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
1537
+ ERROR_MSGS[code] ||
1538
+ `pdftotext ${args.join(
1539
+ " "
1540
+ )} exited with code ${code}`
1541
+ )
1542
+ );
1543
+ }
1603
1544
  });
1604
- } catch (err) {
1605
- return Promise.reject(err);
1606
- }
1545
+ });
1607
1546
  }
1608
1547
 
1609
1548
  /**
@@ -1623,26 +1562,22 @@ class Poppler {
1623
1562
  printVersionInfo: { arg: "-v", type: "boolean" },
1624
1563
  };
1625
1564
 
1626
- try {
1627
- const { stderr } = await execFileAsync(
1628
- pathResolve(this.#popplerPath, "pdfunite"),
1629
- ["-v"]
1630
- );
1565
+ const { stderr } = await execFileAsync(
1566
+ pathResolve(this.#popplerPath, "pdfunite"),
1567
+ ["-v"]
1568
+ );
1631
1569
 
1632
- // @ts-ignore: parseOptions checks if falsy
1633
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
1570
+ // @ts-ignore: parseOptions checks if falsy
1571
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
1634
1572
 
1635
- const args = parseOptions(acceptedOptions, options, versionInfo);
1636
- args.push(...files, outputFile);
1573
+ const args = parseOptions(acceptedOptions, options, versionInfo);
1574
+ args.push(...files, outputFile);
1637
1575
 
1638
- const { stdout } = await execFileAsync(
1639
- pathResolve(this.#popplerPath, "pdfunite"),
1640
- args
1641
- );
1642
- return Promise.resolve(stdout);
1643
- } catch (err) {
1644
- return Promise.reject(err);
1645
- }
1576
+ const { stdout } = await execFileAsync(
1577
+ pathResolve(this.#popplerPath, "pdfunite"),
1578
+ args
1579
+ );
1580
+ return stdout;
1646
1581
  }
1647
1582
  }
1648
1583