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

Merge pull request #189 from ryneeverett/weasyprint

Add WeasyPrint pdf generator support.
This commit is contained in:
hacksalot 2018-01-25 12:10:25 -05:00 committed by GitHub
commit d2d9039abb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 13 deletions

View File

@ -62,9 +62,9 @@ Alternately, install the latest bleeding-edge version (updated daily):
HackMyResume tries not to impose a specific PDF engine requirement on HackMyResume tries not to impose a specific PDF engine requirement on
the user, but will instead work with whatever PDF engines you have installed. the user, but will instead work with whatever PDF engines you have installed.
Currently, HackMyResume's PDF generation requires either [Phantom.js][2] or Currently, HackMyResume's PDF generation requires either [Phantom.js][2],
[wkhtmltopdf][3] to be installed on your system and the `phantomjs` and/or [wkhtmltopdf][3], or [WeasyPrint][11] to be installed on your system and the
`wkhtmltopdf` binaries to be accessible on your PATH. This is an optional corresponding binary to be accessible on your PATH. This is an optional
requirement for users who care about PDF formats. If you don't care about PDF requirement for users who care about PDF formats. If you don't care about PDF
formats, skip this step. formats, skip this step.
@ -320,7 +320,8 @@ PDF engine you have installed through the engine's command-line interface (CLI).
Currently that means one or both of... Currently that means one or both of...
- [wkhtmltopdf][3] - [wkhtmltopdf][3]
- [Phantom.js][3] - [Phantom.js][2]
- [WeasyPrint][11]
..with support for other engines planned in the future. But for now, **one or ..with support for other engines planned in the future. But for now, **one or
both of these engines must be installed and accessible on your PATH in order to both of these engines must be installed and accessible on your PATH in order to
@ -330,6 +331,7 @@ invoke either of these tools directly from your shell or terminal without error:
```bash ```bash
wkhtmltopdf input.html output.pdf wkhtmltopdf input.html output.pdf
phantomjs script.js input.html output.pdf phantomjs script.js input.html output.pdf
weasyprint input.html output.pdf
``` ```
Assuming you've installed one or both of these engines on your system, you can Assuming you've installed one or both of these engines on your system, you can
@ -339,6 +341,7 @@ tell HackMyResume which flavor of PDF generation to use via the `--pdf` option
```bash ```bash
hackmyresume BUILD resume.json TO out.all --pdf phantom hackmyresume BUILD resume.json TO out.all --pdf phantom
hackmyresume BUILD resume.json TO out.all --pdf wkhtmltopdf hackmyresume BUILD resume.json TO out.all --pdf wkhtmltopdf
hackmyresume BUILD resume.json TO out.all --pdf weasyprint
hackmyresume BUILD resume.json TO out.all --pdf none hackmyresume BUILD resume.json TO out.all --pdf none
``` ```
@ -490,6 +493,10 @@ The options file can contain any documented HackMyResume option, including
"sectionTitles": { "sectionTitles": {
"employment": "Work" "employment": "Work"
} }
// Change wkhtmltopdf margins
"wkhtmltopdf": {
"margin-top": "20mm"
}
} }
``` ```
@ -554,6 +561,7 @@ MIT. Go crazy. See [LICENSE.md][1] for details.
[8]: https://youtu.be/N9wsjroVlu8 [8]: https://youtu.be/N9wsjroVlu8
[9]: https://api.jquery.com/jquery.extend/ [9]: https://api.jquery.com/jquery.extend/
[10]: https://github.com/beautify-web/js-beautify [10]: https://github.com/beautify-web/js-beautify
[11]: http://weasyprint.org/
[fresh]: https://github.com/fluentdesk/FRESH [fresh]: https://github.com/fluentdesk/FRESH
[fresca]: https://github.com/fluentdesk/FRESCA [fresca]: https://github.com/fluentdesk/FRESCA
[dry]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself [dry]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself

View File

@ -52,7 +52,7 @@ Definition of the HtmlPdfCLIGenerator class.
} }
if (_.has(engines, safe_eng)) { if (_.has(engines, safe_eng)) {
this.errHandler = info.opts.errHandler; this.errHandler = info.opts.errHandler;
engines[safe_eng].call(this, info.mk, info.outputFile, this.onError); engines[safe_eng].call(this, info.mk, info.outputFile, info.opts, this.onError);
return null; return null;
} }
}; };
@ -86,11 +86,19 @@ Definition of the HtmlPdfCLIGenerator class.
TODO: If HTML generation has run, reuse that output TODO: If HTML generation has run, reuse that output
TODO: Local web server to ease wkhtmltopdf rendering TODO: Local web server to ease wkhtmltopdf rendering
*/ */
wkhtmltopdf: function(markup, fOut, on_error) { wkhtmltopdf: function(markup, fOut, opts, on_error) {
var tempFile; var tempFile, wkhtmltopdf_args, wkhtmltopdf_options;
tempFile = fOut.replace(/\.pdf$/i, '.pdf.html'); tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
FS.writeFileSync(tempFile, markup, 'utf8'); FS.writeFileSync(tempFile, markup, 'utf8');
SPAWN('wkhtmltopdf', [tempFile, fOut], false, on_error, this); wkhtmltopdf_options = _.extend({
'margin-bottom': '10mm',
'margin-top': '10mm'
}, opts.wkhtmltopdf);
wkhtmltopdf_options = _.flatten(_.map(wkhtmltopdf_options, function(v, k) {
return ['--' + k, v];
}));
wkhtmltopdf_args = wkhtmltopdf_options.concat([tempFile, fOut]);
SPAWN('wkhtmltopdf', wkhtmltopdf_args, false, on_error, this);
}, },
/** /**
@ -100,7 +108,7 @@ Definition of the HtmlPdfCLIGenerator class.
TODO: If HTML generation has run, reuse that output TODO: If HTML generation has run, reuse that output
TODO: Local web server to ease Phantom rendering TODO: Local web server to ease Phantom rendering
*/ */
phantomjs: function(markup, fOut, on_error) { phantomjs: function(markup, fOut, opts, on_error) {
var destPath, scriptPath, sourcePath, tempFile; var destPath, scriptPath, sourcePath, tempFile;
tempFile = fOut.replace(/\.pdf$/i, '.pdf.html'); tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
FS.writeFileSync(tempFile, markup, 'utf8'); FS.writeFileSync(tempFile, markup, 'utf8');
@ -109,6 +117,19 @@ Definition of the HtmlPdfCLIGenerator class.
sourcePath = SLASH(PATH.relative(process.cwd(), tempFile)); sourcePath = SLASH(PATH.relative(process.cwd(), tempFile));
destPath = SLASH(PATH.relative(process.cwd(), fOut)); destPath = SLASH(PATH.relative(process.cwd(), fOut));
SPAWN('phantomjs', [scriptPath, sourcePath, destPath], false, on_error, this); SPAWN('phantomjs', [scriptPath, sourcePath, destPath], false, on_error, this);
},
/**
Generate a PDF from HTML using WeasyPrint's CLI interface.
Spawns a child process with `weasyprint <source> <target>`. Weasy Print
must be installed and path-accessible.
TODO: If HTML generation has run, reuse that output
*/
weasyprint: function(markup, fOut, opts, on_error) {
var tempFile;
tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
FS.writeFileSync(tempFile, markup, 'utf8');
SPAWN('weasyprint', [tempFile, fOut], false, on_error, this);
} }
}; };

1
dist/verbs/build.js vendored
View File

