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.