I'm using a custom truetype font in a pdf generated by flying saucer xhtmlrenderer.
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("myfont.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
renderer.setDocument(XMLResource.load(in).getDocument(), url);
renderer.layout();
renderer.createPDF(out);
and within the html being rendered, i have the following (for example)
<html>
<head>
<style type="text/css">
*{font-family:myfont;} /* <-- this works, trust me */
</style>
</head>
<body>
<p>some plain text<b>some bold text</b> <span style="font-weight:bold;">more bold</span></p>
</body>
</html>
but even with the <b>
and the font-weight:bold
I can't get the text to come out bold.
Now, i know this should work because i have a similar (legacy) project which uses the same font, and plain old itext (ie no xhtmlrenderer) and it does produce pdfs with bold text via:
myFont = BaseFont.createFont("myfont.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
Font boldFont = new Font(myFont);
boldFont.setStyle(Font.BOLD);
com.lowagie.text.Document document = ...;
document.add(new Paragraph("plain", myFont));
document.add(new Paragraph("bold", boldFont));
can anyone explain why i can't use bold with xhtmlrenderer, and maybe a way to overcome this issue?
thanks, p.
I'm no expert, but if your font doesn't have a bold variant, then Flying Saucer may simply "not make it bold". When you programmatically make the font bold, you're using a font class that could be (behind the scenes) making the font "bolder" on its own using some kind of internal mechanism. A good example of this in the real world is Photoshop: if you have a font that doesn't have a bold variety, you can't make it bold, but Photoshop has a "simulated bold" option that will go in and thicken up the symbols without really using a "true" bold face.
A good means of diagnosing this for sure would be to switch which font you're using in Flying Saucer but keep the same document. If the new font is also not bold, then the problem is less topical. If it does become bold, then it is because there is no bold version of your font.
Hope this helps.