2

I am trying to call the api endpoint getDocument via Node, and am expecting a Buffer to be returned, however, it is returning a string. Even when I pass in different values for the encoding optional parameter, the data returned is always the same.

When I tested the same endpoint in C#, a MemoryStream is returned which is expected.

My code is as follows:

const document = await envelopesApi.getDocument(accountId, envelopeId, '1')

Where 1 is the documentId (page 1).

The contents of document looks like %PDF-1.5\n%ûüýþ\n%Writing objects... and so on

I am then trying to save this to a file:

fs.writeFileSync('test.pdf', Buffer.from(documentContent))

With no success. How do I get the api response and save it to a file for viewing?

changus
  • 763
  • 2
  • 10
  • 25

2 Answers2

0

Yes, this is correct, you will have to use the correct mime type for PDF (in this case) in order to show this file in the browser.

You can find a node.JS code example that shows you how to do this.

But the most important part in your case would be this line:

mimetype = "application/pdf";
Inbar Gazit
  • 12,566
  • 1
  • 16
  • 23
  • Hi @Inbar, thanks for the quick response. I'm not quite sure I follow, can you give me an example of how you would pass the mimetype into writeFIleSync, for my example? Do I need to still do Buffer.from(documentContent)? It seems like from the code example that documentContent IS the fileBytes. I am not displaying in a browser, I am trying to save the file to disk. Thank you! – changus Oct 06 '21 at 22:16
  • see this - https://stackoverflow.com/questions/31040014/how-to-save-pdf-in-proper-encoding-via-nodejs – Inbar Gazit Oct 06 '21 at 23:01
  • 1
    fs.writeFileSync("testpdf", body,'binary'); – Inbar Gazit Oct 06 '21 at 23:01
  • 1
    that worked! I initially had tried fs.writeFileSync('test.pdf', Buffer.from(documentContent), 'binary'). The Buffer.from was what threw it off. Thank you @Inbar!! – changus Oct 06 '21 at 23:51
0

fs.writeFileSync("testpdf.pdf", body, "binary")

Lesley
  • 1
  • 1