diff --git a/package.json b/package.json index 3786e9d..8eb6ba2 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "simple-html-tokenizer": "^0.2.0", "string.prototype.startswith": "^0.2.0", "underscore": "^1.8.3", + "webshot": "^0.16.0", "wkhtmltopdf": "^0.1.5", "xml-escape": "^1.0.0", "yamljs": "^0.2.4" diff --git a/src/core/default-formats.js b/src/core/default-formats.js index a9620bf..ce344c3 100644 --- a/src/core/default-formats.js +++ b/src/core/default-formats.js @@ -10,10 +10,12 @@ { name: 'txt', ext: 'txt', gen: new FLUENT.TextGenerator() }, { name: 'doc', ext: 'doc', fmt: 'xml', gen: new FLUENT.WordGenerator() }, { name: 'pdf', ext: 'pdf', fmt: 'html', is: false, gen: new FLUENT.HtmlPdfGenerator() }, + { name: 'png', ext: 'png', fmt: 'html', is: false, gen: new FLUENT.HtmlPngGenerator() }, { name: 'md', ext: 'md', fmt: 'txt', gen: new FLUENT.MarkdownGenerator() }, { name: 'json', ext: 'json', gen: new FLUENT.JsonGenerator() }, { name: 'yml', ext: 'yml', fmt: 'yml', gen: new FLUENT.JsonYamlGenerator() }, { name: 'latex', ext: 'tex', fmt: 'latex', gen: new FLUENT.LaTeXGenerator() } + ]; }()); diff --git a/src/core/theme.js b/src/core/theme.js index 6e8356a..b9fe152 100644 --- a/src/core/theme.js +++ b/src/core/theme.js @@ -117,7 +117,7 @@ Definition of the Theme class. var portion = pathInfo.dirname.replace(tplFolder,''); if( portion && portion.trim() ) { if( portion[1] === '_' ) return; - var reg = /^(?:\/|\\)(html|latex|doc|pdf|partials)(?:\/|\\)?/ig; + var reg = /^(?:\/|\\)(html|latex|doc|pdf|png|partials)(?:\/|\\)?/ig; var res = reg.exec( portion ); if( res ) { if( res[1] !== 'partials' ) { diff --git a/src/eng/handlebars-generator.js b/src/eng/handlebars-generator.js index b244a3f..71dbf58 100644 --- a/src/eng/handlebars-generator.js +++ b/src/eng/handlebars-generator.js @@ -36,7 +36,7 @@ Definition of the HandlebarsGenerator class. // Compile and run the Handlebars template. var template = HANDLEBARS.compile(jst); return template({ - r: format === 'html' || format === 'pdf' ? json.markdownify() : json, + r: format === 'html' || format === 'pdf' || format === 'png' ? json.markdownify() : json, RAW: json, filt: opts.filters, cssInfo: cssInfo, diff --git a/src/eng/underscore-generator.js b/src/eng/underscore-generator.js index fe2fb36..0a9be84 100644 --- a/src/eng/underscore-generator.js +++ b/src/eng/underscore-generator.js @@ -35,7 +35,7 @@ Definition of the UnderscoreGenerator class. // Compile and run the template. TODO: avoid unnecessary recompiles. var compiled = _.template(jst); var ret = compiled({ - r: format === 'html' || format === 'pdf' ? json.markdownify() : json, + r: format === 'html' || format === 'pdf' || format === 'png' ? json.markdownify() : json, filt: opts.filters, XML: require('xml-escape'), RAW: json, diff --git a/src/gen/html-png-generator.js b/src/gen/html-png-generator.js new file mode 100644 index 0000000..4d9b5b9 --- /dev/null +++ b/src/gen/html-png-generator.js @@ -0,0 +1,42 @@ +/** +Definition of the HtmlPngGenerator class. +@license MIT. Copyright (c) 2015 James Devlin / FluentDesk. +@module html-png-generator.js +*/ + +(function() { + + var TemplateGenerator = require('./template-generator') + , FS = require('fs-extra') + , HTML = require( 'html' ); + + /** + An HTML-based PDF resume generator for HackMyResume. + */ + var HtmlPngGenerator = module.exports = TemplateGenerator.extend({ + + init: function() { + this._super( 'png', 'html' ); + }, + + /** + Generate the binary PDF. + */ + onBeforeSave: function( info ) { + png( info.mk, info.outputFile ); + return null; // halt further processing + } + + }); + + /** + Generate a PDF from HTML. + */ + function png( markup, fOut ) { + + require('webshot')( markup , { encoding: 'binary', siteType: 'html' } ) + .pipe( FS.createWriteStream( fOut ) ); + + } + +}()); diff --git a/src/hackmyapi.js b/src/hackmyapi.js index 6d96368..f36c40a 100644 --- a/src/hackmyapi.js +++ b/src/hackmyapi.js @@ -18,5 +18,6 @@ module.exports = { JsonGenerator: require('./gen/json-generator'), YamlGenerator: require('./gen/yaml-generator'), JsonYamlGenerator: require('./gen/json-yaml-generator'), - LaTeXGenerator: require('./gen/latex-generator') + LaTeXGenerator: require('./gen/latex-generator'), + HtmlPngGenerator: require('./gen/html-png-generator') };