itext7导出pdf,PdfFont字体文件在Linux下找不到

在这里插入图片描述

 ClassPathResource resource = new ClassPathResource("Fonts/simhei.ttf");
        PdfFont watermark = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            watermark = PdfFontFactory.createFont(resource.getFile().getAbsolutePath(), PdfEncodings.IDENTITY_H,false);
           PdfEncodings.IDENTITY_H,false);
            PdfWriter writer = new PdfWriter(baos);
            PdfDocument pdf = new PdfDocument(writer);
            Document document = new Document(pdf);
            //添加标题

本地正常到Linux下就找不到了
在网上查了好久,说法五花八门也没一个能行的
我的最后这样解决的:
直接以字节流传过去就没问题了

 private static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }




 public static ByteArrayOutputStream PaperTestDetailedToPdfUtil(DetailedToPdfVo checkContentInfo) {
        ClassPathResource resource = new ClassPathResource("Fonts/simhei.ttf");
        PdfFont watermark = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
//            watermark = PdfFontFactory.createFont(resource.getFile().getAbsolutePath(), PdfEncodings.IDENTITY_H,false);
            watermark = PdfFontFactory.createFont(ToPdfUtils.toByteArray(resource.getInputStream()) , PdfEncodings.IDENTITY_H,false);
            PdfWriter writer = new PdfWriter(baos);
            PdfDocument pdf = new PdfDocument(writer);
            Document document = new Document(pdf);
            ...
            ...
            }

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
iText 7是一款用于操作PDF的开源库,可以在多种不同的操作系统上使用,包括Linux。如果在使用iText 7在Linux导出PDF时出现了文字字体空白的问题,可能是由于以下原因导致的: 1. 缺少中文字体Linux系统默认情况下可能没有安装中文字体,这会导致在PDF中显示的文字出现空白。解决方法是在Linux系统上安装中文字体,例如通过执行命令 "apt-get install fonts-wqy-zenhei" 来安装文泉驿正黑字体。 2. 字体路径设置不正确:在使用iText 7时需要正确设置字体路径,确保能够到所需的中文字体文件。可以通过使用setFont方法来设置字体路径和字体名称,例如: Font font = FontFactory.getFont("/path/to/font.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 或者 fontProvider.addFont("/path/to/font.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 这样可以确保在导出PDF时能够正确地使用中文字体。 3. 字体嵌入设置不正确:可能在导出PDF时没有将中文字体正确地嵌入到PDF文件中。可以通过设置字体的嵌入模式来解决此问题,例如: font.setSubset(false); // 设置不进行字体子集化,将整个字体嵌入到PDF中。 这些可能是导致iText 7在Linux导出PDF中文字体空白的常见问题和解决方法。根据具体的情况,可能需要根据以上涉及到的步骤进行检查和调整,以确保在Linux上正确地显示和嵌入中文字体

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏路加

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值