mirror of
https://github.com/JuanCanham/HackMyResume.git
synced 2024-11-22 16:30: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:
parent
8d7cf32988
commit
0a8ee721e8
@ -62,6 +62,7 @@
|
|||||||
"moment": "^2.10.6",
|
"moment": "^2.10.6",
|
||||||
"parse-filepath": "^0.6.3",
|
"parse-filepath": "^0.6.3",
|
||||||
"path-exists": "^2.1.0",
|
"path-exists": "^2.1.0",
|
||||||
|
"phantom": "^0.8.4",
|
||||||
"recursive-readdir-sync": "^1.0.6",
|
"recursive-readdir-sync": "^1.0.6",
|
||||||
"simple-html-tokenizer": "^0.2.0",
|
"simple-html-tokenizer": "^0.2.0",
|
||||||
"string.prototype.startswith": "^0.2.0",
|
"string.prototype.startswith": "^0.2.0",
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
Definition of the HtmlPdfGenerator class.
|
Definition of the HtmlPdfGenerator class.
|
||||||
@module html-pdf-generator.js
|
@module html-pdf-generator.js
|
||||||
|
@license MIT. See LICENSE.md for details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@ -22,61 +23,59 @@ Definition of the HtmlPdfGenerator class.
|
|||||||
Generate the binary PDF.
|
Generate the binary PDF.
|
||||||
*/
|
*/
|
||||||
onBeforeSave: function( info ) {
|
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
|
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 Phantom.
|
||||||
/**
|
*/
|
||||||
Generate a PDF from HTML using wkhtmltopdf.
|
phantom: function( markup, fOut ) {
|
||||||
*/
|
require('phantom').create( function( ph ) {
|
||||||
function pdf_wkhtmltopdf( markup, fOut ) {
|
ph.createPage( function( page ) {
|
||||||
var wk;
|
page.setContent( markup );
|
||||||
try {
|
page.set('paperSize', {
|
||||||
wk = require('wkhtmltopdf');
|
format: 'A4',
|
||||||
wk( markup, { pageSize: 'letter' } )
|
orientation: 'portrait',
|
||||||
.pipe( FS.createWriteStream( fOut ) );
|
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 } } );
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
}());
|
}());
|
||||||
|
@ -152,7 +152,8 @@ Definition of the TemplateGenerator class.
|
|||||||
file.data = that.onBeforeSave({
|
file.data = that.onBeforeSave({
|
||||||
theme: theme,
|
theme: theme,
|
||||||
outputFile: (file.info.major ? f : thisFilePath),
|
outputFile: (file.info.major ? f : thisFilePath),
|
||||||
mk: file.data
|
mk: file.data,
|
||||||
|
opts: that.opts
|
||||||
});
|
});
|
||||||
if( !file.data ) return; // PDF etc
|
if( !file.data ) return; // PDF etc
|
||||||
}
|
}
|
||||||
@ -161,7 +162,7 @@ Definition of the TemplateGenerator class.
|
|||||||
FS.writeFileSync( fileName, file.data,
|
FS.writeFileSync( fileName, file.data,
|
||||||
{ encoding: 'utf8', flags: 'w' } );
|
{ encoding: 'utf8', flags: 'w' } );
|
||||||
that.onAfterSave && that.onAfterSave(
|
that.onAfterSave && that.onAfterSave(
|
||||||
{ outputFile: fileName, mk: file.data } );
|
{ outputFile: fileName, mk: file.data, opts: that.opts } );
|
||||||
}
|
}
|
||||||
catch(ex) {
|
catch(ex) {
|
||||||
require('../core/error-handler').err(ex, false);
|
require('../core/error-handler').err(ex, false);
|
||||||
|
@ -93,8 +93,9 @@ function main() {
|
|||||||
//.arguments('<sources> TO [targets]')
|
//.arguments('<sources> TO [targets]')
|
||||||
//.usage('...')
|
//.usage('...')
|
||||||
.option('-t --theme <theme>', 'Theme name or path', 'modern')
|
.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('-c --css <option>', 'CSS linking / embedding', 'embed')
|
||||||
|
.option('-p --pdf <engine>', 'PDF generation engine')
|
||||||
.description('Generate resume to multiple formats')
|
.description('Generate resume to multiple formats')
|
||||||
.action(function( sources, targets, options ) {
|
.action(function( sources, targets, options ) {
|
||||||
var x = splitSrcDest.call( this );
|
var x = splitSrcDest.call( this );
|
||||||
|
@ -52,6 +52,7 @@ Implementation of the 'generate' verb for HackMyResume.
|
|||||||
_opts.theme = (opts.theme && opts.theme.toLowerCase().trim())|| 'modern';
|
_opts.theme = (opts.theme && opts.theme.toLowerCase().trim())|| 'modern';
|
||||||
_opts.prettify = opts.prettify === true ? _opts.prettify : false;
|
_opts.prettify = opts.prettify === true ? _opts.prettify : false;
|
||||||
_opts.css = opts.css;
|
_opts.css = opts.css;
|
||||||
|
_opts.pdf = opts.pdf;
|
||||||
|
|
||||||
// If two or more files are passed to the GENERATE command and the TO
|
// If two or more files are passed to the GENERATE command and the TO
|
||||||
// keyword is omitted, the last file specifies the output file.
|
// 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)
|
, fName = PATH.basename(f, '.' + fType)
|
||||||
, theFormat;
|
, theFormat;
|
||||||
|
|
||||||
|
var suffix = '';
|
||||||
|
if( targInfo.fmt.outFormat === 'pdf' && _opts.pdf ) {
|
||||||
|
suffix = chalk.green(' (with ' + _opts.pdf + ')');
|
||||||
|
}
|
||||||
|
|
||||||
_log( chalk.green('Generating ') +
|
_log( chalk.green('Generating ') +
|
||||||
chalk.green.bold(targInfo.fmt.outFormat.toUpperCase()) +
|
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.
|
// If targInfo.fmt.files exists, this format is backed by a document.
|
||||||
// Fluent/FRESH themes are handled here.
|
// Fluent/FRESH themes are handled here.
|
||||||
|
Loading…
Reference in New Issue
Block a user