【问题标题】:How to put Webp images in a PDF with itext in java?java - 如何在Java中使用itext将Webp图像放入PDF?
【发布时间】: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

标签: java image pdf itext


【解决方案1】:

PDF 标准目前未将 WebP 列为 PDF 文档中图像编码的允许格式。换句话说,即使 iText(或任何其他 PDF 库,真的)支持在其 PDF 输出中直接包含 WebP 内容,那里的 PDF 查看器无论如何也无法显示它。

长话短说,您需要先将文件转换为其他格式(如 PNG 或 JPG)。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多