views:

169

answers:

2

Hi all,

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.

+1  A: 

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.

mattbasta
flying saucer uses itext to generate the pdf. i was assuming that flying saucer would use the same mechanism (ie font.setStyle) to apply the `bold` transform. clearly i am wrong, and i don't dispute your answer in fact i agree 100% however i am looking for a solution not an explanation. my next stop is going through the flying saucer source code, however i'd pay 50 rep for someone else to do this ;) thanks anyway.
pstanton
i found a ttf file which supplied the bold variant and adding this solved my problem.
pstanton
+1  A: 

If your legacy iText code can produce a bold font then it seems likely that the TTF file does actually contain a bold variant, even if Flying Saucer doesn't use it. You may be able to use the bold variant in Flying Saucer if you specify the exact name of the bold variant in your CSS stylesheet.

For example:

<style type="text/css"> 
* {font-family:myfont;}
b {font-family:"myfont bold";}
</style> 

You should be able to find the exact name of the font by opening a PDF generated by the legacy code in Adobe Reader. Go to File --> Properties... --> Fonts to see a list of all the fonts in the document. There will probably be a "myfont" entry as well as a "myfont bold" entry, or similar. Use the latter in your stylesheet.

gutch