Skip to content

Instantly share code, notes, and snippets.

@surfbuds
Forked from NicolasZanotti/main.js
Created June 2, 2013 01:32
Show Gist options
  • Save surfbuds/5692296 to your computer and use it in GitHub Desktop.
Save surfbuds/5692296 to your computer and use it in GitHub Desktop.
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