Base64 PDF data

I have a server side web service that returns a PDF document as a BASE64 encoded string. When I decode that string in Vaadin and save the PDF document, the document is damaged. I have looked at both the original file that was created and the new file that resulted from the decode string and they are different. I suspect the problem is that the code that generates the encoded string is using “ISO8859-1” character set while the Vaadin is using a different character set.

How would I go about converting the string from the server side character set so that Vaadin can decode the string correctly?

Attached is a file that shows the Base64 data I am sending Vaadin and what Vaadin is seeing. The string values look the same to me.
17001.txt (434 KB)

Hi Freddy,

from doing a quick test run (tcl has a base64 package), I noticed several things:

  • there are some characters before the Base64 starts (I removed them)
  • I then base 64 decoded it and it returns an invalid PDF - but: one that seems to have correct start and end(i.e. PDF… and EOF) - maybe checking this anyways makes sense
  • as Base 64 only uses chars that are the same in every charset
  • some base64 implementations require a linebreak after 70-odd characters, and your file lacks that. This is where I’d look at first (maybe you can find out what the webservice uses and try the same)

Good luck

Best
Sebastian

Sebastian,
I just want to make sure I understand what you are saying correctly.

  1. Since Base64 only uses chars that are the same in charset, I do not need to convert from one charset to another. Is that correct?
  2. You mentioned that there were some charcters before the Base64. Are you talking about "java string = " and "abl string = "? If so, they were added by me to denote where each string came from. If not which characters did you remove?
  3. You mention that some base64 implementation require a linebreak every 70-odd characters. Do you know if that is the case with the com.google.gwt.user.server.Base64Utils? That is the utility that I am using to convert the string back into a binary value.

Thank you

Just thought I would give something a try. I copied the string I was getting from the web service into a on-line tool to do base64 encoding/decoding. That on-line tool decoded the string correctly. The problem has to be with the Base64Utility not handling my data correctly.

Found the problem, the “google.gwt.user.server.Base64Utils” has a bug in decoding strings. So I need another option.
Thank you

Got it work, change my library to org.apache.tomcat.util.codec.binary.Base64, and it worked.

Hi Freddy,

great! Although irrelevant: 1.) yes, base64 should be charset independent; 2.) yes, this was what I removed, so all is OK. and 3.) I don’t know about this specific one.

But as we have a solution…

Best
Sebastian

BTW java 8 has builtin base64 decoders java.util.Base64

If you dont / cant use java 8 you should probably look at the apache commons codec

Here is an interesting atricle regarding base64 performance of several implementations


http://java-performance.info/base64-encoding-and-decoding-performance/

I tried the apache.commons.codec first, and when I ran my code it complained about not being able to find the class. Being new to java and Vaadin, I started looking for another class that would work for me. That is when I found the gwt Base64Utils, which seemed to be working, but my file was corrupt. Once I found out that it had a bug, I noticed the Tomcat version, so I tried that and it worked for me. Do not want to go to 8 yet, as I am under a fairly tight deadline and changing version could cause other problems.