vert.xvertx-verticlevertx-httpclientvertx-eventbusvert.x-webclient

How to read a pdf file in Vertx?


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.


Solution

  • 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);
    }