Initial import
This commit is contained in:
commit
21637cf3a6
8 changed files with 289 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
.npmignore
|
||||
node_modules
|
22
LICENSE-MIT
Normal file
22
LICENSE-MIT
Normal file
|
@ -0,0 +1,22 @@
|
|||
Copyright (c) 2012 Olivier Audard
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
57
README.md
Normal file
57
README.md
Normal file
|
@ -0,0 +1,57 @@
|
|||
# grunt-wkhtmltopdf
|
||||
|
||||
A simple [Grunt][grunt] task that uses [wkhtmltopdf][wkhtmltopdf] to convert HTML files to PDF.
|
||||
Convertion to PDF takes care of `@media print` CSS rules and preserves links to remote web pages.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Setting up wkhtmltopdf
|
||||
|
||||
Download and install wkhtmltopdf from the [project page][wkhtmltopdf_dl].
|
||||
|
||||
Make sure `wkhtmltopdf` is accessible from your `PATH` (Try `wkhtmltopdf -V` in your Terminal).
|
||||
I'm on Mas OS X, so I created the following symlink to my `/usr/local/bin/` folder:
|
||||
|
||||
```
|
||||
ln -s /usr/local/bin/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf /usr/local/bin/wkhtmltopdf
|
||||
```
|
||||
|
||||
### Use it with grunt
|
||||
|
||||
Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-wkhtmltopdf`
|
||||
|
||||
Then add this line to your project's `grunt.js` gruntfile:
|
||||
|
||||
```javascript
|
||||
grunt.loadNpmTasks('grunt-wkhtmltopdf');
|
||||
```
|
||||
|
||||
[wkhtmltopdf]: http://code.google.com/p/wkhtmltopdf/
|
||||
[wkhtmltopdf_dl]: http://code.google.com/p/wkhtmltopdf/downloads/list
|
||||
[grunt]: https://github.com/cowboy/grunt
|
||||
[getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
|
||||
|
||||
## Documentation
|
||||
Simply add the following to your gruntfile:
|
||||
|
||||
```javascript
|
||||
//...
|
||||
wkhtmltopdf: {
|
||||
src: 'path/to/some/html/file/*.html',
|
||||
dest: 'pdf/output/'
|
||||
},
|
||||
//...
|
||||
```
|
||||
|
||||
Then run `grunt wkhtmltopdf` or use it as any other grunt task. Every `html` file in your `path/to/some/html/file/` folder will be turned into a PDF and saved to `pdf/output/` folder.
|
||||
|
||||
## Contributing
|
||||
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt][grunt].
|
||||
|
||||
## Release History
|
||||
|
||||
- *v0.1.0*: First Release
|
||||
|
||||
## License
|
||||
Copyright (c) 2012 Olivier Audard
|
||||
Licensed under the MIT license.
|
2
bin/grunt-wkhtmltopdf
Normal file
2
bin/grunt-wkhtmltopdf
Normal file
|
@ -0,0 +1,2 @@
|
|||
#!/usr/bin/env node
|
||||
require('grunt').npmTasks('grunt-wkhtmltopdf').cli();
|
40
grunt.js
Normal file
40
grunt.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
module.exports = function(grunt) {
|
||||
|
||||
// Project configuration.
|
||||
grunt.initConfig({
|
||||
test: {
|
||||
files: ['test/**/*.js']
|
||||
},
|
||||
lint: {
|
||||
files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js']
|
||||
},
|
||||
watch: {
|
||||
files: '<config:lint.files>',
|
||||
tasks: 'default'
|
||||
},
|
||||
jshint: {
|
||||
options: {
|
||||
curly: true,
|
||||
eqeqeq: true,
|
||||
immed: true,
|
||||
latedef: true,
|
||||
newcap: true,
|
||||
noarg: true,
|
||||
sub: true,
|
||||
undef: true,
|
||||
boss: true,
|
||||
eqnull: true,
|
||||
node: true,
|
||||
es5: true
|
||||
},
|
||||
globals: {}
|
||||
}
|
||||
});
|
||||
|
||||
// Load local tasks.
|
||||
grunt.loadTasks('tasks');
|
||||
|
||||
// Default task.
|
||||
grunt.registerTask('default', 'lint test');
|
||||
|
||||
};
|
41
package.json
Normal file
41
package.json
Normal file
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"name": "grunt-wkhtmltopdf",
|
||||
"description": "Grunt Task that uses wkhtmltopdf to convert HTML files to PDF",
|
||||
"version": "0.1.0",
|
||||
"homepage": "https://github.com/dhar/grunt-wkhtmltopdf",
|
||||
"author": {
|
||||
"name": "Olivier Audard",
|
||||
"email": "audard@gmail.com",
|
||||
"url": "http://www.dhar.fr/"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/dhar/grunt-wkhtmltopdf.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/dhar/grunt-wkhtmltopdf/issues"
|
||||
},
|
||||
"licenses": [
|
||||
{
|
||||
"type": "MIT",
|
||||
"url": "https://github.com/dhar/grunt-wkhtmltopdf/blob/master/LICENSE-MIT"
|
||||
}
|
||||
],
|
||||
"main": "grunt.js",
|
||||
"bin": "bin/grunt-wkhtmltopdf",
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "grunt test"
|
||||
},
|
||||
"dependencies": {
|
||||
"grunt": "~0.3.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"grunt": "~0.3.9"
|
||||
},
|
||||
"keywords": [
|
||||
"gruntplugin"
|
||||
]
|
||||
}
|
91
tasks/wkhtmltopdf.js
Normal file
91
tasks/wkhtmltopdf.js
Normal file
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* grunt-wkhtmltopdf
|
||||
* https://github.com/dhar/grunt-wkhtmltopdf
|
||||
*
|
||||
* Copyright (c) 2012 Olivier Audard
|
||||
* Licensed under the MIT license.
|
||||
*/
|
||||
/*globals module: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
|
||||
// ==========================================================================
|
||||
|
||||
grunt.registerTask('wkhtmltopdf', 'Your task description goes here.', function() {
|
||||
grunt.config.requires('wkhtmltopdf.src');
|
||||
grunt.config.requires('wkhtmltopdf.dest');
|
||||
|
||||
var conf = grunt.config('wkhtmltopdf');
|
||||
|
||||
var htmlFiles = grunt.file.expandFiles(conf.src),
|
||||
dest = (conf.dest && conf.dest !== '') ? conf.dest + '/' : '';
|
||||
|
||||
grunt.log.writeln("pdf output is: " + dest);
|
||||
|
||||
htmlFiles.forEach(function(srcpath) {
|
||||
var dir = dest + srcpath.replace(/.*\/([^\/]+)\/[^\/]+\.html/, '$1');
|
||||
|
||||
// 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.
|
||||
grunt.helper('wkhtmltopdf', {
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ==========================================================================
|
||||
// HELPERS
|
||||
// ==========================================================================
|
||||
|
||||
grunt.registerHelper('wkhtmltopdf', function(options) {
|
||||
|
||||
if (!options || !options.args) {
|
||||
grunt.warn("You need to specify atleast one input file, and exactly one output file");
|
||||
return null;
|
||||
}
|
||||
|
||||
return grunt.utils.spawn({
|
||||
cmd: 'wkhtmltopdf',
|
||||
args: options.args
|
||||
}, function(err, result, code) {
|
||||
grunt.log.writeln('wkhtmltopdf done');
|
||||
if (!err) { return options.done(null); }
|
||||
// Something went horribly wrong.
|
||||
grunt.verbose.or.writeln();
|
||||
grunt.log.write('Running wkhtmltopdf...').error();
|
||||
if (code === 127) {
|
||||
grunt.log.errorlns(
|
||||
'In order for this task to work properly, wkhtmltopdf must be ' +
|
||||
'installed and in the system PATH (if you can run "wkhtmltopdf" at' +
|
||||
' the command line, this task should work). Unfortunately, ' +
|
||||
'wkhtmltopdf cannot be installed automatically via npm or grunt. '
|
||||
);
|
||||
grunt.warn('wkhtmltopdf not found.', options.code);
|
||||
} else {
|
||||
result.split('\n').forEach(grunt.log.error, grunt.log);
|
||||
grunt.warn('wkhtmltopdf exited unexpectedly with exit code ' + code + '.', options.code);
|
||||
}
|
||||
options.done(code);
|
||||
});
|
||||
});
|
||||
|
||||
};
|
34
test/wkhtmltopdf_test.js
Normal file
34
test/wkhtmltopdf_test.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*globals require:false exports:false*/
|
||||
var grunt = require('grunt');
|
||||
|
||||
/*
|
||||
======== A Handy Little Nodeunit Reference ========
|
||||
https://github.com/caolan/nodeunit
|
||||
|
||||
Test methods:
|
||||
test.expect(numAssertions)
|
||||
test.done()
|
||||
Test assertions:
|
||||
test.ok(value, [message])
|
||||
test.equal(actual, expected, [message])
|
||||
test.notEqual(actual, expected, [message])
|
||||
test.deepEqual(actual, expected, [message])
|
||||
test.notDeepEqual(actual, expected, [message])
|
||||
test.strictEqual(actual, expected, [message])
|
||||
test.notStrictEqual(actual, expected, [message])
|
||||
test.throws(block, [error], [message])
|
||||
test.doesNotThrow(block, [error], [message])
|
||||
test.ifError(value)
|
||||
*/
|
||||
|
||||
exports['wkhtmltopdf'] = {
|
||||
setUp: function(done) {
|
||||
// setup here
|
||||
done();
|
||||
},
|
||||
'helper': function(test) {
|
||||
test.expect(0);
|
||||
// tests here
|
||||
test.done();
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue