0

I have a PDF saved as a base 64 CLOB in a database.

As a functional test, I'm just trying to get it to display in my browser. I made a new endpoint in my controller and just put the base64 String into the controller, without even getting the PDF from the database, that looks like this:

@RequestMapping(value = "/output.pdf", method = RequestMethod.GET, produces = "application/pdf")
public void makePDF(HttpServletResponse response) throws Exception {
    String value = "R04jArrrw45jNH6bV02="; //<--This is longer, but I shortened it for this question
    byte[] imageByte = value.getBytes();        
    response.setContentType("application/pdf");
    response.setContentLength(imageBytes.length);
    response.getOutputStream().write(imageBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Whenever I hit the endpoint, I get a Failed to load PDF document message. I can't figure out why.

I'm pretty new to this, so I'm having trouble figuring out what my next steps are. How do I get the PDF to display in the web browser?

EDIT

I was able to get this working, by using modifying my method to the following:

@RequestMapping(value = "/output.pdf", method = RequestMethod.GET, produces = "application/pdf")
public void makePDF(HttpServletResponse response) throws Exception {
    try {
      String value = "R04jArrrw45jNH6bV02="; //<--This is longer, but I shortened it for this question
      byte[] image = Base64.decodeBase64(value.getBytes());

      Document document = new Document();
      document.setPageSize(PageSize.LETTER);
      PdfWriter.getInstance(document, response.getOutputStream());

      Image labelImage = Image.getInstance(image);
      labelImage.setAlignment(Image.TOP);
      labelImage.scalePercent(new Float("35"));

      document.open();
      document.add(labelImage);

      response.setContentType("application/pdf");
      response.setContentLength(imageBytes.length);
      response.getOutputStream().write(image);

      document.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
}

Trying to understand exactly what I'm doing here, and why it worked. Obviously has something to do with Base64 decoding, and using the Document object.

7
  • 1
    Because you're not sending the PDF bytes, but the base-64-encoded PDF bytes. You need to base-64 decode the string, and send the result. Commented Jul 3, 2017 at 18:42
  • Why the odd storage format? Commented Jul 3, 2017 at 18:46
  • @JBNizet - I added decoded it with Base64, and it took away the error message, but doesn't display the image still. But at least I am going in the correct direction. Thanks. Commented Jul 3, 2017 at 18:50
  • @Kayaman - This is a replacement for storing these images as files on a server. We are going file-less, and just storing these images as CLOBs in our DB. Commented Jul 3, 2017 at 18:51
  • Why would you store them as CLOBs and not BLOBs? That's the least sense-less way of storing them, and would have avoided this issue. Commented Jul 3, 2017 at 18:55

1 Answer 1

1

Stack Overflow post Blob vs Clob and why you should not store binary data in Clobs

PDF has a general text-based structure. However, PDF files can contain non-ASCII ("binary") data and should always be considered binary files, - sorry unable to find a source of truth link for this.

There is potential for a lossy encoding of data, and decoding encoded Base-64 in that case.

Decode using Base64 before you stream it out

Base64.decode(base64String, Base64.NO_WRAP)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I did add the Base64 decoding, along with using Document, and was able to get it to work. Updated my original post. Just trying to understand why it works.
@aCarella - You mention that the file is saved Base-64 encoded, the file has to be decoded to the original byte set. Base-64 is not the original encoding for PDF files.

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.