This is an archived forum post. The information may be outdated. Contact us if you have any questions.

How to return raw PDF data using the Node client library?

trav wrote on 2013-05-27:
I am using the Node client library as per the following instructions...

var pdf = require('pdfcrowd');

// create an API client instance
var client = new pdf.Pdfcrowd("username", "apikey");

// convert an HTML string and send the generated PDF in a HTTP response
client.convertHtml('<html>regular HTML code</html>', pdf.sendHttpResponse(response));

How do I return the raw pdf data? I don't want to automatically send it it to the client or save it. I tried the following but it does not work:

client.convertHtml('<html>regular HTML code</html>', function(pdfData) {...});
trav wrote on 2013-05-27:
Figured it out. Here is the code that does it, for any who might have the same problem:

client.convertHtml( bodyHTML, {
pdf: function( rstream ) {
rstream.on( "data", function( chunk ){
data.push( chunk );
});
rstream.on( "end", function() {
var buffer = new Buffer( data.reduce(function( prev, current ) {
return prev.concat( Array.prototype.slice.call( current ));
}, []));
callback( buffer.toString( "base64" ));
});
}
}, pdfConfig );