├── .gitignore ├── examples ├── hello.html ├── strings.js ├── convertFile.js ├── convertURL.js ├── convertString.js └── writeFile.js ├── package.json ├── index.js └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *.pdf 4 | -------------------------------------------------------------------------------- /examples/hello.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | Example page 6 | 7 | 8 |

Hello world

9 | 10 | 11 | -------------------------------------------------------------------------------- /examples/strings.js: -------------------------------------------------------------------------------- 1 | var http = require('http'), 2 | wkhtml = require('node-wkhtml'); 3 | 4 | 5 | http.createServer(function (request, response) { 6 | wkhtml 7 | .spawn('pdf', 'http://nodejs.org/api/all.html') 8 | .pipe(response); 9 | }).listen(8000); 10 | -------------------------------------------------------------------------------- /examples/convertFile.js: -------------------------------------------------------------------------------- 1 | var http = require('http'), 2 | wkhtml = require('../index.js'); 3 | 4 | 5 | http.createServer(function (request, response) { 6 | wkhtml 7 | .spawn('pdf', __dirname + '/hello.html') 8 | .stdout.pipe(response); 9 | }).listen(8000); 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { "name":"node-wkhtml", 2 | "version": "0.1.0alpha1", 3 | "description": "Wrapper for the khtmltopdf and khtmltoimg project.", 4 | "author": "Mike Hemesath ", 5 | "keywords": ["html", "pdf", "img", "wkhtml", "webkit", "converter", "node"], 6 | "main": "index", 7 | "engines": { "node": ">= 0.6.0" }, 8 | } 9 | -------------------------------------------------------------------------------- /examples/convertURL.js: -------------------------------------------------------------------------------- 1 | var http = require('http'), 2 | wkhtml = require('../index.js'); 3 | 4 | 5 | http.createServer(function (request, response) { 6 | console.log('Generating a PDF of http://nodejs.org/api/all.html, this may take a while.') 7 | wkhtml 8 | .spawn('pdf', 'http://nodejs.org/api/all.html') 9 | .stdout.pipe(response); 10 | }).listen(8000); 11 | -------------------------------------------------------------------------------- /examples/convertString.js: -------------------------------------------------------------------------------- 1 | var http = require('http'), 2 | wkhtml = require('../index.js'); 3 | 4 | 5 | http.createServer(function (request, response) { 6 | var pdf = wkhtml.spawn('pdf'); 7 | pdf.stdout.pipe(response); 8 | pdf.stdin.end('

Hello World

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

', 'utf-8'); 9 | }).listen(8000); 10 | -------------------------------------------------------------------------------- /examples/writeFile.js: -------------------------------------------------------------------------------- 1 | var wkhtml = require('../index.js'), 2 | createWriteStream = require('fs').createWriteStream; 3 | 4 | // From URL 5 | wkhtml 6 | .spawn('pdf', 'http://nodejs.org/api/all.html') 7 | .stdout.pipe(createWriteStream(__dirname + "/pdfFromURL.pdf")); 8 | 9 | // From File 10 | wkhtml 11 | .spawn('pdf', __dirname + '/hello.html') 12 | .stdout.pipe(createWriteStream(__dirname + "/pdfFromFile.pdf")); 13 | 14 | // From String 15 | var pdf = wkhtml.spawn('pdf'); 16 | pdf.stdout.pipe(createWriteStream(__dirname + "/pdfFromString.pdf")); 17 | pdf.stdin.end('

Hello World

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

', 'utf-8'); 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var spawn = require('child_process').spawn 2 | 3 | function buildArgs(options, defaults) { 4 | var args = []; 5 | for (name in options) { 6 | var value = options.hasOwnProperty(name) ? options[name] : defaults.hasOwnProperty(name) ? defaults[name] : null; 7 | if (value === true) { 8 | args.push('--' + name) 9 | } else if (value !== false) { 10 | args.push('--' + name + '=' + value); 11 | } 12 | } 13 | args.push('-') 14 | return args; 15 | } 16 | 17 | 18 | var defaults = module.exports.defaults = function(defaults) { 19 | 20 | var defaultEnv = defaults.env || process.env, 21 | pdf = defaults.pdf || {}, 22 | png = defaults.png || {}; 23 | 24 | return { 25 | spawn: function(format, input, opts, env) { 26 | var executable, 27 | defaults; 28 | 29 | if (format === "pdf") { 30 | executable = 'wkhtmltopdf' 31 | defaults = pdf; 32 | } else if (format == "png") { 33 | executable = 'wkhtmltoimg' 34 | defaults = png; 35 | } else { 36 | throw "Unsupported format. Use 1 of pdf or png" 37 | } 38 | 39 | var args = buildArgs(opts || {}, defaults); 40 | return spawn(executable, [input || '-'].concat(args), env || defaultEnv); 41 | } 42 | } 43 | } 44 | 45 | var wkhtml = defaults({}); 46 | module.exports.spawn = wkhtml.spawn; 47 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # node-wkhtml 2 | 3 | Wrapper for the **wkhtmltopdf** and **wkhtmltoimage** shell utilities. Converts html to pdf or image format using the webkit rendering engine, and qt. 4 | 5 | See: https://github.com/antialize/wkhtmltopdf 6 | 7 | ## Usage 8 | Generate a PDF of node doc and pipe to response. 9 | 10 | var wkhtml = require('node-wkhtml'); 11 | wkhtml 12 | .spawn('pdf', 'http://nodejs.org/api/all.html') 13 | .stdout.pipe(response); 14 | 15 | Generate a PDF of node doc and write it to the file system. 16 | 17 | var wkhtml = require('../index.js'), 18 | createWriteStream = require('fs').createWriteStream; 19 | 20 | wkhtml 21 | .spawn('pdf', 'http://nodejs.org/api/all.html') 22 | .stdout.pipe(createWriteStream('node_doc.pdf')); 23 | 24 | 25 | Generate a PDF from a string and pipe it to the response 26 | 27 | var wkhtml = require('node-wkhtml') 28 | 29 | var pdf = wkhtml.spawn('pdf'); 30 | pdf.stdout.pipe(response); 31 | pdf.stdin.end('

Hello World

'); 32 | 33 | See http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html. 34 | 35 | 36 | ## wkhtmltopdf & wkhtmltoimage Installation 37 | 38 | Download the appropriate utility from http://code.google.com/p/wkhtmltopdf/downloads/list. Compilation instructions can be found here: http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html 39 | 40 | **The pdf and image features are in separate utilities**. You will need to download the utility for the feature(s) you plan on using. 41 | 42 | ## node-wkhtml Installation 43 | 44 | npm install node-wkhtml 45 | 46 | ## Examples 47 | See the /examples directory. 48 | 49 | --------------------------------------------------------------------------------