@ -242,6 +242,7 @@ Implementation of the 'build' verb for HackMyResume.
_opts.noTips = opts.noTips; _opts.noTips = opts.noTips;
_opts.debug = opts.debug; _opts.debug = opts.debug;
_opts.sort = opts.sort; _opts.sort = opts.sort;
_opts.wkhtmltopdf = opts.wkhtmltopdf;
that = this; that = this;
_opts.onTransform = function(info) { _opts.onTransform = function(info) {
that.stat(HMEVENT.afterTransform, info); that.stat(HMEVENT.afterTransform, info);

View File

@ -37,7 +37,7 @@ module.exports = class HtmlPdfCLIGenerator extends TemplateGenerator
safe_eng = 'phantomjs' if safe_eng == 'phantom' safe_eng = 'phantomjs' if safe_eng == 'phantom'
if _.has engines, safe_eng if _.has engines, safe_eng
@errHandler = info.opts.errHandler @errHandler = info.opts.errHandler
engines[ safe_eng ].call @, info.mk, info.outputFile, @onError engines[ safe_eng ].call @, info.mk, info.outputFile, info.opts, @onError
return null # halt further processing return null # halt further processing
@ -64,11 +64,20 @@ engines =
TODO: If HTML generation has run, reuse that output TODO: If HTML generation has run, reuse that output
TODO: Local web server to ease wkhtmltopdf rendering TODO: Local web server to ease wkhtmltopdf rendering
### ###
wkhtmltopdf: (markup, fOut, on_error) -> wkhtmltopdf: (markup, fOut, opts, on_error) ->
# Save the markup to a temporary file # Save the markup to a temporary file
tempFile = fOut.replace /\.pdf$/i, '.pdf.html' tempFile = fOut.replace /\.pdf$/i, '.pdf.html'
FS.writeFileSync tempFile, markup, 'utf8' FS.writeFileSync tempFile, markup, 'utf8'
SPAWN 'wkhtmltopdf', [ tempFile, fOut ], false, on_error, @
# Prepare wkhtmltopdf arguments.
wkhtmltopdf_options = _.extend(
{'margin-bottom': '10mm', 'margin-top': '10mm'}, opts.wkhtmltopdf)
wkhtmltopdf_options = _.flatten(_.map(wkhtmltopdf_options, (v, k)->
return ['--' + k, v]
))
wkhtmltopdf_args = wkhtmltopdf_options.concat [ tempFile, fOut ]
SPAWN 'wkhtmltopdf', wkhtmltopdf_args , false, on_error, @
return return
@ -80,7 +89,7 @@ engines =
TODO: If HTML generation has run, reuse that output TODO: If HTML generation has run, reuse that output
TODO: Local web server to ease Phantom rendering TODO: Local web server to ease Phantom rendering
### ###
phantomjs: ( markup, fOut, on_error ) -> phantomjs: ( markup, fOut, opts, on_error ) ->
# Save the markup to a temporary file # Save the markup to a temporary file
tempFile = fOut.replace /\.pdf$/i, '.pdf.html' tempFile = fOut.replace /\.pdf$/i, '.pdf.html'
FS.writeFileSync tempFile, markup, 'utf8' FS.writeFileSync tempFile, markup, 'utf8'
@ -90,3 +99,17 @@ engines =
destPath = SLASH PATH.relative( process.cwd(), fOut) destPath = SLASH PATH.relative( process.cwd(), fOut)
SPAWN 'phantomjs', [ scriptPath, sourcePath, destPath ], false, on_error, @ SPAWN 'phantomjs', [ scriptPath, sourcePath, destPath ], false, on_error, @
return return
###*
Generate a PDF from HTML using WeasyPrint's CLI interface.
Spawns a child process with `weasyprint <source> <target>`. Weasy Print
must be installed and path-accessible.
TODO: If HTML generation has run, reuse that output
###
weasyprint: ( markup, fOut, opts, on_error ) ->
# Save the markup to a temporary file
tempFile = fOut.replace /\.pdf$/i, '.pdf.html'
FS.writeFileSync tempFile, markup, 'utf8'
SPAWN 'weasyprint', [tempFile, fOut], false, on_error, @
return

View File

@ -176,6 +176,7 @@ _prep = ( src, dst, opts ) ->
_opts.noTips = opts.noTips _opts.noTips = opts.noTips
_opts.debug = opts.debug _opts.debug = opts.debug
_opts.sort = opts.sort _opts.sort = opts.sort
_opts.wkhtmltopdf = opts.wkhtmltopdf
that = @ that = @
# Set up callbacks for internal generators # Set up callbacks for internal generators