1

Following is the pdf buffer. I want convert buffer to pdf file.

data = {
    "file": {
        "type": "Buffer",
        "data": [
            102,
            24,
            62
        ]
    },
}
res.send( data );
4
  • Where does the buffer come from? If you are creating it, than you should write it into a file and send that back. Commented Mar 4, 2020 at 9:53
  • The buffer is of pdf file Commented Mar 4, 2020 at 9:57
  • Then why not just sending the file back? You can create a readstream and pipe that to the response Commented Mar 4, 2020 at 10:02
  • I was trying to send pdf file along with associated json data {status:200, fileBuffer: fileBuffer, "otherJsonData": {"a": 1, "b":2} } but it is not possible to use res.download and res.send together, so I converted pdf file to buffer and then sent it Commented Mar 4, 2020 at 10:18

1 Answer 1

2

You can use res.sendFile and use the header to send some data along.

// res.sendFile(path [, options] [, fn])

let options = {
  headers: {
      'TheDataYouWantToSend': Date.now() // for example a timestamp
  }
}

req.sendFile(absolutePathToFile, options)

I hope this helps

Sign up to request clarification or add additional context in comments.

3 Comments

how to set custom header? it is throwing an error { "statusCode": 425, "statusMsg": "Invalid character in header content [\"dataArr\"]" }
I am trying to pass json array in headers {"Content-Type": "application/json", "dataArr": [{"a": 1, "b": 2}] })
I think you can just use JSON.stringify on the array, and parse it on the frontend. Also you might not want to set the content type to json, since you are sending a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.