成都心情
BlogJava
::
首頁
:: ::
聯系
::
聚合
::
管理
::
98 隨筆 :: 2 文章 :: 501 評論 :: 1 Trackbacks
公告
本作品采用
知識共享署名-相同方式共享 2.5 中國大陸許可協議
進行許可。
(15)
給我留言
查看公開留言
查看私人留言
隨筆分類
(91)
Java EE 服務器端(13)
(rss)
Java EE 表現層及容器(12)
(rss)
Java ME(1)
(rss)
Java 基礎(10)
(rss)
MatLab(1)
(rss)
O/R Mapping(13)
(rss)
Versant db4o 中文項目(12)
(rss)
Web前端技術
(rss)
五花八門(8)
(rss)
大數據(3)
(rss)
工作流(10)
(rss)
數據庫(2)
(rss)
模式與策略(6)
(rss)
隨筆檔案
(99)
2016年7月 (2)
2016年6月 (4)
2016年5月 (3)
2016年4月 (2)
2010年7月 (1)
2010年6月 (2)
2010年5月 (3)
2010年3月 (1)
2010年1月 (1)
2009年10月 (1)
2009年9月 (1)
2009年7月 (1)
2009年6月 (1)
2009年3月 (1)
2009年2月 (1)
2008年12月 (2)
2008年9月 (1)
2008年8月 (1)
2008年7月 (1)
2008年6月 (1)
2008年4月 (1)
2008年3月 (1)
2008年1月 (1)
2007年12月 (2)
2007年10月 (1)
2007年9月 (1)
2007年8月 (1)
2007年6月 (2)
2007年5月 (1)
2007年4月 (1)
2007年2月 (1)
2007年1月 (1)
2006年12月 (1)
2006年11月 (1)
2006年10月 (1)
2006年9月 (1)
2006年8月 (1)
2006年7月 (1)
2006年6月 (1)
2006年5月 (1)
2006年4月 (1)
2006年3月 (1)
2006年2月 (1)
2006年1月 (1)
2005年12月 (1)
2005年11月 (1)
2005年10月 (1)
2005年9月 (2)
2005年8月 (37)
文章分類
(2)
我的收藏(2)
(rss)
友情鏈接
david.turing
(rss)
wyingquan的專欄
(rss)
信息安全思想
俺的豬窩~!@
喜馬拉雅的雪杉
(rss)
無聊人士
(rss)
竹十一
(rss)
老劉忙不忙
(rss)
邢紅瑞的blog
(rss)
積分與排名
積分 - 635665
排名 - 74
最新評論
1.?re: 精確截取字符串(轉載)
string=goodStr(string); 這個方法 是干什么用處的?
--JustPassoner
2.?re: 精確截取字符串(轉載)
@國家機器
六六,認識你是我等榮幸,酒瘋知己千杯燒...
--JustPassoner
3.?re: 使用Memory Analyzer tool(MAT)分析內存泄漏(二)[未登錄]
樓主可以去看看一篇jvm的連載 公眾號 ITmenghuo
--dddd
4.?re: 使用Memory Analyzer tool(MAT)分析內存泄漏(一)
小瑕疵。
圖片顯示不了。
--袁良錠
5.?re: Hadoop周刊—第 169 期
喲,又開始更新了
--救救劉書記
閱讀排行榜
1.?使用Memory Analyzer tool(MAT)分析內存泄漏(二)(121270)
2.?使用Memory Analyzer tool(MAT)分析內存泄漏(一)(75980)
3.?Java 中的位運算(31049)
4.?Ajax輪詢以及Comet模式—寫在Servlet 3.0發布之前(15655)
5.?RBAC 模型初探(13709)
評論排行榜
1.?德國申根商務簽證攻略(成都版)(36)
2.?OSWorkflow 探索(29)
3.?北漂找工作經歷(26)
4.?使用Memory Analyzer tool(MAT)分析內存泄漏(一)(23)
5.?開源面向對象數據庫 db4o 之旅: 初識 db4o“db4o 之旅(一)”(21)
利用 iText 實現 PDF 報表下載
很久沒更新 blog 了,工作和一些事情占用了大部分精力,實在是身不由己。今天終于有空整理一下最近用到的東西。
有個朋友的項目需要用到 PDF 報表下載,之前我只做過 Excel 的,相信再做一次 PDF 的下載一定很有趣吧。在網上找了一大圈,似乎 iText 比較符合我的要求,而且這個工具很早很早以前就有了,生命力很旺盛。進入 iText 的主頁(http://www.lowagie.com/iText/),發現作者很勤勞,最近2個月都有新版本發布。哪知道現在高興得太早了,一堆問題接踵而至。
下載倒是很簡單,一個
iText in a Web Application
正是我要找的,不過這個例子很簡單。通過 Google 之后,又發現要下載一個 CJK 的包(iTextAsian.jar)才能正確顯示中文,好吧我去找。很幸運的是在 iText by Example 里找到了這個 jar 的 link,興致勃勃的跑去下載,結果這是個無效鏈接,最后在 sourceForge 上才找到,不容易啊。解決了這些問題,想必能夠安穩的使用了吧,由于這個項目比較急,沒什么耐心一個個的翻閱 iText by Example,想找點捷徑,據說 iText 可以從 html 直接生成 PDF,竊喜!找了 apache common 的 httpclient,動態模擬 http 請求來抓 html,根據控制臺的 print,的確把 html 抓到了,然后開始轉換到 PDF,先解決了中文顯示問題,可是后面的問題解決不了了,html 的 table 和 div 這些,轉換到 PDF 都走樣了... ...
很不爽,看來還是只有老老實實的啃 iText by Example實在點。這次稍微耐心點,一點點的看,首先搞清楚了它的 Font 設置,然后是 Table 和 Cell 的關系,經過反復調試,有點效果了。把代碼貼出來,做個標記吧。以免以后又抓狂。
??1
?
package
?org.rosenjiang.servlet;
??2
?
??3
?
import
?java.awt.Color;
??4
?
import
?java.io.IOException;
??5
?
import
?java.util.HashMap;
??6
?
import
?java.util.List;
??7
?
import
?java.util.Map;
??8
?
??9
?
import
?javax.servlet.ServletException;
?10
?
import
?javax.servlet.http.HttpServlet;
?11
?
import
?javax.servlet.http.HttpServletRequest;
?12
?
import
?javax.servlet.http.HttpServletResponse;
?13
?
?14
?
import
?org.springframework.web.context.WebApplicationContext;
?15
?
import
?org.springframework.web.context.support.WebApplicationContextUtils;
?16
?
?17
?
import
?org.rosenjiang.service.UserService;
?18
?
import
?com.lowagie.text.Document;
?19
?
import
?com.lowagie.text.DocumentException;
?20
?
import
?com.lowagie.text.Font;
?21
?
import
?com.lowagie.text.Paragraph;
?22
?
import
?com.lowagie.text.pdf.BaseFont;
?23
?
import
?com.lowagie.text.pdf.PdfPCell;
?24
?
import
?com.lowagie.text.pdf.PdfPTable;
?25
?
import
?com.lowagie.text.pdf.PdfWriter;
?26
?
?27
?
/*
?28
?
?*?ReportServlet
?29
?
?*?@author?rosen?jiang
?30
?
?*?@since?2008-12
?31
?
??
*/
?
?32
?
public
?
class
?ReportServlet?
extends
?HttpServlet?{
?33
?
???????
?34
?
????
/**
?35
?
?????*?Return?a?PDF?document?for?download.
?36
?
?????*?
?37
?
?????
*/
?38
?
????
public
?
void
?doGet?(HttpServletRequest?request,?HttpServletResponse?response)
?39
?
????
throws
?IOException,?ServletException?{
?40
?
????????String?account_id?
=
?request.getParameter(
"
account_id
"
);
?41
?
????????String?search_date_from?
=
?request.getParameter(
"
search_date_from
"
);
?42
?
????????String?to?
=
?request.getParameter(
"
to
"
);
?43
?
????????WebApplicationContext?ctx?
=
?WebApplicationContextUtils.getWebApplicationContext(
this
.getServletContext());
?44
?
????????UserService?userService?
=
?(UserService)ctx.getBean(
"
userService
"
);
?45
?
????????List
<
Map
<
String,?Object
>>
?list?
=
?userService.getAccountActivity(account_id,?search_date_from,?to);
?46
?
????????
//
?create?PDF?document
?47
?
????????Document?document?
=
?
new
?Document();
?48
?
????????
try
?{
?49
?
????????????
//
set?response?info
?50
?
????????????response.setContentType(
"
application/x-msdownload;charset=UTF-8
"
);
?51
?
????????????response.setHeader(
"
Content-Disposition
"
,
"
attachment;filename=report.pdf
"
);
?52
?
????????????
//
open?output?stream
?53
?
????????????PdfWriter.getInstance(document,?response.getOutputStream());
?54
?
????????????
//
?open?PDF?document
?55
?
????????????document.open();
?56
?
????????????
//
?set?chinese?font
?57
?
????????????BaseFont?bfChinese?
=
?BaseFont.createFont(
"
STSong-Light
"
,?
"
UniGB-UCS2-H
"
,?BaseFont.NOT_EMBEDDED);??
?58
?
????????????Font?f2?
=
?
new
?Font(bfChinese,?
2
,?Font.NORMAL);
?59
?
????????????Font?f6?
=
?
new
?Font(bfChinese,?
6
,?Font.NORMAL);
?60
?
????????????Font?f8?
=
?
new
?Font(bfChinese,?
8
,?Font.NORMAL);
?61
?
????????????Font?f10?
=
?
new
?Font(bfChinese,?
10
,?Font.NORMAL);
?62
?
????????????Font?f12?
=
?
new
?Font(bfChinese,?
12
,?Font.BOLD);
?63
?
????????????
//
set?title
?64
?
????????????document.add(
new
?Paragraph(
"
金融報表
"
,?f12));?
?65
?
????????????
//
<br>
?66
?
????????????document.add(
new
?Paragraph(
"
?
"
,f6));?
?67
?
????????????
//
set?sub?title
?68
?
????????????document.add(
new
?Paragraph(
"
賬戶信息
"
,?f10));?
?69
?
????????????
//
<br>
?70
?
????????????document.add(
new
?Paragraph(
"
?
"
,?f2));
?71
?
????????????
//
process?business?data
?72
?
????????????
if
(list.size()
>
0
?
&&
?list.get(
0
).get(
"
bankbook_no
"
)
!=
null
){
?73
?
????????????????
float
?openBalance?
=
?
0
;
?74
?
????????????????
//
create?table?with?7?columns
?75
?
????????????????PdfPTable?table?
=
?
new
?PdfPTable(
7
);
?76
?
????????????????
//
100%?width
?77
?
????????????????table.setWidthPercentage(
100
);
?78
?
????????????????table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
?79
?
????????????????
//
create?cells
?80
?
????????????????PdfPCell?cell?
=
?
new
?PdfPCell();
?81
?
????????????????
//
set?color
?82
?
????????????????cell.setBackgroundColor(
new
?Color(
213
,?
141
,?
69
));
?83
?
????????????????cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
?84
?
????????????????
//
?85
?
????????????????cell.setPhrase(
new
?Paragraph(
"
交易日
"
,?f8));
?86
?
????????????????table.addCell(cell);
?87
?
????????????????cell.setPhrase(
new
?Paragraph(
"
類型
"
,?f8));
?88
?
????????????????table.addCell(cell);
?89
?
????????????????cell.setPhrase(
new
?Paragraph(
"
備注
"
,?f8));
?90
?
????????????????table.addCell(cell);
?91
?
????????????????cell.setPhrase(
new
?Paragraph(
"
ID
"
,?f8));
?92
?
????????????????table.addCell(cell);
?93
?
????????????????cell.setPhrase(
new
?Paragraph(
"
票號
"
,?f8));
?94
?
????????????????table.addCell(cell);
?95
?
????????????????cell.setPhrase(
new
?Paragraph(
"
合計
"
,?f8));
?96
?
????????????????table.addCell(cell);
?97
?
????????????????cell.setPhrase(
new
?Paragraph(
"
余額
"
,?f8));
?98
?
????????????????table.addCell(cell);
?99
?
????????????????
//
create?another?cell
100
?
????????????????PdfPCell?newcell?
=
?
new
?PdfPCell();
101
?
????????????????newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
102
?
????????????????
103
?
????????????????Map
<
String,?Object
>
?map?
=
?
new
?HashMap
<
String,?Object
>
();
104
?
????????????????
for
(
int
?i?
=
?
0
;?i?
<
?list.size();?i
++
){
105
?
????????????????????map?
=
?list.get(i);
106
?
????????????????????String?cashInout?
=
?map.get(
"
cash_inout
"
).toString();
107
?
????????????????????newcell.setPhrase(
new
?Paragraph(map.get(
"
trade_date
"
).toString(),?f8));
108
?
????????????????????table.addCell(newcell);
109
?
????????????????????newcell.setPhrase(
new
?Paragraph(map.get(
"
bankbook_type
"
).toString(),?f8));
110
?
????????????????????table.addCell(newcell);
111
?
????????????????????newcell.setPhrase(
new
?Paragraph(map.get(
"
memo
"
).toString(),?f8));
112
?
????????????????????table.addCell(newcell);
113
?
????????????????????newcell.setPhrase(
new
?Paragraph(map.get(
"
account_id
"
).toString(),?f8));
114
?
????????????????????table.addCell(newcell);
115
?
????????????????????newcell.setPhrase(
new
?Paragraph(map.get(
"
ticket_no
"
).toString(),?f8));
116
?
????????????????????table.addCell(newcell);
117
?
????????????????????newcell.setPhrase(
new
?Paragraph(map.get(
"
amount
"
).toString(),?f8));
118
?
????????????????????table.addCell(newcell);
119
?
????????????????????newcell.setPhrase(
new
?Paragraph(openBalance
+
""
,?f8));
120
?
????????????????????table.addCell(newcell);
121
?
????????????????????
if
(cashInout.equals(
"
I
"
)){
122
?
????????????????????????openBalance?
=
?openBalance?
+
?Float.valueOf(map.get(
"
amount
"
).toString());
123
?
????????????????????}
else
?
if
(cashInout.equals(
"
O
"
)){
124
?
????????????????????????openBalance?
=
?openBalance?
-
?Float.valueOf(map.get(
"
amount
"
).toString());
125
?
????????????????????}
126
?
????????????????}
127
?
????????????????
//
print?total?column
128
?
????????????????newcell.setPhrase(
new
?Paragraph(
"
合計
"
+
openBalance,?f8));
129
?
????????????????table.addCell(
""
);
130
?
????????????????table.addCell(
""
);
131
?
????????????????table.addCell(
""
);
132
?
????????????????table.addCell(
""
);
133
?
????????????????table.addCell(
""
);
134
?
????????????????table.addCell(
""
);
135
?
????????????????table.addCell(newcell);
136
?
????????????????document.add(table);
137
?
????????????}
else
{
138
?
????????????????PdfPTable?table?
=
?
new
?PdfPTable(
1
);
139
?
????????????????table.setWidthPercentage(
100
);
140
?
????????????????table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
141
?
????????????????PdfPCell?cell?
=
?
new
?PdfPCell(
new
?Paragraph(
"
暫無數據
"
));
142
?
????????????????cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
143
?
????????????????table.addCell(cell);
144
?
????????????????document.add(table);
145
?
????????????}
146
?
????????}
147
?
????????
catch
(DocumentException?de)?{
148
?
????????????de.printStackTrace();
149
?
????????????System.err.println(
"
document:?
"
?
+
?de.getMessage());
150
?
????????}
finally
{
151
?
????????????
//
?close?the?document?and?the?outputstream?is?also?closed?internally
152
?
????????????document.close();
153
?
????????}????????
154
?
????}
155
?
}
代碼結構清晰,本來也沒什么東西,就是通過 Spring 調用 service 方法,獲取數據后按照 iText 結構輸出即可。不過代碼里面有個很愚蠢的動作:
document.add(
new
?Paragraph(
"
?
"
,f6)),主要是找不到如何輸出空白行,所以只好出此下策。如果哪位有解法,請告知一下。
做技術的確不能太著急,慢慢來,總會找到出口的。
請注意!引用、轉貼本文應注明原作者:Rosen Jiang 以及出處:
http://m.tkk7.com/rosen
posted on 2008-12-07 13:27
Rosen
閱讀(10005)
評論(3)
編輯
收藏
所屬分類:
Java EE 服務器端
評論
#
re: 利用 iText 實現 PDF 報表下載
2008-12-31 16:02
chis
不錯,很有用。
回復
更多評論
#
re: 利用 iText 實現 PDF 報表下載[未登錄]
2009-02-25 21:47
Max
Document.add(Chunk.NEWLINE); //Try this for add blank row
or
Paragraph.add(Chunk.NEWLINE);
回復
更多評論
#
re: 利用 iText 實現 PDF 報表下載
2009-02-26 09:53
Rosen
@Max
感謝Max同學的熱心。
回復
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
Servlet的異步轉同步調用
利用 iText 實現 PDF 報表下載
OSGi起步(OSGi for Beginners)
Java 生成 JPG 縮略圖
Apache FileUpload 上傳以及 JExcelApi 解析
JBI-Java 實現 SOA 的標準途徑(翻譯)
與 Axis 1.x 同行
BIRT 總覽(翻譯)
XML 的 RUD
四種 XML 解析技術之不完全測試
Powered by:
BlogJava
Copyright © Rosen
主站蜘蛛池模板:
处破痛哭A√18成年片免费
|
麻豆成人久久精品二区三区免费
|
四虎成人免费观看在线网址
|
亚洲日本在线免费观看
|
曰批视频免费40分钟试看天天
|
亚洲乱理伦片在线观看中字
|
18女人腿打开无遮掩免费
|
91亚洲一区二区在线观看不卡
|
国产精品入口麻豆免费观看
|
亚洲第一视频网站
|
99国产精品免费观看视频
|
亚洲视频精品在线观看
|
亚洲免费在线观看视频
|
亚洲一区二区三区免费在线观看
|
aⅴ免费在线观看
|
亚洲人成网站18禁止
|
日本免费v片一二三区
|
黄色网址在线免费观看
|
亚洲中文字幕在线第六区
|
一区二区三区无码视频免费福利
|
亚洲伊人tv综合网色
|
日韩免费一区二区三区在线播放
|
亚洲一本到无码av中文字幕
|
四虎影在线永久免费观看
|
亚洲伦理一区二区
|
99国产精品永久免费视频
|
亚洲av成人一区二区三区在线播放
|
免费二级毛片免费完整视频
|
一个人看的www免费高清
|
久久精品亚洲综合
|
国产精品美女午夜爽爽爽免费
|
狼人大香伊蕉国产WWW亚洲
|
亚洲精品乱码久久久久久下载
|
四虎最新永久免费视频
|
亚洲码欧美码一区二区三区
|
亚洲人成色7777在线观看不卡
|
很黄很污的网站免费
|
亚洲精品9999久久久久无码
|
中文字幕亚洲无线码
|
波多野结衣在线免费观看
|
精品久久久久久亚洲中文字幕
|