Skip to content

Instantly share code, notes, and snippets.

@ziahamza
Created September 18, 2012 17:37
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ziahamza/3744524 to your computer and use it in GitHub Desktop.
Save ziahamza/3744524 to your computer and use it in GitHub Desktop.
trivial/simple nodejs document viewer, uses unoconv to convert docs and allows to preview pdf, docx and all libre office supported extentions right from the browser
var http = require('http'),
fs = require('fs'),
path = require('path'),
exec = require('child_process').exec;
function pipeDoc(inputPath, finalType, stream) {
var finalPath = path.dirname(inputPath)
+ "/" + path.basename(inputPath).split('.')[0] + ".html";
var convCommand = 'unoconv -f ' + finalType + " " + inputPath;
exec(convCommand,function(err, stdout, stderr) {
process.stdout.write(stdout);
fs.createReadStream(finalPath).pipe(stream);
});
}
http.createServer(function(rq, rs) {
rs.writeHead(200, {'Content-Type': 'text/html'});
var rqPath = rq.url;
if (path.extname(rqPath).length > 1) {
pipeDoc(rq.url, "html", rs);
}
else {
rs.end(
"<html><body><h1>Use the url http://localhost:8080/path_to_document to view in browser</h1></body></html>"
);
}
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment