Improve predictability of the destination for created PDFs.
Now generated PDFs are placed at the destination specified in the end-users's Gruntfile, rather than appending the source file's path to the destination path. The created PDFs have the same name as the source HTML file, excep the file extension is changed to .pdf.
This commit is contained in:
parent
469ff6bb42
commit
dad2687587
1 changed files with 43 additions and 22 deletions
|
@ -19,32 +19,53 @@ module.exports = function(grunt) {
|
|||
grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() {
|
||||
|
||||
this.files.forEach(function(file) {
|
||||
var srcpath = file.src.toString(),
|
||||
dest = (file.dest && file.dest !== '') ? file.dest + '/' : '';
|
||||
|
||||
grunt.log.writeln("pdf output is: " + dest);
|
||||
var pathlib = require('path');
|
||||
// calculate the destination directory and ensure it exists, since
|
||||
// wkhtmltopdf won't create the PDF if the destination directory doesn't
|
||||
// exist
|
||||
var destPath = file.dest;
|
||||
if (grunt.file.isFile(file.dest)) {
|
||||
destPath = pathlib.dirname(file.dest);
|
||||
}
|
||||
grunt.file.mkdir(destPath);
|
||||
|
||||
var dir = dest + srcpath.replace(/.*\/([^\/]+)\/[^\/]+\.html/, '$1');
|
||||
file.src.forEach(function(src) {
|
||||
|
||||
// Create dest folder as wkhtmltopdf won't generate output if it doesn't exist
|
||||
grunt.file.mkdir(dir);
|
||||
var destpath = dir + '/' +
|
||||
srcpath.replace(/.*\/([^\/]+)\.html/, '$1.pdf');
|
||||
|
||||
// Launch PhantomJS.
|
||||
helper.convert({
|
||||
code: 90,
|
||||
args: [
|
||||
'--dpi', '96', // workarround to wkhtmltopdf letter-spacing bug (see http://code.google.com/p/wkhtmltopdf/issues/detail?id=72)
|
||||
'--print-media-type', // Use @print media type
|
||||
srcpath,
|
||||
destpath
|
||||
],
|
||||
done: function(err) {
|
||||
if (err) {
|
||||
grunt.log('>>>', err);
|
||||
}
|
||||
var dest = file.dest;
|
||||
// wkhtmltopdf seems to require that the destination be a file
|
||||
// location, not a directory, so if the given destination is a
|
||||
// directory then append the name of the source file but with the
|
||||
// extension changed to .pdf
|
||||
if (grunt.file.isDir(dest)) {
|
||||
var srcFileName = pathlib.basename(src);
|
||||
var srcFileExtension = pathlib.extname(src);
|
||||
var destFileName = srcFileName.replace(
|
||||
new RegExp(srcFileExtension + "$"), // match only the end of the string
|
||||
".pdf"
|
||||
);
|
||||
dest = pathlib.join(destPath + destFileName);
|
||||
}
|
||||
|
||||
grunt.log.writeln(
|
||||
"Converting " + src + " -> " + dest
|
||||
);
|
||||
|
||||
// Launch PhantomJS.
|
||||
helper.convert({
|
||||
code: 90,
|
||||
args: [
|
||||
'--dpi', '96', // workarround to wkhtmltopdf letter-spacing bug (see http://code.google.com/p/wkhtmltopdf/issues/detail?id=72)
|
||||
'--print-media-type', // Use @print media type
|
||||
src,
|
||||
dest
|
||||
],
|
||||
done: function(err) {
|
||||
if (err) {
|
||||
grunt.log('>>>', err);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue