summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-05 19:52:51 -0700
committerSamy Pessé <samypesse@gmail.com>2014-04-05 19:52:51 -0700
commit761cfb4a2c68cfcfa5557dd41ca912cb716df05b (patch)
tree3372f793cef521edf4b2ddc36b8f8464db090c6e /lib
parent8e3339ead200da978172aa70e1f8e114a4126c0f (diff)
downloadgitbook-761cfb4a2c68cfcfa5557dd41ca912cb716df05b.zip
gitbook-761cfb4a2c68cfcfa5557dd41ca912cb716df05b.tar.gz
gitbook-761cfb4a2c68cfcfa5557dd41ca912cb716df05b.tar.bz2
Add pdf generator using gitbook-pdf
Diffstat (limited to 'lib')
-rw-r--r--lib/generate/index.js1
-rw-r--r--lib/generate/pdf/Index.js56
2 files changed, 57 insertions, 0 deletions
diff --git a/lib/generate/index.js b/lib/generate/index.js
index 4ed9f08..38e7503 100644
--- a/lib/generate/index.js
+++ b/lib/generate/index.js
@@ -9,6 +9,7 @@ var parse = require("../parse");
var generators = {
"site": require("./site"),
"page": require("./page"),
+ "pdf": require("./pdf"),
"json": require("./json")
};
diff --git a/lib/generate/pdf/Index.js b/lib/generate/pdf/Index.js
new file mode 100644
index 0000000..680bda3
--- /dev/null
+++ b/lib/generate/pdf/Index.js
@@ -0,0 +1,56 @@
+var util = require("util");
+var path = require("path");
+var Q = require("q");
+var _ = require("lodash");
+var exec = require('child_process').exec;
+
+var fs = require("../fs");
+var parse = require("../../parse");
+var BaseGenerator = require("../page");
+
+/*
+ * This generator inherits from the single page generator
+ * and convert the page output to pdf using gitbook-pdf
+ */
+var Generator = function() {
+ BaseGenerator.apply(this, arguments);
+
+ // Options for PDF generation
+ this.options = _.defaults(this.options, {
+ format: "A4"
+ });
+};
+util.inherits(Generator, BaseGenerator);
+
+Generator.prototype.finish = function() {
+ var that = this;
+
+ return BaseGenerator.prototype.finish.apply(this)
+ .then(function() {
+ var d = Q.defer();
+
+ var command = [
+ "gitbook-pdf",
+ "generate",
+ path.join(that.options.output, "index.html"),
+ path.join(that.options.output, "index.pdf"),
+ "--format="+that.options.format
+ ].join(" ");
+
+ exec(command, function (error, stdout, stderr) {
+ if (error) {
+ if (error.code == 127) {
+ error.message = "Need to install gitbook-pdf using: npm install gitbook-pdf -g";
+ } else {
+ error.message = error.message + " "+stdout;
+ }
+ return d.reject(error);
+ }
+ d.resolve();
+ });
+
+ return d.promise;
+ });
+};
+
+module.exports = Generator; \ No newline at end of file