-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
1109 lines (1019 loc) · 31.3 KB
/
index.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const { spawn, } = require('child_process');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
/**
* PdfTk Class
* @class
*/
class PdfTk {
/**
* PdfTk constructor.
* @param {Array} src - Input source file(s).
* @param {Array} [tmpFiles] - Array of temp files that have been created while initializing the constructor.
* @returns {Object} PdfTk class instance.
*/
constructor(src) {
try {
/**
* Promise library to use
* @member
* @type {Object}
*/
this._Promise = this._Promise || Promise;
/**
* PdfTk binary path
* @member
* @type {String}
*/
this._bin = this._bin || process.env.PDFTK_PATH || 'pdftk';
/**
* Allows the plugin to ignore the PDFTK warnings. Useful with huge PDF files.
* @member
* @type {Boolean}
*/
this._ignoreWarnings = this._ignoreWarnings || false;
/**
* Allows the plugin to change where the temp file is written.
* @member
* @type {String}
*/
this._tempDir = this._tempDir || path.join(__dirname, './node-pdftk-tmp/');
const input = [];
/**
* @member
* @type {Array}
*/
this.tmpFiles = [];
/**
* Write a temp file and save the path for deletion later.
* @private
* @function
* @param {Object} srcFile - Buffer to be written as a temp file.
* @returns {String} Path of the newly created temp file.
*/
const writeTempFile = srcFile => {
const tmpPath = this._tempDir;
const uniqueId = crypto.randomBytes(16).toString('hex');
const tmpFile = path.normalize(`${tmpPath}/${uniqueId}.pdf`);
fs.writeFileSync(tmpFile, srcFile);
this.tmpFiles.push(tmpFile);
return tmpFile;
};
/**
* Make sure input src is an array
*/
src = Array.isArray(src) ? src : [
src,
];
/**
* Loop through src and push to input array
*/
for (const srcFile of src) {
if (Buffer.isBuffer(srcFile)) {
input.push(writeTempFile(srcFile));
} else if (PdfTk.isObject(srcFile)) {
for (const handle in srcFile) {
/* istanbul ignore else */
if (srcFile.hasOwnProperty(handle)) {
if (Buffer.isBuffer(srcFile[handle])) {
input.push(`${handle}=${writeTempFile(srcFile[handle])}`);
} else {
input.push(`${handle}=${srcFile[handle]}`);
}
}
}
} else {
input.push(srcFile);
}
}
/**
* @member
* @type {Array}
*/
this.src = input;
/**
* @member
* @type {Array}
*/
this.args = [].concat(this.src);
/**
* @member
* @type {Array}
*/
this.postArgs = [];
} catch (err) {
this.error = err;
}
return this;
}
/**
* Simple object check. Arrays not included.
* @static
* @public
* @param item - Item to check.
* @returns {Boolean} Is object.
*/
static isObject(item) {
return typeof item === 'object' && !Array.isArray(item) && item !== null;
}
/**
* Simple string check.
* @static
* @public
* @param item - Item to check.
* @returns {Boolean} Is string.
*/
static isString(item) {
return typeof item === 'string' || item instanceof String;
}
/**
* Returns a buffer from a file.
* @static
* @public
* @param {(String|Buffer)} file - File to buffer.
* @returns {Object} Buffered file.
*/
static fileToBuffer(file) {
file = PdfTk.isString(file) ? fs.readFileSync(file) : file;
return file;
}
/**
* Returns a buffer from a string.
* @static
* @public
* @param {(String|Buffer)} str - String to buffer.
* @returns {Object} Buffered string.
*/
static stringToBuffer(str, encoding = 'utf8') {
if (Buffer.from) {
return Buffer.from(str, encoding);
}
return new Buffer(str, encoding);
}
/**
* Sanitizes fdf input
* @statuc
* @public
* @param {String} str - String to sanitize
* @returns {String} Sanitized string
*/
static sanitizeForFdf(str) {
return str
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)');
}
/**
* Creates fdf file from JSON input.
* Converts input values to binary buffer, which seems to allow PdfTk to render utf-8 characters.
* @static
* @public
* @param {Object} data - JSON data to transform to fdf.
* @returns {Buffer} Fdf data as a buffer.
*/
static generateFdfFromJSON(data) {
const header = PdfTk.stringToBuffer(`
%FDF-1.2\n
${String.fromCharCode(226) + String.fromCharCode(227) + String.fromCharCode(207) + String.fromCharCode(211)}\n
1 0 obj\n
<<\n
/FDF\n
<<\n
/Fields [\n
`);
let body = PdfTk.stringToBuffer('');
for (const prop in data) {
/* istanbul ignore else */
if (data.hasOwnProperty(prop) && data[prop] !== null && data[prop] !== undefined) {
body = Buffer.concat([
body,
PdfTk.stringToBuffer('<<\n/T ('),
]);
body = Buffer.concat([
body,
PdfTk.stringToBuffer(PdfTk.sanitizeForFdf(prop.toString()), 'binary'),
]);
body = Buffer.concat([
body,
PdfTk.stringToBuffer(')\n/V('),
]);
body = Buffer.concat([
body,
PdfTk.stringToBuffer(PdfTk.sanitizeForFdf(data[prop].toString()), 'binary'),
]);
body = Buffer.concat([
body,
PdfTk.stringToBuffer(')\n>>\n'),
]);
}
}
const footer = PdfTk.stringToBuffer(`
]\n
>>\n
>>\n
endobj \n
trailer\n
\n
<<\n
/Root 1 0 R\n
>>\n
%%EOF\n
`);
return Buffer.concat([
header,
body,
footer,
]);
}
/**
* Creates pdf info text file from JSON input.
* @static
* @public
* @param {Object} data - JSON data to transform to info file.
* @returns {Buffer} Info text file as a buffer.
*/
static generateInfoFromJSON(data) {
const info = [];
for (const prop in data) {
/* istanbul ignore else */
if (data.hasOwnProperty(prop)) {
const begin = PdfTk.stringToBuffer('InfoBegin\nInfoKey: ');
const key = PdfTk.stringToBuffer(prop.toString());
const newline = PdfTk.stringToBuffer('\nInfoValue: ');
const value = PdfTk.stringToBuffer(data[prop].toString());
const newline2 = PdfTk.stringToBuffer('\n');
info.push(begin, key, newline, value, newline2);
}
}
return Buffer.concat(info);
}
/**
* Creates an input command that uses the stdin.
* @private
* @param {String} command - Command to create.
* @param {(String|Buffer)} file - Stdin file.
*/
_commandWithStdin(command, file) {
this.stdin = PdfTk.fileToBuffer(file);
this.args.push(
command,
'-'
);
}
/**
* Clean up temp files, if created.
* @private
*/
_cleanUpTempFiles() {
while (this.tmpFiles.length > 0) {
try {
fs.unlinkSync(this.tmpFiles.pop());
} catch (err) {
/* keep going */
}
}
}
/**
* Run the command.
* @public
* @param {String} writeFile - Path to the output file to write from stdout. If used with the "outputDest" parameter, two files will be written.
* @param {String} [outputDest] - The output file to write without stdout. When present, the returning promise will not contain the output buffer. If used with the "writeFile" parameter, two files will be written.
* @param {Boolean} [needsOutput=true] - Optional boolean used to exclude the 'output' argument (only used for specific methods).
* @returns {Promise} Promise that resolves the output buffer, if "outputDest" is not given.
*/
output(writeFile, outputDest, needsOutput = true) {
return new this._Promise((resolve, reject) => {
if (this.error) {
this._cleanUpTempFiles();
return reject(this.error);
}
try {
if (needsOutput) {
this.args.push(
'output',
outputDest || '-'
);
}
this.args = this.args.concat(this.postArgs);
const child = spawn(this._bin, this.args);
const result = [];
child.stderr.on('data', data => {
if (!this._ignoreWarnings && data.toString().toLowerCase().includes('error')) {
this._cleanUpTempFiles();
return reject(data.toString('utf8'));
}
});
child.on('error', e => {
this._cleanUpTempFiles();
if (e.code === 'ENOENT') {
return reject(new Error(`
pdftk was called but is not installed on your system.
Install it here: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
`));
}
return reject(e);
});
child.stdout.on('data', data => result.push(PdfTk.stringToBuffer(data)));
child.on('close', code => {
this._cleanUpTempFiles();
if (code === 0) {
const output = Buffer.concat(result);
if (writeFile) {
return fs.writeFile(writeFile, output, err => {
if (err) return reject(err);
return resolve(output);
});
}
return resolve(output);
}
return reject(code);
});
if (this.stdin) {
child.stdin.write(this.stdin);
child.stdin.end();
}
} catch (err) {
this._cleanUpTempFiles();
return reject(err);
}
});
}
/**
* Assembles ("concatenate") pages from input PDFs to create a new PDF.
* @public
* @chainable
* @param {(String|Array)} [catCommand] - Page ranges for cat method.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-cat}
*/
cat(catCommand) {
if (this.error) return this;
try {
this.args.push('cat');
if (catCommand) {
catCommand = Array.isArray(catCommand) ? catCommand : catCommand.split(' ');
for (const cmd of catCommand) {
this.args.push(cmd);
}
}
} catch (err) {
this.error = err;
}
return this;
}
/**
* Collates pages from input PDF to create new PDF.
* @public
* @chainable
* @param {(String|Array)} [shuffleCommand] - Page ranges for shuffle method.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-shuffle}
*/
shuffle(shuffleCommand) {
if (this.error) return this;
try {
this.args.push('shuffle');
if (shuffleCommand) {
shuffleCommand = Array.isArray(shuffleCommand) ? shuffleCommand : shuffleCommand.split(' ');
for (const cmd of shuffleCommand) {
this.args.push(cmd);
}
}
} catch (err) {
this.error = err;
}
return this;
}
/**
* Splits a single PDF into individual pages.
* @public
* @param {String} [outputOptions] - Burst output options for naming conventions.
* @returns {Promise}
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-burst}
*/
burst(outputOptions) {
if (this.error) return this;
const hasOutput = !!outputOptions;
try {
this.args.push('burst');
} catch (err) {
this.error = err;
}
return this.output(null, (outputOptions || null), hasOutput);
}
/**
* Takes a single input PDF and rotates just the specified pages.
* @public
* @chainable
* @param {(String|Array)} rotateCommand - Page ranges for rotate command.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-rotate}
*/
rotate(rotateCommand) {
if (this.error) return this;
try {
this.args.push('rotate');
if (rotateCommand) {
rotateCommand = Array.isArray(rotateCommand) ? rotateCommand : rotateCommand.split(' ');
for (const cmd of rotateCommand) {
this.args.push(cmd);
}
}
} catch (err) {
this.error = err;
}
return this;
}
/**
* Generate fdf file from input PDF.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-generate-fdf}
*/
generateFdf() {
if (this.error) return this;
try {
this.args.push('generate_fdf');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Fill a PDF form from JSON data.
* @public
* @chainable
* @param {Object} data - Form fill data.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-fill-form}
*/
fillForm(data) {
if (this.error) return this;
try {
data = PdfTk.isString(data) ? data : PdfTk.generateFdfFromJSON(data);
this._commandWithStdin('fill_form', data);
} catch (err) {
this.error = err;
}
return this;
}
/**
* Applies a PDF watermark to the background of a single PDF.
* @public
* @chainable
* @param {(String|Buffer)} file - PDF file that contains the background to be applied.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-background}
*/
background(file) {
if (this.error) return this;
try {
this._commandWithStdin('background', file);
} catch (err) {
this.error = err;
}
return this;
}
/**
* Same as the background operation, but applies each page of the background PDF to the corresponding page of the input PDF.
* @public
* @chainable
* @param {(String|Buffer)} file - PDF file that contains the background to be applied.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-multibackground}
*/
multiBackground(file) {
if (this.error) return this;
try {
this._commandWithStdin('multibackground', file);
} catch (err) {
this.error = err;
}
return this;
}
/**
* This behaves just like the background operation except it overlays the stamp PDF page on top of the input PDF document’s pages.
* @public
* @chainable
* @param {(String|Buffer)} file - PDF file that contains the content to be stamped.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-stamp}
*/
stamp(file) {
if (this.error) return this;
try {
this._commandWithStdin('stamp', file);
} catch (err) {
this.error = err;
}
return this;
}
/**
* Same as the stamp operation, but applies each page of the stamp PDF to the corresponding page of the input PDF.
* @public
* @chainable
* @param {(String|Buffer)} file - PDF file that contains the content to be stamped.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-multistamp}
*/
multiStamp(file) {
if (this.error) return this;
try {
this._commandWithStdin('multistamp', file);
} catch (err) {
this.error = err;
}
return this;
}
/**
* Outputs PDF bookmarks and metadata.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-dump-data}
*/
dumpData() {
if (this.error) return this;
try {
this.args.push('dump_data');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Outputs PDF bookmarks and metadata with utf-8 encoding.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-dump-data-utf8}
*/
dumpDataUtf8() {
if (this.error) return this;
try {
this.args.push('dump_data_utf8');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Outputs form field statistics.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-dump-data-fields}
*/
dumpDataFields() {
if (this.error) return this;
try {
this.args.push('dump_data_fields');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Outputs form field statistics with utf-8 encoding.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-dump-data-fields-utf8}
*/
dumpDataFieldsUtf8() {
if (this.error) return this;
try {
this.args.push('dump_data_fields_utf8');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Outputs PDF annotation information.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-dump-data-annots}
*/
dumpDataAnnots() {
if (this.error) return this;
try {
this.args.push('dump_data_annots');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Update the bookmarks and metadata of a PDF with utf-8 encoding.
* @public
* @chainable
* @param {Object} data - Update data.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-update-info}
*/
updateInfo(data) {
if (this.error) return this;
try {
data = PdfTk.isString(data) ? data : PdfTk.generateInfoFromJSON(data);
this._commandWithStdin('update_info', data);
} catch (err) {
this.error = err;
}
return this;
}
/**
* Update the bookmarks and metadata of a PDF.
* @public
* @chainable
* @param {Object} data - Update data.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-update-info-utf8}
*/
updateInfoUtf8(data) {
if (this.error) return this;
try {
data = PdfTk.isString(data) ? data : PdfTk.generateInfoFromJSON(data);
this._commandWithStdin('update_info_utf8', data);
} catch (err) {
this.error = err;
}
return this;
}
/**
* Attach files to PDF.
* @public
* @chainable
* @param {(String|String[])} files - Files to attach.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-attach} for more information.
*/
attachFiles(files) {
if (this.error) return this;
try {
files = Array.isArray(files) ? files : [
files,
];
this.args.push('attach_files');
for (const file of files) {
this.args.push(file);
}
} catch (err) {
this.error = err;
}
return this;
}
/**
* Unpack files into an output directory. This method is not chainable, and hereby does not require
* the output method afterwards.
* @public
* @param {String} outputDir - Output directory for files.
* @returns {Promise}
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-unpack} for more information.
*/
unpackFiles(outputDir) {
if (this.error) return this;
try {
this.args.push('unpack_files');
} catch (err) {
this.error = err;
}
return this.output(null, outputDir);
}
/**
* Used with the {@link attachFiles} method to attach to a specific page.
* @public
* @chainable
* @param {Number} pageNo - Page number in which to attach.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-op-attach}
*/
toPage(pageNo) {
if (this.error) return this;
try {
this.args.push(
'to_page',
pageNo
);
} catch (err) {
this.error = err;
}
return this;
}
/**
* Merge PDF form fields and their data.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-output-flatten}
*/
flatten() {
if (this.error) return this;
try {
this.postArgs.push('flatten');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Set Adobe Reader to generate new field appearances.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-output-need-appearances}
*/
needAppearances() {
if (this.error) return this;
try {
this.postArgs.push('need_appearances');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Restore page sream compression.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-compress}
*/
compress() {
if (this.error) return this;
try {
this.postArgs.push('compress');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Remove page stream compression.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-compress}
*/
uncompress() {
if (this.error) return this;
try {
this.postArgs.push('uncompress');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Keep first ID when combining files.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-keep-id}
*/
keepFirstId() {
if (this.error) return this;
try {
this.postArgs.push('keep_first_id');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Keep final ID when combining pages.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-keep-id}
*/
keepFinalId() {
if (this.error) return this;
try {
this.postArgs.push('keep_final_id');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Drop all XFA data.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-drop-xfa}
*/
dropXfa() {
if (this.error) return this;
try {
this.postArgs.push('drop_xfa');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Set the verbose option.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-verbose}
*/
verbose() {
if (this.error) return this;
try {
this.postArgs.push('verbose');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Never prompt when errors occur.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-ask}
*/
dontAsk() {
if (this.error) return this;
try {
this.postArgs.push('dont_ask');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Always prompt when errors occur.
* @public
* @chainable
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-ask}
*/
doAsk() {
if (this.error) return this;
try {
this.postArgs.push('do_ask');
} catch (err) {
this.error = err;
}
return this;
}
/**
* Set the input password.
* @public
* @chainable
* @param {String} password - Password to set.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-input-pw}
*/
inputPw(password) {
if (this.error) return this;
try {
this.args.push(
'input_pw',
password
);
} catch (err) {
this.error = err;
}
return this;
}
/**
* Set the user password.
* @public
* @chainable
* @param {String} password - Password to set.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-output-enc-user-pw}
*/
userPw(password) {
if (this.error) return this;
try {
this.postArgs.push(
'user_pw',
password
);
} catch (err) {
this.error = err;
}
return this;
}
/**
* Set the owner password.
* @public
* @chainable
* @param {String} password - Password to set.
* @returns {Object} PdfTk class instance.
* @see {@link https://www.pdflabs.com/docs/pdftk-man-page/#dest-output-enc-owner-pw}
*/
ownerPw(password) {
if (this.error) return this;
try {