How to render certain HTML entities (such as arrows) to PDF?

Tags: pdfHtmlentitiesHTML to PDF

This question was originally asked on Mar 18, '15 by aggsol

When we discussed fonts in chapter 6, we mentioned that Symbol and ZapfDingbats don't play well with HTML, and that other fonts are better suited when you need special symbols. When we discussed internationalization, we noticed that we can render Greek characters without the need for a font as Symbol. The C07E12_SpecialCharacters example shows that we don't need ZapfDingbats to render arrows, smileys, or any other special symbol. Other fonts such as FreeSans and FreeSerif are used instead.

Special characters in a PDF
Special characters in a PDF

The HTML code uses HTML entities to represent these special symbols:

public static final String HTML = "<html><head></head>" +
    "<body style=\"font-size:12.0pt; font-family:Arial\">" +
    "<p>Special symbols: " +
    "&larr;  &darr; &harr; &uarr; &rarr; &euro; &copy; &#9786;</p>" +
    "</body></html>";

The pdfHTML add-on is perfectly capable of parsing such entities. No special settings are needed in the createPdf() method:

public void createPdf(String html, String dest) throws IOException {
    HtmlConverter.convertToPdf(html, new FileOutputStream(dest));
}

If your application runs on a Windows machine, you can also use system fonts such as symbol.ttf and several WebDings and WingDings fonts if you need a symbol or special character.