2014-02-24 56 views
1

我正在使用飛碟lib生成pdf。但我有一些html實體的問題。飛碟 - 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>&#8592;</p> 

<p>&larr;</p> 

<p>&uarr;</p> 
<p>&#8593;</p> 
<p>&#8595;</p> 
<p></p> 

</body> 
</html> 

一切工作的實體旁邊的罰款。沒有什麼東西只能呈現空白點,應該用箭頭表示。 有沒有人有解決方案?

回答

1

問題是默認情況下,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中的FONT-FAMILY聲明:

body 
{ 
font-family: DejaVu Sans; 
} 
+0

非常感謝,這解決了我的問題。現在一切正常。 – user3333010

+0

很高興幫助。不要猶豫,接受答案(見[這裏](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235))。 – obourgain