Having successfully logged in, and have got the list of documents from an envelope, I am trying to retrieve a signed document from DocuSign, using the DocuSign Node SDK, using this code:
envelopesApi.getDocument(accountId, envelopeId, documents[0].documentId, function(err, data, response) {
log.info({data: data.substring(0, 100)}, 'getDocument data');
log.info({response}, 'getDocument response');
fs.writeFile('/home/martin/downloaded.pdf', data, (err) => {
next(err);
});
});
The data
variable is a string. It is not base64 encoded. The first logging statement (using the Bunyan logging module) shows that the string starts with these characters:
%PDF-1.4
%ûüýþ
4 0 obj
<<
/Parent 3 0 R
/Resources 5 0 R
/MediaBox [0.00000 0.00000 595.00000 842.00
therefore I can see that it's not base64 encoded. It seems strange to me to be holding the contents of a pdf file in a string. I was expecting a Buffer
object.
When I open (in Chrome) the file that this code saved, it appears to be a valid pdf file (i.e. Chrome doesn't error saying the file is corrupt), and it has the right number of pages, but it is completely unreadable. There is no legible text at all on the page, indicating that something has got corrupted.
Looking at the EnvelopesApi.js and ApiClient.js files from the SDK I can see that it's requesting a PDF, and that there is code in the ApiClient specifically for handling PDFs - which appears to be reading from a readable stream & appending to a string.
I'm aware that there's an alternative to not use the NOde SDK, and just use the REST API directly (as per the examples in the official REST API Recipe: Getting an Envelope's Document List), but I'd like to use the SDK if possible.
Am I missing something that I should be doing with this data
parameter?