如何将 ByteBuffer 转换为 pdf [英] How to convert ByteBuffer to pdf

查看:60
本文介绍了如何将 ByteBuffer 转换为 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,pdf 报告仅在打印预览中打开,允许用户直接打印 pdf 文档.现在我想自动执行此操作以验证 pdf 内容.

In my application a pdf report only opens in print preview which allows user to directly print the pdf document. Now i want to automate this to verify the pdf content.

我通过base64中的api获取了pdf内容[确实拆分以仅获取数据],我尝试在解码后转换为字节数组,但它只打印垃圾字符.[字节数组到字符串]现在我已将此数据转换为 ByteBuffer 并希望将其写入 pdf.

I have got the pdf content through an api which is in base64 [did split to get only data], i tried converting to byte array after decoding but it only prints junk characters.[byte array to string] Now i have converted this data into ByteBuffer and want this to write in pdf.

ByteBuffer decodedBytes = new BASE64Decoder().decodeBufferToByteBuffer(
    new String(base64split2[1].substring(0, base64split2[1].length() - 1))
);

谁能告诉我如何将这个 ByteBuffer 的解码字节转换为 pdf.

Can someone tell me how do i convert this decodedBytes of ByteBuffer to pdf.

谢谢

推荐答案

  byte[] decodedBytes = new BASE64Decoder().decodeBuffer(str);
        InputStream targetStream = new ByteArrayInputStream(decodedBytes);
        PDDocument document = PDDocument.load(targetStream);
        document.save("C:/test.pdf");
        FileUtils.writeByteArrayToFile(new File("C:/test.pdf"), decodedBytes);

使用上面的代码转换为pdf.

Using above code to convert to pdf.

这篇关于如何将 ByteBuffer 转换为 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