1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-22 08:20:11 +00:00

Allow for multiple PDF engines / support Phantom PDFs.

Start formalizing PDF generation apparatus and support a `--pdf`
parameter allowing the user to specify the flavor of PDF generation.
This commit is contained in:
hacksalot 2016-01-03 04:11:42 -05:00
parent 8d7cf32988
commit 0a8ee721e8
5 changed files with 58 additions and 50 deletions

View File

@ -62,6 +62,7 @@
"moment": "^2.10.6",
"parse-filepath": "^0.6.3",
"path-exists": "^2.1.0",
"phantom": "^0.8.4",
"recursive-readdir-sync": "^1.0.6",
"simple-html-tokenizer": "^0.2.0",
"string.prototype.startswith": "^0.2.0",

View File

@ -1,6 +1,7 @@
/**
Definition of the HtmlPdfGenerator class.
@module html-pdf-generator.js
@license MIT. See LICENSE.md for details.
*/
(function() {
@ -22,61 +23,59 @@ Definition of the HtmlPdfGenerator class.
Generate the binary PDF.
*/
onBeforeSave: function( info ) {
pdf.call( this, info.mk, info.outputFile );
engines[ info.opts.pdf || 'wkhtmltopdf' ]
.call( this, info.mk, info.outputFile );
return null; // halt further processing
}
});
/**
Generate a PDF from HTML.
*/
function pdf( markup, fOut ) {
pdf_wkhtmltopdf.call( this, markup, fOut );
var engines = {
/**
Generate a PDF from HTML using wkhtmltopdf.
*/
wkhtmltopdf: function(markup, fOut) {
var wk;
try {
wk = require('wkhtmltopdf');
wk( markup, { pageSize: 'letter' } )
.pipe( FS.createWriteStream( fOut ) );
}
catch(ex) {
// { [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', ... }
// { [Error: ENOENT] }
throw { fluenterror: this.codes.wkhtmltopdf };
}
},
}
/**
Generate a PDF from HTML using wkhtmltopdf.
*/
function pdf_wkhtmltopdf( markup, fOut ) {
var wk;
try {
wk = require('wkhtmltopdf');
wk( markup, { pageSize: 'letter' } )
.pipe( FS.createWriteStream( fOut ) );
/**
Generate a PDF from HTML using Phantom.
*/
phantom: function( markup, fOut ) {
require('phantom').create( function( ph ) {
ph.createPage( function( page ) {
page.setContent( markup );
page.set('paperSize', {
format: 'A4',
orientation: 'portrait',
margin: '1cm'
});
page.set("viewportSize", {
width: 1024, // TODO: option-ify
height: 768 // TODO: Use "A" sizes
});
page.set('onLoadFinished', function(success) {
page.render( fOut );
ph.exit();
});
},
{ dnodeOpts: { weak: false } } );
});
}
catch(ex) {
// { [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', syscall: 'write' }
// { [Error: ENOENT] }
throw { fluenterror: this.codes.wkhtmltopdf };
}
}
};
// function pdf_phantom() {
// pdfCount++;
// require('phantom').create( function( ph ) {
// ph.createPage( function( page ) {
// page.setContent( markup );
// page.set('paperSize', {
// format: 'A4',
// orientation: 'portrait',
// margin: '1cm'
// });
// page.set("viewportSize", {
// width: 1024, // TODO: option-ify
// height: 768 // TODO: Use "A" sizes
// });
// page.set('onLoadFinished', function(success) {
// page.render( fOut );
// pdfCount++;
// ph.exit();
// });
// },
// { dnodeOpts: { weak: false } } );
// });
// }
}());

View File

@ -152,7 +152,8 @@ Definition of the TemplateGenerator class.
file.data = that.onBeforeSave({
theme: theme,
outputFile: (file.info.major ? f : thisFilePath),
mk: file.data
mk: file.data,
opts: that.opts
});
if( !file.data ) return; // PDF etc
}
@ -161,7 +162,7 @@ Definition of the TemplateGenerator class.
FS.writeFileSync( fileName, file.data,
{ encoding: 'utf8', flags: 'w' } );
that.onAfterSave && that.onAfterSave(
{ outputFile: fileName, mk: file.data } );
{ outputFile: fileName, mk: file.data, opts: that.opts } );
}
catch(ex) {
require('../core/error-handler').err(ex, false);

View File

@ -93,8 +93,9 @@ function main() {
//.arguments('<sources> TO [targets]')
//.usage('...')
.option('-t --theme <theme>', 'Theme name or path', 'modern')
.option('-p --prettify', 'Preffity HTML output', true)
.option('-n --no-prettify', 'Disable HTML prettification', true)
.option('-c --css <option>', 'CSS linking / embedding', 'embed')
.option('-p --pdf <engine>', 'PDF generation engine')
.description('Generate resume to multiple formats')
.action(function( sources, targets, options ) {
var x = splitSrcDest.call( this );

View File

@ -52,6 +52,7 @@ Implementation of the 'generate' verb for HackMyResume.
_opts.theme = (opts.theme && opts.theme.toLowerCase().trim())|| 'modern';
_opts.prettify = opts.prettify === true ? _opts.prettify : false;
_opts.css = opts.css;
_opts.pdf = opts.pdf;
// If two or more files are passed to the GENERATE command and the TO
// keyword is omitted, the last file specifies the output file.
@ -112,9 +113,14 @@ Implementation of the 'generate' verb for HackMyResume.
, fName = PATH.basename(f, '.' + fType)
, theFormat;
var suffix = '';
if( targInfo.fmt.outFormat === 'pdf' && _opts.pdf ) {
suffix = chalk.green(' (with ' + _opts.pdf + ')');
}
_log( chalk.green('Generating ') +
chalk.green.bold(targInfo.fmt.outFormat.toUpperCase()) +
chalk.green(' resume: ') + chalk.green.bold( PATH.relative(process.cwd(), f )) );
chalk.green(' resume') + suffix + chalk.green(': ') + chalk.green.bold( PATH.relative(process.cwd(), f )) );
// If targInfo.fmt.files exists, this format is backed by a document.
// Fluent/FRESH themes are handled here.