Unable to convert base64 string to pdf

589 Views Asked by At
    Document document = new Document();

    String b64Image = medikmResourceRequest.getResourceImage();
    String fileName = resourceDir+"/"+medikmResourceRequest.getPhysicianId()+"/"+medikmResourceRequest.getName()+" "+ System.currentTimeMillis() +".pdf";

    PdfWriter.getInstance(document, new FileOutputStream(new File(fileName)));
    document.open();

    byte[] decoded = Base64.decodeBase64(b64Image.getBytes());

    document.add(Image.getInstance(decoded));
    document.close();

Above code is not working properly for large images, they are getting cropped but its working fine for small image.

Please suggest.

1

There are 1 best solutions below

1
On

The error is caused by medikmResourceRequest.getResourceImage() and b64Image.getBytes().

Your program should NOT transfer byte[](image data) via String. When JVM convert byte[] to String, if the byte data cannot mapping to char of String's charset. It will be replace to ?. The byte[] returned by b64Image.getByte() is differnet than original data and cause your image broken.