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.

aCarella
  • 2,369
  • 11
  • 52
  • 85
  • 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. – JB Nizet Jul 03 '17 at 18:42
  • Why the odd storage format? – Kayaman Jul 03 '17 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. – aCarella Jul 03 '17 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. – aCarella Jul 03 '17 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. – Kayaman Jul 03 '17 at 18:55
  • @Kayaman - I'm not really sure how that helps me, though. I stated in my original post that I'm pretty new to this. Is there a specific solution you can provide for my issue? I'd gladly upvote and mark it as the correct answer if it works. – aCarella Jul 03 '17 at 19:03
  • @aCarella the code from your edit might result in something your browser accepts and displays as PDF but it surely is **not a valid PDF** - if it was one, your original data (in `String value`) would have to be a PDF damaged in a *very* peculiar manner. – mkl Jul 04 '17 at 04:23

1 Answers1

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)
A G
  • 61
  • 4
  • 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 Jul 03 '17 at 20:26
  • @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. – A G Jul 10 '17 at 17:29