all files / html-boilerplate-pdf/lib/ htmlboilerplate-pdf.js

94.12% Statements 48/51
81.25% Branches 26/32
100% Functions 7/7
100% Lines 47/47
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     11× 11× 11×   11×   11× 11× 11×     11×   11× 11× 11×     11× 11× 11× 11× 11×   11× 11×     11×     11× 11× 11×     11× 11× 11×   11×   11×                                       11× 11×       11×              
var fs = require("fs");
var path = require("path");
var through = require("through");
var extend = require("extend");
var pygments = require('pygmentize-bundled');
var tmp = require("tmp");
var childProcess = require("child_process");
var duplexer = require("duplexer");
var streamft = require("stream-from-to");
 
tmp.setGracefulCleanup();
 
function htmlBoilerplatePDF (opts) {
  opts = opts || {};
  opts.phantomPath = opts.phantomPath || require("phantomjs").path;
  opts.runningsPath = path.resolve(__dirname + "/..", opts.runningsPath || '') || __dirname + "/runnings.js";
 
  opts.cssPath = opts.cssPath || __dirname + "/../pdf.css";
 
  var relativeCssPath = path.resolve(process.cwd(), opts.cssPath);
  Eif (fs.existsSync(relativeCssPath)) {
    opts.cssPath = relativeCssPath;
  }
 
  opts.highlightCssPath = opts.highlightCssPath || __dirname + "/../highlight.css";
 
  var relativeHighlightCssPath = path.resolve(process.cwd(), opts.highlightCssPath);
  Eif (fs.existsSync(relativeHighlightCssPath)) {
    opts.highlightCssPath = relativeHighlightCssPath;
  }
 
  opts.paperFormat = opts.paperFormat || "A4";
  opts.paperOrientation = opts.paperOrientation || "portrait";
  opts.paperBorder = opts.paperBorder || "1cm";
  opts.renderDelay = opts.renderDelay || 500;
  opts.preProcessHtml = opts.preProcessHtml || function () { return through(); };
 
  var inputStream = through();
  var outputStream = through();
 
  // Stop input stream emitting data events until we're ready to read them
  inputStream.pause();
 
  // Create tmp file to save HTML for phantom to process
  tmp.file({postfix: ".html"}, function (er, tmpHtmlPath, tmpHtmlFd) {
    Iif (er) return outputStream.emit("error", er);
    fs.close(tmpHtmlFd);
 
    // Create tmp file to save PDF to
    tmp.file({postfix: ".pdf"}, function (er, tmpPdfPath, tmpPdfFd) {
      Iif (er) return outputStream.emit("error", er);
      fs.close(tmpPdfFd);
 
      var htmlToTmpHtmlFile = fs.createWriteStream(tmpHtmlPath);
 
      htmlToTmpHtmlFile.on("finish", function () {
        // Invoke phantom to generate the PDF
        var childArgs = [
          path.join(__dirname, "..", "lib-phantom", "pdf.js"),
          tmpHtmlPath,
          tmpPdfPath,
          opts.runningsPath,
          opts.cssPath,
          opts.highlightCssPath,
          opts.paperFormat,
          opts.paperOrientation,
          opts.paperBorder,
          opts.renderDelay,
        ];
 
        childProcess.execFile(opts.phantomPath, childArgs, function(er, stdout, stderr) {
          //if (stdout) console.log(stdout)
          //if (stderr) console.error(stderr)
          Iif (er) return outputStream.emit("error", er);
          fs.createReadStream(tmpPdfPath).pipe(outputStream);
        });
      });
 
      // Setup the pipeline
      inputStream.pipe(opts.preProcessHtml()).pipe(htmlToTmpHtmlFile);
      inputStream.resume();
    });
  });
 
  return extend(
    duplexer(inputStream, outputStream),
    streamft(function () {
      return htmlBoilerplatePDF(opts);
    })
  );
}
 
module.exports = htmlBoilerplatePDF;