traviscooper / node-wkhtml

Convert html to PDF or PNG format using the webkit rendering engine, and qt.
104 stars 17 forks source link

write pdf to http response #6

Closed malonecj closed 12 years ago

malonecj commented 12 years ago

Hi. I'm attempting to write converted pdf from stdout to browser window with the following code. Should you be able to call response.write(stdout) as I'm doing below, or could I be missing something? At the moment, this opens a blank pdf or occasionally the browser crashes when trying to load it


var url = require("url");

function pdfExport(response, request) { var urlObj = url.parse(request.url, true); var fileName = urlObj.query["filename"]; var gotoUrl = urlObj.query["url"]; console.log("Request handler 'pdfExport' was called.");

var PDF = require("node-wkhtml").pdf();
new PDF({url:'www.google.com'}).convert(function(err, stdout) {
    if (err) {
        console.log(err);
    } else {
        response.writeHead(200, {"Content-Type": "application/pdf"});
        response.write(stdout);
        console.log("PDF Complete!");
    }
});

}

exports.pdfExport = pdfExport;

mhemesath commented 12 years ago

I'll take a look at this tonight. Sorry, this project has been a bit neglected. There are a number of improvements I need to make to it.

bencpeters commented 12 years ago

Did you ever discover anything wrt this issue? I'm playing with this plugin right now and having a similar problem when trying to serve up an image via http without saving it serverside first.


var http = require('http');
var Image = require("node-wkhtml").image();

http.createServer(function (request, response) {
    new Image({ url: "www.google.com" }).convert (function (err, stdout) {
      //var theImage = new Buffer (stdout, 'binary');
      response.writeHead(200, {'Content-Type' : 'image/png',
                               'Content-Length' : stdout.length
      });
      response.write (stdout, 'binary');
      response.end ();
      //write out an error, if there is one
      if (err)
        console.log (err);
    });
}).listen(8124);

So far doing a little digging I've confirmed that the command is being generated correctly (wkhtmltoimage www.google.com -), and when I pipe the output of that straight into a file (in the command line) it works fine. So there's some problem with how this function is handling that output...

I also tried the fix https://github.com/mhemesath/node-wkhtml/issues/4, but it didn't fix anything

mhemesath commented 12 years ago

Yeah, I'm using the wrong API. It needs to be switched to use spawn instead of exec. This will resolve the maxBuffer problems and will make it easier to pipe the output.

bencpeters commented 12 years ago

Cool! I'll fork it and play with and see if I can get it working...

mhemesath commented 12 years ago

Yeah, this whole project could be greatly simplified. I'm thinking of doing away with "convert" and "convertAs" and rather just making a convenience wrapper around spawn and returning the child process.

This will empower anyone using this module to do whatever they want with the streamed output (save as file, or pipe to response).

e.g.

img = Image({ url: "www.google.com" })

img.stdout.on('data', function (data) {
   console.log('stdout: ' + data);
});

img.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

img.on('exit', function (code) {
  console.log('child process exited with code ' + code);
});
bencpeters commented 12 years ago

Played with it a bit more and got it working with spawn. I'll clean it up a bit and post it up for you to look at tonight sometime probably.

mhemesath commented 12 years ago

Cool, if you get that done I'll merge it in. Otherwise I'll probably go ahead and make the changes I outlined above. My end-goal for this is to make it easy to plug into Express 3, so that any view can effortlessly be returned as a PDF or Image instead of HTML. Using spawn and returning the child process should make that much easier.

mhemesath commented 12 years ago

I pushed a major refactor and provided examples on piping the pdf directly to the response.