mirror of
https://github.com/JuanCanham/HackMyResume.git
synced 2024-11-22 16:30:11 +00:00
Support CLI-based PDF generation.
Support Phantom and wkhtmltopdf generation via CLI.
This commit is contained in:
parent
96b9bb68e3
commit
97ebecd84a
@ -9,7 +9,7 @@
|
||||
{ name: 'html', ext: 'html', gen: new (require('../gen/html-generator'))() },
|
||||
{ name: 'txt', ext: 'txt', gen: new (require('../gen/text-generator'))() },
|
||||
{ name: 'doc', ext: 'doc', fmt: 'xml', gen: new (require('../gen/word-generator'))() },
|
||||
{ name: 'pdf', ext: 'pdf', fmt: 'html', is: false, gen: new (require('../gen/html-pdf-generator'))() },
|
||||
{ name: 'pdf', ext: 'pdf', fmt: 'html', is: false, gen: new (require('../gen/html-pdf-cli-generator'))() },
|
||||
{ name: 'png', ext: 'png', fmt: 'html', is: false, gen: new (require('../gen/html-png-generator'))() },
|
||||
{ name: 'md', ext: 'md', fmt: 'txt', gen: new (require('../gen/markdown-generator'))() },
|
||||
{ name: 'json', ext: 'json', gen: new (require('../gen/json-generator'))() },
|
||||
|
@ -120,7 +120,7 @@ Error-handling routines for HackMyResume.
|
||||
chalk.yellow(' to create.');
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.wkhtmltopdf:
|
||||
case HACKMYSTATUS.pdfGeneration:
|
||||
msg = chalk.red.bold('ERROR: PDF generation failed. ') + chalk.red('Make sure wkhtmltopdf is ' +
|
||||
'installed and accessible from your path.');
|
||||
if( ex.inner ) msg += chalk.red('\n' + ex.inner);
|
||||
|
@ -16,7 +16,7 @@ Status codes for HackMyResume.
|
||||
resumeNotFoundAlt: 6,
|
||||
inputOutputParity: 7,
|
||||
createNameMissing: 8,
|
||||
wkhtmltopdf: 9,
|
||||
pdfgeneration: 9,
|
||||
missingPackageJSON: 10,
|
||||
invalid: 11,
|
||||
invalidTarget: 12
|
||||
|
113
src/gen/html-pdf-cli-generator.js
Normal file
113
src/gen/html-pdf-cli-generator.js
Normal file
@ -0,0 +1,113 @@
|
||||
/**
|
||||
Definition of the HtmlPdfCLIGenerator class.
|
||||
@module html-pdf-generator.js
|
||||
@license MIT. See LICENSE.md for details.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
(function() {
|
||||
|
||||
|
||||
|
||||
var TemplateGenerator = require('./template-generator')
|
||||
, FS = require('fs-extra')
|
||||
, HTML = require( 'html' )
|
||||
, PATH = require('path')
|
||||
, SLASH = require('slash');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
An HTML-driven PDF resume generator for HackMyResume. Talks to Phantom,
|
||||
wkhtmltopdf, and other PDF libraries over a CLI.
|
||||
*/
|
||||
var HtmlPdfCLIGenerator = module.exports = TemplateGenerator.extend({
|
||||
|
||||
|
||||
|
||||
init: function() {
|
||||
this._super( 'pdf', 'html' );
|
||||
},
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Generate the binary PDF.
|
||||
*/
|
||||
onBeforeSave: function( info ) {
|
||||
try {
|
||||
engines[ info.opts.pdf || 'wkhtmltopdf' ]
|
||||
.call( this, info.mk, info.outputFile );
|
||||
return null; // halt further processing
|
||||
}
|
||||
catch(ex) {
|
||||
// { [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', ... }
|
||||
// { [Error: ENOENT] }
|
||||
throw { fluenterror: this.codes.pdfGeneration, inner: ex };
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
var engines = {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Generate a PDF from HTML using wkhtmltopdf.
|
||||
*/
|
||||
wkhtmltopdf: function(markup, fOut) {
|
||||
|
||||
// Save the markup to a temporary file
|
||||
var tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
|
||||
FS.writeFileSync( tempFile, markup, 'utf8' );
|
||||
|
||||
var spawn = require('child_process').spawn;
|
||||
var child = spawn('wkhtmltopdf', [
|
||||
tempFile, fOut
|
||||
]);
|
||||
|
||||
// child.stdout.on('data', function(chunk) {
|
||||
// // output will be here in chunks
|
||||
// });
|
||||
|
||||
// or if you want to send output elsewhere
|
||||
//child.stdout.pipe(dest);
|
||||
},
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Generate a PDF from HTML using Phantom.
|
||||
See: https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js
|
||||
*/
|
||||
phantom: function( markup, fOut ) {
|
||||
|
||||
// Save the markup to a temporary file
|
||||
var tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
|
||||
FS.writeFileSync( tempFile, markup, 'utf8' );
|
||||
|
||||
var scriptPath = SLASH( PATH.relative( process.cwd(),
|
||||
PATH.resolve( __dirname, '../utils/rasterize.js' ) ) );
|
||||
var sourcePath = SLASH( PATH.relative( process.cwd(), tempFile) );
|
||||
var destPath = SLASH( PATH.relative( process.cwd(), fOut) );
|
||||
|
||||
var spawn = require('child_process').spawn;
|
||||
var child = spawn('phantomjs', [ scriptPath, sourcePath, destPath ]);
|
||||
|
||||
// child.stdout.on('data', function(chunk) {
|
||||
// // output will be here in chunks
|
||||
// });
|
||||
//
|
||||
// // or if you want to send output elsewhere
|
||||
// child.stdout.pipe(dest);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
}());
|
@ -45,7 +45,7 @@ Definition of the HtmlPdfGenerator class.
|
||||
catch(ex) {
|
||||
// { [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', ... }
|
||||
// { [Error: ENOENT] }
|
||||
throw { fluenterror: this.codes.wkhtmltopdf, inner: ex };
|
||||
throw { fluenterror: this.codes.pdfGeneration, inner: ex };
|
||||
}
|
||||
},
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user