iTextRenderer 在依賴 iText 的基礎上,單獨實現了HTML渲染PDF,基本上能實現 CSS 2.1的整體性,並且完全符合 W3C 規范。
使用html和css定義樣式和呈現的內容。如下流程圖:

中文支持
首先需要添加中文字庫,也就是你的頁面中用到的所有字體:
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("C:/Windows/Fonts/simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
注意:頁面中字體不能使用中文,需要使用英文名稱,而且是大小寫敏感的!例如宋體的英文名稱是 SimSun(注意不是simsun!,首字母都是大寫的)
錯誤寫法:font-family:宋體 或者 font-family:simsun
正確寫法:font-family:SimSun 或者 font-family:SimHei
如果生成的pdf中文不顯示或者亂碼,請確認如下信息:
-
確保頁面中所有內容都指定了字體,最好能指定 body {font-family:....},以防止漏網之魚。
-
確保上述所有字體均通過addFont加入,字體名稱錯誤或者字體不存在會拋出異常,很方便,但是沒導入的字體不會有任何提示。
-
確保字體名稱正確,不使用中文,大小寫正確。
-
確保html標簽都正確,簡單的方法是所有內容都去掉,隨便寫幾個中文看看能否正常生成,如果可以,在認真檢查html標簽,否則再次檢查上述幾條。
還有就是中文換行的問題了,帶有中文而且文字較多存在換行情況時,需要給table加入樣式:
table-layout:fixed,然后表格中的td使用%還指定td的寬度。
加密及權限
加密方法較為簡單:
ITextRenderer renderer = new ITextRenderer();
renderer.setPDFEncryption(getEncryption());
private PDFEncryption getEncryption()
{
PDFEncryption encrypt = new PDFEncryption(new String("a").getBytes(), new String("b").getBytes(), PdfWriter.ALLOW_SCREENREADERS);
return encrypt;
}
需要引入jar包!bcprov-jdk16-145.jar,百度一下很多的。
兩個參數:兩個都是密碼,不同的是第一個密碼是瀏覽密碼,輸入該密碼打開pdf后根據設置的權限進行控制,第二個密碼屬於所有者密碼,使用該密碼打開pdf權限不受控制。
多頁面生成pdf
其實很簡單,第一個頁面不變,從第二個起:
for(int i = 1; i < inputFile.length; i++)
{
renderer.setDocument(new File(root, inputFile[i]));
renderer.layout();
renderer.writeNextDocument();
}
renderer.finishPDF();
標簽
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="****.css" rel="stylesheet" type="text/css" />
<bookmarks>
<bookmark name="a" href="#a" />
<bookmark name="b" href="#b" />
</bookmarks>
</head>
注意,如果你是將多個頁面生成到一個pdf中,那么只要在最后一個頁面中加入bookmark就可以了!否則會重復。
頁面生成橫向的pdf
在html的style里面加入pdf能識別的樣式,@page{}這個就是與其他樣式區別開來的標志,例如這里面寫@page{size:297mm 210mm;}這個就表示紙張的寬是297毫米,高是210毫米,這樣打印出來的效果就跟橫着的A4紙一樣了,一般放在style第一行。
頁面其他特性
所有style存放在css文件中(如果只是某個特性可以直接寫在html),使用link元素關聯,media="print"必須被設置。header和footer兩個div也是重要的,footer有兩個特殊的元素:pagenumber 、 pagecount,設置pdf頁數時被使用。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Alice's Adventures in Wonderland -- Chapter I</title>
<link rel="stylesheet" type="text/css" href="alice.css" media="print"/>
</head>
<body>
<div id="header" style="">Alice's Adventures in Wonderland</div>
<div id="footer" style=""> Page <span id="pagenumber"/> of <span id="pagecount"/> </div>
<h1>CHAPTER I</h1>
<h2>Down the Rabbit-Hole</h2>
<p class="dropcap-holder">
<div class="dropcap">A</div>
lice was beginning to get very tired of sitting by her sister
on the bank, and of having nothing to do: once or twice she had
peeped into the book her sister was reading, but it had no pictures
or conversations in it, `and what is the use of a book,' thought
Alice `without pictures or conversation?'
</p>
<p>So she was considering in her own mind (as well as she could,
for the hot day made her feel very sleepy and stupid), whether the
pleasure of making a daisy-chain would be worth the trouble of
getting up and picking the daisies, when suddenly a White Rabbit
with pink eyes ran close by her. </p>
<p class="figure">
<img src="alice2.gif" width="200px" height="300px"/>
<br/>
<b>White Rabbit checking watch</b>
</p>
... the rest of the chapter
@page {
size: 4.18in 6.88in;
margin: 0.25in;
-fs-flow-top: "header";
-fs-flow-bottom: "footer";
-fs-flow-left: "left";
-fs-flow-right: "right";
border: thin solid black;
padding: 1em;
}
#header {
font: bold serif;
position: absolute; top: 0; left: 0;
-fs-move-to-flow: "header";
}
#footer {
font-size: 90%; font-style: italic;
position: absolute; top: 0; left: 0;
-fs-move-to-flow: "footer";
}
#pagenumber:before {
content: counter(page);
}
#pagecount:before {content: counter(pages);
}
