1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-05-12 00:27:08 +01:00
Files
HackMyResume/src/gen/html-pdf-generator.js
hacksalot 0a8ee721e8 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.
2016-01-03 04:11:42 -05:00

82 lines
1.9 KiB
JavaScript

/**
Definition of the HtmlPdfGenerator 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' );
/**
An HTML-driven PDF resume generator for HackMyResume.
*/
var HtmlPdfGenerator = module.exports = TemplateGenerator.extend({
init: function() {
this._super( 'pdf', 'html' );
},
/**
Generate the binary PDF.
*/
onBeforeSave: function( info ) {
engines[ info.opts.pdf || 'wkhtmltopdf' ]
.call( this, info.mk, info.outputFile );
return null; // halt further processing
}
});
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.
*/
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 } } );
});
}
};
}());