使用ITextRenderer的pdf中没有中文单词

时间:2019-07-06 09:56:05

标签: java pdf itext

我正在使用ItextRenderer将html转换为pdf,它适用于西班牙语,法语单词,但是当我尝试使用中文单词时,它不包含PDF

  ITextRenderer renderer = new ITextRenderer();
  renderer.setDocumentFromString(html);
  renderer.layout();

我尝试添加字体,但仍然无法正常工作

   renderer.getFontResolver().addFont("C:\\Windows\\Fonts\\mingliu.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

   renderer.getFontResolver().addFont("C:\\Windows\\Fonts\\simsun.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

   renderer.getFontResolver().addFont("C:\\Windows\\Fonts\\batang.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

   renderer.setDocumentFromString(html);
   renderer.layout();
   OutputStream os = 
   Files.newOutputStream(Paths.get("chinese.pdf"));
   renderer.createPDF(os);

2 个答案:

答案 0 :(得分:0)

要管理中文单词,您将需要以下3个库:

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.11</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.5.11</version>
        </dependency>

汉字编码的必要部分是使用正确的亚洲字体解析html字符串,请参见以下核心代码:

    void createPDF(final String content, final String filename) {
        final Document document = new Document(PageSize.LETTER);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, Files.newOutputStream(Paths.get("target", filename)));
            document.open();
            InputStream input = new ByteArrayInputStream(content.getBytes());
            XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
            worker.parseXHtml(writer, document, input, Charset.forName("UTF-8"), new AsianFontProvider());
        } catch (DocumentException | IOException e) {
            log.error("Failed to generate PDF for {}", filename, e);
        } finally {
            document.close();
        }
    }
    class AsianFontProvider extends XMLWorkerFontProvider {
        @Override
        public Font getFont(final String fontName, final String encoding, final boolean embedded, final float size, final int style, final BaseColor color) {
            try {
                BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                Font font = new Font(bf, size, style, color);
                font.setColor(color);
                return font;
            } catch (DocumentException | IOException e) {
                log.error("Failed to create font for {} with encoding {}", fontName, encoding, e);
                throw new IllegalArgumentException("incorrect font creation, please check the font parameters", e);
            }
        }
    }

有关更多详细信息,您可以访问我的github,我为此问题创建了mini project

答案 1 :(得分:0)

要查看PDF中的任何其他语言,首先需要在系统中安装该字体包。 itext可以在生成PDF文件时轻松地将任何语言写入到您的PDF文件中,但是您的系统应该具有该特定的字体包来显示这些字体。