Code coverage report for wkhtmltopdf-nodejs-pdfapi/index.js

Statements: 96.77% (30 / 31)      Branches: 91.67% (11 / 12)      Functions: 88.89% (8 / 9)      Lines: 96.77% (30 / 31)      Ignored: 5 statements, 2 functions, 2 branches     

All files » wkhtmltopdf-nodejs-pdfapi/ » 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 671       1 1 1     1   4 20           4     4   4 4   4   4 4 5 4   5 2   5 1       4 4   4 2               1 1 1 1   1             1  
var spawn = require('child_process').spawn,
    Promise = require('promise'),
    fs = require('fs');
 
var PdfApi = function(displayServer, displayServerParameterList) {
    this.displayServer = displayServer;
    this.displayServerParameterList = displayServerParameterList || [];
};
 
PdfApi.prototype = {
    createPdf: function(request, outputFilePath) {
        var pdfCommandParts = request.toString().split(' ').filter(function(value) {
                return value !== '';
            }),
            commandParts = ['wkhtmltopdf'].concat(pdfCommandParts),
            displayServerCommandParts = [],
            debug = [];
 
        Iif (this.displayServer) {
            displayServerCommandParts.push(this.displayServer);
        }
        displayServerCommandParts = displayServerCommandParts.concat(this.displayServerParameterList);
 
        commandParts = displayServerCommandParts.concat(commandParts);
        commandParts.push(outputFilePath);
 
        var command = spawn(commandParts[0], commandParts.slice(1));
 
        return new Promise(function(resolve, reject) {
            var onDataOutput = function(data) {
                if (data) {
                    debug.push(data.toString());
                }
                if (data.toString().match(/^Exit with code.*due to.*error/)) {
                    reject(data, debug);
                }
                if (data.toString().trim() === 'Done') {
                    resolve(data, debug);
                }
            };
 
            command.stdout.on('data', onDataOutput);
            command.stderr.on('data', onDataOutput);
 
            command.on('close', function () {
                reject('Pdf has not been created. See debug output for more details', debug);
            });
        }.bind(this));
    },
 
 
    deletePdf: function(filePath) {
        /* istanbul ignore next */
        return new Promise(function(resolve, reject) {
            fs.unlink(filePath, function(error) {
                if (error) {
                    reject(error);
                } else {
                    resolve();
                }
            }.bind(this));
        });
    }
};
 
module.exports = PdfApi;