Compare commits
19 commits
Author | SHA1 | Date | |
---|---|---|---|
|
f7a76fa952 | ||
|
69a270f604 | ||
|
0b93441169 | ||
|
ac6e85b1c2 | ||
|
696955b345 | ||
|
38007b19ad | ||
|
b21de20c73 | ||
|
5b29b57245 | ||
|
4a06d92741 | ||
|
2bcb115dfe | ||
|
7344889930 | ||
|
dad2687587 | ||
|
469ff6bb42 | ||
|
7f9813423f | ||
|
847b3f3697 | ||
|
d783bdedb9 | ||
|
b8aad1b7d8 | ||
|
7dd749efb8 | ||
|
f8bfe3cdb9 |
4 changed files with 140 additions and 48 deletions
|
@ -1,12 +1,14 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
module.exports = function(grunt) {
|
module.exports = function(grunt) {
|
||||||
|
|
||||||
|
// Load grunt tasks automatically
|
||||||
|
require('load-grunt-tasks')(grunt);
|
||||||
|
|
||||||
// Project configuration.
|
// Project configuration.
|
||||||
grunt.initConfig({
|
grunt.initConfig({
|
||||||
test: {
|
nodeunit: {
|
||||||
files: ['test/**/*.js']
|
all: ['test/**/_test.js']
|
||||||
},
|
|
||||||
lint: {
|
|
||||||
files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js']
|
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
files: '<config:lint.files>',
|
files: '<config:lint.files>',
|
||||||
|
@ -25,16 +27,19 @@ module.exports = function(grunt) {
|
||||||
boss: true,
|
boss: true,
|
||||||
eqnull: true,
|
eqnull: true,
|
||||||
node: true,
|
node: true,
|
||||||
es5: true
|
|
||||||
},
|
},
|
||||||
globals: {}
|
all: [
|
||||||
|
'Gruntfile.js',
|
||||||
|
'tasks/{,*/}*.js',
|
||||||
|
'test/{,*/}*.js',
|
||||||
|
]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Load local tasks.
|
|
||||||
grunt.loadTasks('tasks');
|
|
||||||
|
|
||||||
// Default task.
|
// Default task.
|
||||||
grunt.registerTask('default', 'lint test');
|
grunt.registerTask('default', [
|
||||||
|
'jshint',
|
||||||
|
'nodeunit:all'
|
||||||
|
]);
|
||||||
|
|
||||||
};
|
};
|
66
README.md
66
README.md
|
@ -5,17 +5,20 @@ Convertion to PDF takes care of `@media print` CSS rules and preserves links to
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
### Setting up wkhtmltopdf
|
### Setting up wkhtmltopdf
|
||||||
|
|
||||||
Download and install wkhtmltopdf from the [project page][wkhtmltopdf_dl].
|
Download and install wkhtmltopdf from the [project page][wkhtmltopdf_dl].
|
||||||
|
|
||||||
Make sure `wkhtmltopdf` is accessible from your `PATH` (Try `wkhtmltopdf -V` in your Terminal).
|
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:
|
I'm on Mac 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
|
ln -s /usr/local/bin/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf /usr/local/bin/wkhtmltopdf
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can also install it via homebrew: `$ brew install wkhtmltopdf`.
|
||||||
|
|
||||||
|
|
||||||
### Use it with grunt
|
### Use it with grunt
|
||||||
|
|
||||||
Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-wkhtmltopdf`
|
Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-wkhtmltopdf`
|
||||||
|
@ -26,11 +29,6 @@ Then add this line to your project's `grunt.js` gruntfile:
|
||||||
grunt.loadNpmTasks('grunt-wkhtmltopdf');
|
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
|
## Documentation
|
||||||
Simply add task definition in your gruntfile. See the folllowing example:
|
Simply add task definition in your gruntfile. See the folllowing example:
|
||||||
|
|
||||||
|
@ -51,15 +49,61 @@ Simply add task definition in your gruntfile. See the folllowing example:
|
||||||
|
|
||||||
Run `grunt wkhtmltopdf` to execute all the targets or `grunt wkhtmltopdf:targetname` to execute a specific target. Every `html` file defined by the `src` parameter will be turned into a PDF and saved to `dest` folder.
|
Run `grunt wkhtmltopdf` to execute all the targets or `grunt wkhtmltopdf:targetname` to execute a specific target. Every `html` file defined by the `src` parameter will be turned into a PDF and saved to `dest` folder.
|
||||||
|
|
||||||
|
### Sending arguments to `wkhtml2pdf`
|
||||||
|
|
||||||
|
Arguments sent via the `args` key in the task definition will be forwarded to `wkhtml2pdf` as follow :
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
//...
|
||||||
|
wkhtmltopdf: {
|
||||||
|
prod: {
|
||||||
|
src: 'path/to/some/html/file/*.html',
|
||||||
|
dest: 'pdf/output/',
|
||||||
|
args: [
|
||||||
|
'--dpi', '96',
|
||||||
|
'--print-media-type',
|
||||||
|
'--grayscale'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
//...
|
||||||
|
```
|
||||||
|
|
||||||
|
Defining custom arguments will override default ones:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// grunt-wkhtml2pdf default arguments
|
||||||
|
var args = [
|
||||||
|
'--dpi', '96',
|
||||||
|
'--print-media-type'
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
See [wkhtml2pdf documentation][wkhtmltopdf_doc] for available options
|
||||||
|
|
||||||
## Contributing
|
## 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].
|
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
|
## Release History
|
||||||
|
|
||||||
- *v0.1.0*: First Release
|
- *v0.5.0*:
|
||||||
- *v0.2.0*: Fixed compatibility with grunt 0.4
|
- Merged [#12](https://github.com/dharFr/grunt-wkhtmltopdf/pull/12) (Fix [#11](https://github.com/dharFr/grunt-wkhtmltopdf/issues/11)) : Added async support
|
||||||
|
- Merged [#9](https://github.com/dharFr/grunt-wkhtmltopdf/pull/9) : Added ability to override the arguments
|
||||||
|
- *v0.4.0*:
|
||||||
|
- Update project config files to work with Grunt 0.4
|
||||||
|
- Update wkthmltopdf task to use Grunt 0.4 multi-task API
|
||||||
|
- Improve predictability of the destination for created PDFs
|
||||||
- *v0.3.0*: grunt-wkhtmltopdf is now a multi-task
|
- *v0.3.0*: grunt-wkhtmltopdf is now a multi-task
|
||||||
|
- *v0.2.0*: Fixed compatibility with grunt 0.4
|
||||||
|
- *v0.1.0*: First Release
|
||||||
|
|
||||||
## License
|
## License
|
||||||
Copyright (c) 2012 Olivier Audard
|
Copyright (c) 2012 Olivier Audard
|
||||||
Licensed under the MIT license.
|
Licensed under the MIT license.
|
||||||
|
|
||||||
|
|
||||||
|
[wkhtmltopdf]: http://wkhtmltopdf.org/
|
||||||
|
[wkhtmltopdf_dl]: http://wkhtmltopdf.org/downloads.html
|
||||||
|
[wkhtmltopdf_doc]: http://wkhtmltopdf.org/docs.html
|
||||||
|
[grunt]: https://github.com/gruntjs/grunt
|
||||||
|
[getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
|
||||||
|
|
11
package.json
11
package.json
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "grunt-wkhtmltopdf",
|
"name": "grunt-wkhtmltopdf",
|
||||||
"description": "Grunt Task that uses wkhtmltopdf to convert HTML files to PDF",
|
"description": "Grunt Task that uses wkhtmltopdf to convert HTML files to PDF",
|
||||||
"version": "0.3.0",
|
"version": "0.4.0",
|
||||||
"homepage": "http://www.dhar.fr/grunt-wkhtmltopdf/",
|
"homepage": "http://www.dhar.fr/grunt-wkhtmltopdf/",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Olivier Audard",
|
"name": "Olivier Audard",
|
||||||
|
@ -30,10 +30,13 @@
|
||||||
"test": "grunt test"
|
"test": "grunt test"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"grunt": "~0.3.9"
|
"grunt": "~0.4.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"grunt": "~0.3.9"
|
"grunt": "~0.4.1",
|
||||||
|
"grunt-contrib-jshint": "^0.10.0",
|
||||||
|
"grunt-contrib-nodeunit": "^0.4.0",
|
||||||
|
"load-grunt-tasks": "^0.4.0"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"gruntplugin",
|
"gruntplugin",
|
||||||
|
@ -41,4 +44,4 @@
|
||||||
"convert",
|
"convert",
|
||||||
"html"
|
"html"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,33 +18,73 @@ module.exports = function(grunt) {
|
||||||
|
|
||||||
grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() {
|
grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() {
|
||||||
|
|
||||||
var htmlFiles = grunt.file.expandFiles(this.file.src),
|
// using grunt async support so we can be sure task doesn't
|
||||||
dest = (this.file.dest && this.file.dest !== '') ? this.file.dest + '/' : '';
|
// end before PDF processing does: http://gruntjs.com/api/inside-tasks
|
||||||
|
var done = this.async(),
|
||||||
|
files = this.files;
|
||||||
|
|
||||||
grunt.log.writeln("pdf output is: " + dest);
|
// iterate through each file grouping
|
||||||
|
files.forEach(function(file, filesIndex) {
|
||||||
|
|
||||||
htmlFiles.forEach(function(srcpath) {
|
var pathlib = require('path');
|
||||||
var dir = dest + srcpath.replace(/.*\/([^\/]+)\/[^\/]+\.html/, '$1');
|
// 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);
|
||||||
|
|
||||||
// Create dest folder as wkhtmltopdf won't generate output if it doesn't exist
|
file.src.forEach(function(src, srcIndex) {
|
||||||
grunt.file.mkdir(dir);
|
|
||||||
var destpath = dir + '/' +
|
|
||||||
srcpath.replace(/.*\/([^\/]+)\.html/, '$1.pdf');
|
|
||||||
|
|
||||||
// Launch PhantomJS.
|
var dest = file.dest;
|
||||||
helper.convert({
|
// wkhtmltopdf seems to require that the destination be a file
|
||||||
code: 90,
|
// location, not a directory, so if the given destination is a
|
||||||
args: [
|
// directory then append the name of the source file but with the
|
||||||
'--dpi', '96', // workarround to wkhtmltopdf letter-spacing bug (see http://code.google.com/p/wkhtmltopdf/issues/detail?id=72)
|
// extension changed to .pdf
|
||||||
'--print-media-type', // Use @print media type
|
if (grunt.file.isDir(dest)) {
|
||||||
srcpath,
|
var srcFileName = pathlib.basename(src);
|
||||||
destpath
|
var srcFileExtension = pathlib.extname(src);
|
||||||
],
|
var destFileName = srcFileName.replace(
|
||||||
done: function(err) {
|
new RegExp(srcFileExtension + "$"), // match only the end of the string
|
||||||
if (err) {
|
".pdf"
|
||||||
grunt.log('>>>', err);
|
);
|
||||||
}
|
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);
|
||||||
|
}
|
||||||
|
// if this is the last src of the last file, we are done.
|
||||||
|
if((filesIndex+1 >= files.length) && (srcIndex+1 >= file.src.length)) {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue