Created
January 10, 2012 16:41
-
-
Save NicolasZanotti/1589937 to your computer and use it in GitHub Desktop.
Render HTML content to a PDF file with wkhtmltopdf and Node.js
This file contains 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
var http = require('http'); | |
var sys = require('sys'); | |
var exec = require('child_process').exec; | |
var util = require('util'); | |
var fs = require('fs'); | |
http.createServer(function(request, response) { | |
var dummyContent = '<!doctype html><html><head><title>Test</title><meta charset="utf-8"></head><body><p>Hello world!</p></body></html>'; | |
var htmlFileName = "page.html", pdfFileName = "page.pdf"; | |
// Save to HTML file | |
fs.writeFile(htmlFileName, dummyContent, function(err) { | |
if(err) { throw err; } | |
util.log("file saved to site.html"); | |
}); | |
// Convert HTML to PDF with wkhtmltopdf (http://code.google.com/p/wkhtmltopdf/) | |
var child = exec("wkhtmltopdf " + htmlFileName + " " + pdfFileName, function(err, stdout, stderr) { | |
if(err) { throw err; } | |
util.log(stderr); | |
}); | |
response.writeHead(200, {'Content-Type' : 'text/plain'}); | |
response.end('Rendered to ' + htmlFileName + ' and ' + pdfFileName + '\n'); | |
}).listen(8124); | |
console.log('Server running at http://127.0.0.1:8124/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i am using ubantu,
how to save pdf in a folder,in a sever as well as on client side,
here the log output
Server running at http://127.0.0.1:8124/
err Error: Command failed: /bin/sh -c wkhtmltopdf page.html page.pdf
/bin/sh: wkhtmltopdf: not found
28 Apr 20:01:40 - /bin/sh: wkhtmltopdf: not found
28 Apr 20:01:40 - file saved to site.html
err Error: Command failed: /bin/sh -c wkhtmltopdf page.html page.pdf
/bin/sh: wkhtmltopdf: not found
28 Apr 20:01:40 - /bin/sh: wkhtmltopdf: not found
28 Apr 20:01:40 - file saved to site.html