baba.kabira
baba.kabira

Reputation: 3171

iText monospaced fonts in html view with bold, italic from css style

I am using html to generate PDF document, since its an EMR record I have to use Monospaced fonts.

PDF is getting generated fine, but css style for bold and italics are getting ignored, as I am using single .otf file for font hence no bold and italics.

I was wondering how to enable the same. Below are the code snippets.

Font Factory:

public static class MyFontFactory implements FontProvider,Serializable {
        public Font getFont(String fontname,
                String encoding, boolean embedded, float size,
                int style, BaseColor color) {
                BaseFont bf3 = null;
            try {
                bf3 = BaseFont.createFont("Inconsolata.otf",BaseFont.CP1252, BaseFont.EMBEDDED);
            } catch (Exception e) {
                e.printStackTrace();
            } 

            return new Font(bf3, 6);
        }

        public boolean isRegistered(String fontname) {
            return false;
        }
    }

PDF Generation Code:

public void createPdf(Object object) throws Exception, DocumentException{
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(new File("test.pdf")));
        // step 3
        document.open();
       // create extra properties
        HashMap<String,Object> map = new HashMap<String, Object>();
        map.put(HTMLWorker.FONT_PROVIDER, new MyFontFactory());
        // step 4
        String snippet;
        // create the snippet
        snippet = createHtmlSnippet(object);
        Map<Object,Object> model = new HashMap<Object,Object>();
        model.put("object", object);

        StyleSheet css = new StyleSheet();
        Map<String, String> stylemap = new HashMap<String, String>();
        stylemap.put("font-style", "italic");
        stylemap.put("font-size", "small");
        stylemap.put("font-weight", "bold");
        css.loadStyle("header",(HashMap<String, String>) stylemap);
        css.loadStyle("strongClass", "text-decoration", "underline");
        List<Element> objects = HTMLWorker.parseToList(new StringReader(snippet), css, map);

       for (Element element : objects)
              document.add(element);
        // step 5
        document.close();
    }

In the above code css supplied does not produce any effect on output as I mentioned due to single font defined, if I want to have bold and italics how can that be achieved?

Really appreciate if anyone provides pointers or help regarding same.

Thanks.

Note: If I remove Monospaced fonts css gets applied.

Upvotes: 1

Views: 2049

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

You are confusing a font family with a font.

Inconsolata is a font family consisting of different fonts:

  • Inconsolata regular as defined in inconsolata.ttf
  • Inconsolata bold as defined in inconsolata-Bold.ttf

See http://code.google.com/p/googlefontdirectory/source/browse/ofl/inconsolata/

I didn't know of any bold, italic or bold-italic version because I assumed "there is no bold or italic for Inconsolata." And if there is no font program for other styles, you shouldn't expect iText to support those styles (*).

Then I found a repository with a TTF for the bold font: http://code.google.com/p/googlefontdirectory/source/browse/ofl/inconsolata/

Searching StackOverflow I read the question about Inconsolata Italic in MacVim on StackOverflow; unfortunately these fonts can't be used in iText.

(*) When a font doesn't support bold or italic, iText can mimic these styles by changing the render mode and/or the skew. However, you'll have better results by choosing another monospaced font.

Upvotes: 1

Related Questions