Lebecca
Lebecca

Reputation: 2878

How to use iText get pdf rendered with multiple fonts

I'm using iText to parse html to pdf with both english and chinese characters. I'm using

  // 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'

I've already make it possible to get chinese characters parsing not a problem with dependency

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

and customized font provider

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);
  }
}

and pdf rendring code

  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
    }
  }

But the english characters in the result pdf is not that beautiful without a proper font (all characters are using STSong-Light font). I want to get pdf rendered with chinese characters using STSong-Light and english characters using some fonts that iText supported originally such as Times-Roman.

I found this SO thread makes building a document possible with multiple fonts using FontSelector. But how to make it compatible with pdf creation process? The XMLWorkerHelper.getInstance().parseXHtml api only accept a FontProvider as parameter. Any ideas about this?

Upvotes: 0

Views: 1628

Answers (1)

Lebecca
Lebecca

Reputation: 2878

The solution is do something on the customized font provider, make it not return only one font, but return font depends on the html cell font-family attribute.

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);
  }

}

With above font provider, and set the style="font-family:STSong-Light attribute of a html cell contains chinese characters to format them, and other english characters will be well formatted with TIMES_ROMAN;

Upvotes: 1

Related Questions