【发布时间】:2022-01-08 18:03:22
【问题描述】:
我有一个包含带有 Webp 扩展名的图像的 zip 文件,我已将它们加载到 byte[] 上的数组中。 当我尝试在 pdf 中添加该图像时,会抛出一个异常,提示 字节数组不是可识别的图像格式,itext 不支持 Webp 图像吗?
public byte[] generatePdfById(String uuid) {
try {
log.info("Generate document for id {} ", uuid);
PointDao ptDao = pointRepository.getById(uuid);
Document document = new Document();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfWriter.getInstance(document, outputStream);
List<byte[]> images = storedService.getBucketZipByKey(ptDao.getImageZip(), "images");
byte[] firstImage = images.get(0);
Image image = Image.getInstance(firstImage);
document.open();
document.add(image);
document.close();
byte[] bytes = outputStream.toByteArray();
return bytes;
} catch (Exception e) {
throw new PtRuntimeException(e.getMessage());
}
}
【问题讨论】:
-
您使用的是什么 PDF 库?我认为 PDF 本身不支持嵌入 WebP,因此需要转换文件。不幸的是,Java 中对 WebP 的支持还不是很好,但是有一些选择。 jDeli(商业)、TwelveMonkeys ImageIO*(开源,仅支持有损)。 *) 我是开发人员/维护人员。 ??????
-
我使用的是 itextpdf 版本 5.5.13.2