0

After requesting a document via the DocuSign api and writing it to the file system it appears blank after opening it. The docs say it returns a "PDF File" and the response body is returned as below.

const doc = 
    await rp.get(`${apiBaseUrl}/${BASE_URI_SUFFIX}/accounts/${accountId}/envelopes/${envelopeId}/documents/${document.documentId}`,
    {auth: { bearer: token }}
    );

fs.writeFile(document.name, new Buffer(doc, "binary"), function(err) {
        if (err) throw err;
        console.log('Saved!');
});

Response body:

{
"documents": [
    {
        "name": "Name of doc.docx",
        "content": "%PDF-1.5\n%\ufffd\ufffd\ufffd\ufffd\n%Writing objects...\n4 0 obj\n<<\n/Type /Page\n/Resources 5 0 R\n/Parent 3 0 R\n/MediaBox [0 0 612 792 ]\n/Contents [6 0 R 7 0 R 8 0 R 9 0 R 10 0 R ]\n/Group <<\n/Type /Group\n/S /Transparency\n/CS /DeviceRGB\n>>\n/Tabs /S\n/StructParents 0\n>>\nendobj\n5 0 obj\n<<\n/Font <<\n/F1 11 0 R\n/F2 12 0 R\n/F3 13 0 R\n>>\n/ExtGState <<\n/GS7 14 0 R\n/GS8 15 0 R\n>>\n/ProcSet [/PDF /Text ...
    }
]}

Screenshot of document: enter image description here

kybak
  • 820
  • 3
  • 13
  • 28

1 Answers1

1

The EnvelopeDocuments::get API method returns the PDF itself, not an object as you are showing.

For a working example of the method, see example 7, part of the Node.js set of examples.

Added

Also, the fs.writeFile call supports writing from a string source. I'd try:

fs.writeFile(document.name, doc, {encoding: "binary"},
    function(err) {
        if (err) throw err;
        console.log('Saved!');
    });

Incorrect encoding

Your question shows the pdf's content as a string with the control characters encoded as unicode strings:

"%PDF-1.5\n%\ufffd\ufffd\ufffd\ufffd\n%Writing objects...

but this is not correct. The beginning of a PDF file includes binary characters that are not displayable except in a hex editor. This is what you should see at the top of a PDF:

enter image description here

Note the 10th character. It is hex c4. In your string, the equivalent character has been encoded as \ufffd (it is ok that they aren't the same character, they are two different PDFs). The fact that the character has been encoded is your problem.

Solutions

  1. Convince the requests library and the fs.WriteFile methods to not encode the data. Or to decode it as needed. See this solution for the requests library.
  2. Or use the DocuSign Node.js SDK as I show in the example code referenced above.
Larry K
  • 47,808
  • 15
  • 87
  • 140
  • Apologies, the question was misleading. The response body above refers to the final combination of the two endpoints: /envelopes/documents and envelopes/documents/{id}. The ID and name are being pulled from the first request and the ID is used in the second request. The solution you have suggested results in the same blank document. – kybak Dec 03 '18 at 22:36
  • 1
    Sigh, I've had similar blank pdf problems. It is most likely due to a transformation being done ("helpfully") by Node.js or the request library. I suggest: 1. Assure yourself that what is being sent back to you is a good pdf. (make sure the response library isn't changing the file.) Or consider using the Node.js SDK from DocuSign as I show in my code example referenced above. 2. Then work on getting the [doc] byte string into a file. – Larry K Dec 04 '18 at 05:42
  • See addition to the body of my answer. – Larry K Dec 04 '18 at 05:48
  • Amazing, that worked! That was a HUGE help, thank you. – kybak Dec 04 '18 at 16:23