Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When calling envelopesApi.getDocument in Docusign Node SDK, in what format is the data returned? How do I write it to a file?

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?

like image 541
Martin Pain Avatar asked Oct 24 '25 17:10

Martin Pain


1 Answers

See the api recipe to download a document here

Here is the sample code to download a document.

  envelopesApi.getDocument(accountId, envelopeId, documentId, function (error, document, response) {
    if (error) {
      console.log('Error: ' + error);
      return;
    }

    if (document) {
      try {
        var fs = require('fs');
        var path = require('path');
        // download the document pdf
        var filename = accountId + '_' + envelopeId + '_' + documentId + '.pdf';
        var tempFile = path.resolve(__dirname, filename);
        fs.writeFile(tempFile, new Buffer(document, 'binary'), function (err) {
          if (err) console.log('Error: ' + err);
        });
        console.log('Document ' + documentId + ' from envelope ' + envelopeId + ' has been downloaded to ' + tempFile);
      } catch (ex) {
        console.log('Exception: ' + ex);
      }
    }
  });
like image 97
Praveen Reddy Avatar answered Oct 26 '25 08:10

Praveen Reddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!