We are developing an app with Recurly integration and trying to use it PDF Invoice function.
The app is based on Node.js (Meteor platform).
It receives correct answer from Recurly with binary file:
But I can't correctly save it. I've tried two ways: print it on the client side in browser:
var file = window.URL.createObjectURL(new Blob([r.content], {type: "application/pdf"}));
var a = document.createElement("a");
a.href = file;
a.download = "invoicePDF";
document.body.appendChild(a);
a.click();
window.onfocus = function () {
document.body.removeChild(a)
}
And save it directly on server (just for test):
var fs = require('fs');
var wstream = fs.createWriteStream('C:/recurly.pdf');
wstream.write(result.content);
wstream.end();
But in both cases I've ended up with non-working pdf file. Acrobat, Foxit reader and Chrome cann't open this file — it's damaged.
Do you have any suggestions where I'm wrong? Maybe I need some content convertation before saving it or anything else?
Added
The result of this request I've sent to client and printed in console (image above).
try {
result = HTTP.call(
'GET',
'https://' + Meteor.settings.recurly.SUBDOMAIN + '.recurly.com/v2/invoices/' + invoiceId,
{
headers: {
Authorization: "Basic " + (new Buffer(Meteor.settings.recurly.API_KEY)).toString('base64'),
Accept: 'application/pdf'
}
}
);
} catch (err) {
result = e;
}