I am new to VertX and I want to read a pdf using the "GET" method. I know that buffer will be used. But there are no resources on the internet on how to do that.
Asked
Active
Viewed 313 times
0
-
Where is this PDF file stored at first? What have you tried so far? – tmarwen Oct 09 '21 at 15:18
-
The pdf file is in a couchbase DB. I am using webclient to get the pdf file. So I have to get the pdf and send it as a http response. – Utkal keshari Sahu Oct 10 '21 at 08:28
1 Answers
0
Omitting the details of how you would get the file from your data store (couchbase DB), it is fair to assume the data is read correctly into a byte[]
.
Once the data is read, you can feed it to an io.vertx.core.buffer.Buffer
that can be used to shuffle data to the HttpServerResponse
as follows:
public void sendPDFFile(byte[] fileBytes, HttpServerResponse response) {
Buffer buffer = Buffer.buffer(fileBytes);
response.putHeader("Content-Type", "application/pdf")
.putHeader("Content-Length", String.valueOf(buffer.length()))
.setStatusCode(200)
.end(buffer);
}

tmarwen
- 15,750
- 5
- 43
- 62
-
What is the purpose of storing it in byte[] array and then using Buffer.buffer() ? – Utkal keshari Sahu Oct 11 '21 at 01:56
-
Because `io.vertx.core.buffer.Buffer` is the internal data wrapper used by *Vert.x* to move data around and to achieve common data access interface. Additionally, using the specific `io.vertx.core.buffer.Buffer` type allows to pipe and stream data as needed instead of sending batched data. – tmarwen Oct 11 '21 at 10:25
-