index.js
/**
     * 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.
     */
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.
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('warning'))) {
                        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);
            }
        });
    }
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 ( 'warning' ) ) ) { this . _cleanUpTempFiles ( ) ; return reject ( data . toString ( 'utf8' ) ) ; } } ) ; child . on ( 'error' , e => { this . _cleanUpTempFiles ( ) ; if ( e . code === 'ENOENT' ) { return reject ( new Error ( ` ` ) ) ; } 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 ) ; } } ) ; }
Run the command.
Run the command .