Flying saucer (xhtmlrenderer) doesn't dare my font?

I use my own TrueType PDF font created using the xhtmlrenderer flying saucer.

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 inside the displayed html 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 <b> and font-weight:bold I can’t get the text in bold.

Now I know that this should work, because I have a similar (inherited) project that uses the same font, and a plain old itext (i.e. no xhtmlrenderer), and it creates pdf files 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 someone explain why i can't use bold with xhtmlrenderer and maybe a way to overcome this problem?

thanks p.

+4
source share
2 answers

I am not an expert, but if your font does not have a bold version, then Flying Saucer may simply "not make it bold." When you programmatically create a font in bold, you use a font class that can be (behind the scenes), making the font "bolder" on its own, using some kind of internal mechanism. Photoshop is a good example of this in the real world: 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 enter and thicken characters without using a “true” bold face .

A good way to diagnose this would be to switch the font that you use in the Flying Saucer, but save the same document. If the new font is also not shown in bold, then the problem is less relevant. If it becomes bold, it is because there is no bold version of the font.

Hope this helps.

+4
source

If your legacy iText code can create a bold font, then it seems likely that the TTF file does contain a bold version, even if Flying Saucer does not use it. You can use the bold variant in Flying Saucer if you specify the exact name of the bold variant in the CSS stylesheet.

For instance:

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

You can find the exact font name by opening the PDF file generated by the legacy code in Adobe Reader. Go to File → Properties ... → Fonts to view a list of all fonts in the document. There will probably be a record of "myfont", as well as a record of "myfont bold" or similar. Use the latter in your style sheet.

+2
source

Source: https://habr.com/ru/post/1316636/


All Articles