飞碟(xhtmlrenderer)没有加粗我的字体?
我在由飞碟 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>
,但即使使用 和
font-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 进行粗体显示,也许有一种方法可以克服这个问题?
谢谢,p。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是专家,但如果您的字体没有粗体变体,那么 Flying Saucer 可能只是“不使其粗体”。当您以编程方式将字体设置为粗体时,您使用的字体类可能(在幕后)使用某种内部机制自行将字体设置为“粗体”。现实世界中的一个很好的例子是 Photoshop:如果您的字体没有粗体,则无法将其设置为粗体,但 Photoshop 有一个“模拟粗体”选项,可以进入并加粗这些符号没有真正使用“真正的”粗体。
诊断此问题的一个好方法是切换您在 Flying Saucer 中使用的字体,但保留相同的文档。如果新字体也不是粗体,那么这个问题就不那么热门了。如果它确实变成粗体,那么这是因为您的字体没有粗体版本。
希望这有帮助。
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.
如果您的旧 iText 代码可以生成粗体字体,那么 TTF 文件实际上可能包含粗体变体,即使 Flying Saucer 不使用它。如果您在 CSS 样式表中指定粗体变体的确切名称,则可以在 Flying Saucer 中使用粗体变体。
例如:
您应该能够通过在 Adobe Reader 中打开由旧代码生成的 PDF 来找到字体的确切名称。转到文件 -->属性... -->字体可查看文档中所有字体的列表。可能会有一个“myfont”条目以及一个“myfont 粗体”条目或类似条目。在样式表中使用后者。
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:
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.