Last active
August 29, 2015 14:06
-
-
Save joshtronic/2ed8336067dd769bdbc7 to your computer and use it in GitHub Desktop.
PHP to HTML to PDF, BOOM!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.1/wkhtmltox-0.12.1_osx-carbon-i386.pkg | |
// Requires a hacked grunt-wkhtmltopdf/tasks/wkhtmltopdf.js file (attached to this gist) | |
module.exports = function(grunt) | |
{ | |
grunt.initConfig( | |
{ | |
shell: { | |
php2html: { | |
command: 'php index.php > index.html' | |
} | |
}, | |
watch: { | |
files: ['index.php'], | |
tasks: ['shell', 'wkhtmltopdf'], | |
}, | |
wkhtmltopdf: { | |
dev: { | |
args: [ | |
'-O', 'Landscape', | |
'-s', 'Letter', | |
'-T', 0, | |
'-R', 0, | |
'-B', 0, | |
'-L', 0 | |
], | |
src: 'index.html', | |
dest: './' | |
} | |
} | |
}); | |
grunt.loadNpmTasks('grunt-contrib-watch'); | |
grunt.loadNpmTasks('grunt-shell'); | |
grunt.loadNpmTasks('grunt-wkhtmltopdf'); | |
grunt.registerTask('default', ['shell', 'wkhtmltopdf']); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* grunt-wkhtmltopdf | |
* https://github.com/dhar/grunt-wkhtmltopdf | |
* | |
* Copyright (c) 2012 Olivier Audard | |
* Licensed under the MIT license. | |
*/ | |
/*globals module:false, require:false*/ | |
module.exports = function(grunt) { | |
// Please see the grunt documentation for more information regarding task and | |
// helper creation: https://github.com/cowboy/grunt/blob/master/docs/toc.md | |
// ========================================================================== | |
// TASKS | |
// ========================================================================== | |
var helper = require('./lib/wkhtmltopdf-lib').init(grunt); | |
grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() { | |
this.files.forEach(function(file) { | |
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); | |
file.src.forEach(function(src) { | |
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 | |
); | |
// default args | |
var 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 | |
]; | |
// overrides the args | |
if (file.args) { | |
args = file.args; | |
} | |
// adds the src and dest | |
args = args.concat([src, dest]); | |
// Launch wkhtmltopdf. | |
helper.convert({ | |
code: 90, | |
args: args, | |
done: function(err) { | |
if (err) { | |
grunt.log('>>>', err); | |
} | |
} | |
}); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment