如何使用iText获取以多种字体呈现的pdf

问题描述 投票:0回答:1

我正在使用iText将html和pdf解析为中英文字符。我正在使用

  // for pdf rendering
  compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.13.1'

  // for pdf rendering
  compile group: 'com.itextpdf.tool', name: 'xmlworker', version: '5.5.13.1'

我已经使汉字解析成为可能,并且没有依赖性的问题

  // for chinese font in pdf rendering
  compile group: 'com.itextpdf', name: 'itext-asian', version: '5.2.0'

和定制字体提供者

public class StSongProvider extends XMLWorkerFontProvider {

  private static final Logger LOG = LoggerFactory.getLogger(StSongProvider.class);

  public StSongProvider() {
    super(null, null);
  }

  @Override
  public Font getFont(final String fontName, String encoding, float size, final int style) {
    BaseFont bfChinese = null;
    try {
      bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    } catch (Exception e) {
      LOG.error("Not found STSong-Light,maybe com.itextpdf.itext-asian dependency problem");
    }
    return new Font(bfChinese, size, style);
  }
}

和pdf格式代码

  public static File html2Pdf(String html, String fileName) {
    try {
      String path = buildPath(fileName);
      // step 1
      Document document = new Document(PageSize.A4);
      document.setMargins(20, 20, 0, 0);
      // step 2
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
      // step 3
      document.open();
      // step 4
      InputStream cssInput = null;
      XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8)), cssInput, new StSongProvider());
      // step 5
      document.close();
      LOG.info("PDF file: {} rendering successfully", path);
      return new File(path);
    } catch (IOException ex) {
      // do something
    } catch (DocumentException ex) {
      // do something
    }
  }

但是如果没有正确的字体,结果pdf中的英文字符不会那么漂亮(所有字符都使用STSong-Light字体)。我想用STSong-Light用汉字渲染pdf,用iText最初支持的某些字体(例如Times-Roman)用英文字符渲染。

我发现此SO thread使使用FontSelector可以使用多种字体来创建文档。但是如何使其与pdf创建过程兼容?XMLWorkerHelper.getInstance().parseXHtmlAPI仅接受FontProvider作为参数。有任何想法吗?

java pdf fonts itext
1个回答
0
投票

解决方案是在自定义字体提供程序上执行某项操作,使其不只返回一种字体,而是根据html单元格font-family属性返回字体。

public class StSongProvider extends XMLWorkerFontProvider {

  private static final Logger LOG = LoggerFactory.getLogger(StSongProvider.class);

  public StSongProvider() {
    super(null, null);
  }

  @Override
  public Font getFont(final String fontName, String encoding, float size, final int style) {
    BaseFont font = null;
    try {
      if (StringUtils.equals(fontName, "STSong-Light")) {
        font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
      } else {
        font = BaseFont.createFont(FontFactory.TIMES_ROMAN, FontFactory.defaultEncoding, true);
      }
    } catch (Exception e) {
      // do something
    }
    return new Font(font, size, style);
  }

}

使用上述字体提供程序,并设置包含中文字符的html单元格的style="font-family:STSong-Light属性以对其进行格式设置,而其他英文字符将使用TIMES_ROMAN进行很好的格式设置。

© www.soinside.com 2019 - 2024. All rights reserved.