我在飞碟 xhtmlrenderer 生成的 pdf 中使用自定义 truetype 字体。
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);
在呈现的 html 中,我有以下内容(例如)
<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>
但即使使用<b>
andfont-weight:bold
我也无法让文本变得粗体。
现在,我知道这应该可以工作,因为我有一个类似的(旧版)项目,它使用相同的字体和普通的旧 itext(即没有 xhtmlrenderer),它确实通过以下方式生成带有粗体文本的 pdf:
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));
谁能解释为什么我不能在 xhtmlrenderer 中使用粗体,也许是一种克服这个问题的方法?
谢谢,页。