traviscooper / node-wkhtml

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

Generating image through URL then capturing stdout through convert() not working #4

Closed wallslide closed 12 years ago

wallslide commented 13 years ago

As seen at this URL:

http://nodejs.org/docs/v0.4.9/api/child_processes.html#child_process.exec

There are options that can be passed in to the exec() command to increase the maximum buffer size, and to force binary encoding. I was getting this error:

"exec error: Error: maxBuffer exceeded."

with this command:

var Image = require("node-wkhtml").image();
var url = "http://jqueryui.com/";
new Image({ url: url }).convert(function(error, stdout){
  console.log("done");
});

To solve the error I edited the PDF.prototype.convert function found in "node-wkhtml/lib/image.js" to pass in a larger maxBuffer argument like this:

PDF.prototype.convert = function(callback) {
    exec(   util.buildCommand(this),
            {maxBuffer: 10*1024*1024},
            callback);
  }

Although this got rid of the error message, when I saved the resultant data into a database and tried to read it out, it was not parsable as an image file (although there was definitely the correct amount of data saved). To solve this final problem, I told exec to use binary encoding like this:

PDF.prototype.convert = function(callback) {
    exec(   util.buildCommand(this),
            {encoding: 'binary', maxBuffer: 10*1024*1024},
            callback);
  }

I think it makes sense that the encoding should always be set to binary in this case. As for the maxBuffer, I'm not sure if setting a large number has any effect beyond the potential for an extremely large image to hurt the system. It might be good if this was an argument a user could pass in through the convert() function since they should have a better idea of their size requirements.

mhemesath commented 13 years ago

Doh, youre right.. the buffer size does need to be configurable. Feel free to fork and create a pull request and illl merge those changes in.

mhemesath commented 13 years ago

I think the ideal solution is to create js bindings to wkhtml, but this will work for now.

mhemesath commented 12 years ago

I pushed a major refactor that uses spawn. You can listen to the data event directly on the stream or just pipe it to the response or filesystem. Check out the examples and let me know if you have any questions. Thanks!