我正在使用 Flying Saucer 库生成 pdf。但我对某些 html 实体有疑问。
我已经在寻找解决方案,我在这个论坛和其他地方找到了很多提示,但问题仍然存在。
我试过这种方法:
http://sdtidbits.blogspot.com/2008/11/flying-saucer-xhtml-rendering-and-local.html
但没有成功
我的代码是这样的:
os = new FileOutputStream(pdf);
ITextRenderer renderer = new ITextRenderer();
ChainingReplacedElementFactory chainingReplacedElementFactory = new ChainingReplacedElementFactory();
chainingReplacedElementFactory.addReplacedElementFactory(new B64ImgReplacedElementFactory(renderer.getSharedContext()));
renderer.getSharedContext().setReplacedElementFactory(chainingReplacedElementFactory);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
其中 pdf 是要创建的新 pdf 的名称,url 是
File f = new File(url);
if (f.exists()) {
url = f.toURI().toURL().toString();
}
我的html文件是这样的
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<style type="text/css">
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, caption, tbody, tfoot, thead, tr, th
{
color: #444;
font-family: Arial;
font-size: 14px;
line-height: 25px;
border: none;
}
table, td {border: solid 1px #CCC;}
img {page-break-inside: avoid;}
</style>
<title></title>
</head>
<body>
<h1>Test</h1>
<p>Html etites to test</p>
<p>←</p>
<p>←</p>
<p>↑</p>
<p>↑</p>
<p>↓</p>
<p></p>
</body>
</html>
在这些实体旁边一切正常。在应该用箭头表示的地方,没有任何东西只呈现空白点。 有人对此有解决方案吗?
最佳答案
问题是 iText 默认使用的字体不支持您要打印的字符。
解决方案是嵌入另一种可以显示该字符的字体,例如DejaVu .
在java文件中,向渲染器声明字体:
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("font/DEJAVUSANS.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
并更改 HTML 中的字体系列声明:
body
{
font-family: DejaVu Sans;
}
关于java - Flying Saucer - 不呈现 html 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21984712/