OJVM OJVM - 1 year ago 210
Java Question

create pdf from binary data in java

I'm getting this string from a web service.


It is supposed to be a pdf file, i tried this library pdfbox from apache, but it writes the content as a text inside the pdf. I've tried with ByteArrayInputStream but the pdf created is not valid, corrupted, this is some of the code i've wrote.

public void escribePdf(String texto, String rutaSalida) throws IOException{

byte[] biteToRead = texto.getBytes();
InputStream is = new ByteArrayInputStream(biteToRead );
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(rutaSalida))));
int c;
while((c = is.read()) != -1) {


Answer Source

That is Base64 encoded (most probably UTF-8) data, you must decode it before using; such as:

import sun.misc.BASE64Decoder;


BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(biteToRead);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download