简体   繁体   English

无法将base64字符串转换为pdf

[英]Unable to convert base64 string to pdf

    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. 请提出建议。

The error is caused by medikmResourceRequest.getResourceImage() and b64Image.getBytes() . 该错误是由medikmResourceRequest.getResourceImage()b64Image.getBytes()

Your program should NOT transfer byte[] (image data) via String. 您的程序不应通过String传输byte[] (图像数据)。 When JVM convert byte[] to String, if the byte data cannot mapping to char of String's charset. 当JVM将byte[]转换为String时,如果字节数据无法映射到String的字符集的char。 It will be replace to ? 它将替换为? . The byte[] returned by b64Image.getByte() is differnet than original data and cause your image broken. b64Image.getByte()返回的byte[]与原始数据不同,并导致图像损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM