From 0a8ee721e8f8918ba6bc3987561f7d3247b9b26c Mon Sep 17 00:00:00 2001 From: hacksalot Date: Sun, 3 Jan 2016 04:11:42 -0500 Subject: [PATCH] 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. --- package.json | 1 + src/gen/html-pdf-generator.js | 91 +++++++++++++++++------------------ src/gen/template-generator.js | 5 +- src/index.js | 3 +- src/verbs/generate.js | 8 ++- 5 files changed, 58 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index c031a5b..eab3a21 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/gen/html-pdf-generator.js b/src/gen/html-pdf-generator.js index 8d433ed..5eb931d 100644 --- a/src/gen/html-pdf-generator.js +++ b/src/gen/html-pdf-generator.js @@ -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 } } ); - // }); - // } }()); diff --git a/src/gen/template-generator.js b/src/gen/template-generator.js index 2eadd55..a61046c 100644 --- a/src/gen/template-generator.js +++ b/src/gen/template-generator.js @@ -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); diff --git a/src/index.js b/src/index.js index b8244c0..6a3d7c9 100644 --- a/src/index.js +++ b/src/index.js @@ -93,8 +93,9 @@ function main() { //.arguments(' TO [targets]') //.usage('...') .option('-t --theme ', 'Theme name or path', 'modern') - .option('-p --prettify', 'Preffity HTML output', true) + .option('-n --no-prettify', 'Disable HTML prettification', true) .option('-c --css