2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Definition of the HtmlPdfCLIGenerator class.
|
2016-02-02 18:38:12 +00:00
|
|
|
@module generators/html-pdf-generator.js
|
2016-01-27 10:29:26 +00:00
|
|
|
@license MIT. See LICENSE.md for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
2016-02-13 08:27:11 +00:00
|
|
|
var FS, HMSTATUS, HtmlPdfCLIGenerator, PATH, SLASH, SPAWN, TemplateGenerator, _, engines,
|
2016-02-02 18:38:12 +00:00
|
|
|
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
|
|
|
hasProp = {}.hasOwnProperty;
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
TemplateGenerator = require('./template-generator');
|
|
|
|
|
|
|
|
FS = require('fs-extra');
|
|
|
|
|
|
|
|
PATH = require('path');
|
|
|
|
|
|
|
|
SLASH = require('slash');
|
|
|
|
|
2016-02-01 14:25:22 +00:00
|
|
|
_ = require('underscore');
|
|
|
|
|
|
|
|
HMSTATUS = require('../core/status-codes');
|
|
|
|
|
2016-02-13 08:27:11 +00:00
|
|
|
SPAWN = require('../utils/safe-spawn');
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
An HTML-driven PDF resume generator for HackMyResume. Talks to Phantom,
|
|
|
|
wkhtmltopdf, and other PDF engines over a CLI (command-line interface).
|
|
|
|
If an engine isn't installed for a particular platform, error out gracefully.
|
|
|
|
*/
|
|
|
|
|
2016-02-02 18:38:12 +00:00
|
|
|
module.exports = HtmlPdfCLIGenerator = (function(superClass) {
|
|
|
|
extend(HtmlPdfCLIGenerator, superClass);
|
|
|
|
|
|
|
|
function HtmlPdfCLIGenerator() {
|
|
|
|
HtmlPdfCLIGenerator.__super__.constructor.call(this, 'pdf', 'html');
|
|
|
|
}
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/** Generate the binary PDF. */
|
2016-02-02 18:38:12 +00:00
|
|
|
|
|
|
|
HtmlPdfCLIGenerator.prototype.onBeforeSave = function(info) {
|
2016-02-01 14:25:22 +00:00
|
|
|
var safe_eng;
|
2016-02-13 21:08:45 +00:00
|
|
|
if (info.ext !== 'html' && info.ext !== 'pdf') {
|
2016-02-13 08:27:11 +00:00
|
|
|
return info.mk;
|
|
|
|
}
|
2016-02-01 14:25:22 +00:00
|
|
|
safe_eng = info.opts.pdf || 'wkhtmltopdf';
|
|
|
|
if (safe_eng === 'phantom') {
|
|
|
|
safe_eng = 'phantomjs';
|
|
|
|
}
|
|
|
|
if (_.has(engines, safe_eng)) {
|
|
|
|
this.errHandler = info.opts.errHandler;
|
2017-01-07 00:05:46 +00:00
|
|
|
engines[safe_eng].call(this, info.mk, info.outputFile, info.opts, this.onError);
|
2016-02-01 14:25:22 +00:00
|
|
|
return null;
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
2016-02-02 18:38:12 +00:00
|
|
|
};
|
|
|
|
|
2016-02-02 02:14:36 +00:00
|
|
|
|
|
|
|
/* Low-level error callback for spawn(). May be called after HMR process
|
|
|
|
termination, so object references may not be valid here. That's okay; if
|
|
|
|
the references are invalid, the error was already logged. We could use
|
|
|
|
spawn-watch here but that causes issues on legacy Node.js.
|
|
|
|
*/
|
2016-02-02 18:38:12 +00:00
|
|
|
|
|
|
|
HtmlPdfCLIGenerator.prototype.onError = function(ex, param) {
|
2016-02-02 02:14:36 +00:00
|
|
|
var ref;
|
|
|
|
if ((ref = param.errHandler) != null) {
|
|
|
|
if (typeof ref.err === "function") {
|
|
|
|
ref.err(HMSTATUS.pdfGeneration, ex);
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 18:38:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return HtmlPdfCLIGenerator;
|
|
|
|
|
|
|
|
})(TemplateGenerator);
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
engines = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
Generate a PDF from HTML using wkhtmltopdf's CLI interface.
|
|
|
|
Spawns a child process with `wkhtmltopdf <source> <target>`. wkhtmltopdf
|
|
|
|
must be installed and path-accessible.
|
|
|
|
TODO: If HTML generation has run, reuse that output
|
|
|
|
TODO: Local web server to ease wkhtmltopdf rendering
|
|
|
|
*/
|
2017-01-07 00:05:46 +00:00
|
|
|
wkhtmltopdf: function(markup, fOut, opts, on_error) {
|
2018-01-30 17:40:17 +00:00
|
|
|
var tempFile, wkargs, wkopts;
|
2016-01-27 10:29:26 +00:00
|
|
|
tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
|
|
|
|
FS.writeFileSync(tempFile, markup, 'utf8');
|
2018-01-30 17:40:17 +00:00
|
|
|
wkopts = _.extend({
|
|
|
|
'margin-top': '10mm',
|
|
|
|
'margin-bottom': '10mm'
|
2017-01-07 00:05:46 +00:00
|
|
|
}, opts.wkhtmltopdf);
|
2018-01-30 17:40:17 +00:00
|
|
|
wkopts = _.flatten(_.map(wkopts, function(v, k) {
|
2017-01-07 00:05:46 +00:00
|
|
|
return ['--' + k, v];
|
|
|
|
}));
|
2018-01-30 17:40:17 +00:00
|
|
|
wkargs = wkopts.concat([tempFile, fOut]);
|
|
|
|
SPAWN('wkhtmltopdf', wkargs, false, on_error, this);
|
2016-01-27 10:29:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Generate a PDF from HTML using Phantom's CLI interface.
|
|
|
|
Spawns a child process with `phantomjs <script> <source> <target>`. Phantom
|
|
|
|
must be installed and path-accessible.
|
|
|
|
TODO: If HTML generation has run, reuse that output
|
|
|
|
TODO: Local web server to ease Phantom rendering
|
|
|
|
*/
|
2017-01-07 00:05:46 +00:00
|
|
|
phantomjs: function(markup, fOut, opts, on_error) {
|
2016-02-01 14:25:22 +00:00
|
|
|
var destPath, scriptPath, sourcePath, tempFile;
|
2016-01-27 10:29:26 +00:00
|
|
|
tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
|
|
|
|
FS.writeFileSync(tempFile, markup, 'utf8');
|
2016-02-13 08:27:11 +00:00
|
|
|
scriptPath = PATH.relative(process.cwd(), PATH.resolve(__dirname, '../utils/rasterize.js'));
|
|
|
|
scriptPath = SLASH(scriptPath);
|
2016-01-27 10:29:26 +00:00
|
|
|
sourcePath = SLASH(PATH.relative(process.cwd(), tempFile));
|
|
|
|
destPath = SLASH(PATH.relative(process.cwd(), fOut));
|
2016-02-13 08:27:11 +00:00
|
|
|
SPAWN('phantomjs', [scriptPath, sourcePath, destPath], false, on_error, this);
|
2017-01-16 05:15:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Generate a PDF from HTML using WeasyPrint's CLI interface.
|
|
|
|
Spawns a child process with `weasyprint <source> <target>`. Weasy Print
|
|
|
|
must be installed and path-accessible.
|
|
|
|
TODO: If HTML generation has run, reuse that output
|
|
|
|
*/
|
|
|
|
weasyprint: function(markup, fOut, opts, on_error) {
|
|
|
|
var tempFile;
|
|
|
|
tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
|
|
|
|
FS.writeFileSync(tempFile, markup, 'utf8');
|
|
|
|
SPAWN('weasyprint', [tempFile, fOut], false, on_error, this);
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}).call(this);
|
2016-02-02 02:14:36 +00:00
|
|
|
|
|
|
|
//# sourceMappingURL=html-pdf-cli-generator.js.map
|