.
.
.
.
.
這幾天需要做一個單據打印功能,沒有找到好的辦法,於是只能采用生成PDF文件,然后由客戶端下載到本地進行打印,如果使用Chrome瀏覽器還能支持在線打印預覽。
那么在這里筆者跟大家分享一下使用iText組件的方法,適用於從沒有接觸過iText的新手,老手請飄過。
這里純屬筆者從實踐中所得的經驗,如有錯誤或疏忽之處還請讀者指正。
首先從iText的官網下載這個開源的小組件。
這里筆者使用的是Java版itext-5.2.1。
將itext-5.2.1.zip壓縮包解壓縮后得到7個文件:itextpdf-5.2.1.jar(核心組件)、itextpdf-5.2.1-javadoc.jar(API文檔)、itextpdf-5.2.1-sources.jar(源代碼)、itext-xtra-5.2.1.jar、itext-xtra-5.2.1-javadoc.jar、itext-xtra-5.2.1-sources.jar
使用5步即可生成一個簡單的PDF文檔。
1 // 1.創建 Document 對象 2 Document _document = new Document(); 3 // 2.創建書寫器,通過書寫器將文檔寫入磁盤 4 PdfWriter _pdfWriter = PdfWriter.getInstance(_document, new FileOutputStream("生成文件的路徑")); 5 // 3.打開文檔 6 _document.open(); 7 // 4.向文檔中添加內容 8 _document.add(new Paragraph("Hi")); 9 // 5.關閉文檔 10 _document.close();
OK,搞定,不出問題的話就會在你指定的路徑中生成一個PDF文檔,內容是純文本的“Hi”。
可是這樣並不能完全滿足我們的需求,因為通常我們要生成的PDF文件不一定是純文本格式的,比如我現在要實現打印銷售單的功能,那么最起碼需要繪制表格才行,怎么辦呢?且跟筆者繼續向下研究。
在iText中,有專門的表格類,即PdfPTable類。筆者做了一個簡單的表格示例,請先看代碼:
1 OutTradeList _otl = this.getOtlBiz().findOutTradeListById(this.getOtlid()); 2 String _fileName = _otl.getOtlId() + ".pdf"; 3 4 // iText 處理中文 5 BaseFont _baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", true); 6 // 1.創建 Document 對象 7 Document _document = new Document(PageSize.A4); 8 9 HttpServletResponse response = ServletActionContext.getResponse(); 10 response.setContentType("application/pdf; charset=ISO-8859-1"); 11 response.setHeader("Content-Disposition", "inline; filename=" + new String(_fileName.getBytes(), "iso8859-1")); 12 13 // 2.創建書寫器,通過書寫器將文檔寫入磁盤 14 PdfWriter _pdfWriter = null; 15 try { 16 _pdfWriter = PdfWriter.getInstance(_document, response.getOutputStream()); 17 } catch (Exception e) { 18 this.setMessage("單據生成失敗,請檢查服務器目錄權限配置是否正確"); 19 e.printStackTrace(); 20 System.out.println("2.掛了"); 21 // return INPUT; 22 return null; 23 } 24 if(_pdfWriter == null) { 25 this.setMessage("單據生成失敗,請檢查服務器目錄權限配置是否正確"); 26 System.out.println("3.掛了"); 27 // return INPUT; 28 return null; 29 } 30 31 // 3.打開文檔 32 _document.open(); 33 34 // 4.創建需要填入文檔的元素 35 PdfPTable _table = new PdfPTable(4); 36 PdfPCell _cell = null; 37 38 _table.addCell(new Paragraph("單據號", new Font(_baseFont))); 39 _cell = new PdfPCell(new Paragraph(_otl.getOtlId())); 40 _cell.setColspan(3); 41 _table.addCell(_cell); 42 43 _table.addCell(new Paragraph("客戶名稱", new Font(_baseFont))); 44 _cell = new PdfPCell(new Paragraph(_otl.getClients().getName(), new Font(_baseFont))); 45 _cell.setColspan(3); 46 _table.addCell(_cell); 47 48 _table.addCell(new Paragraph("銷售日期", new Font(_baseFont))); 49 _cell = new PdfPCell(new Paragraph(_otl.getOutDate().toString())); 50 _cell.setColspan(3); 51 _table.addCell(_cell); 52 53 _cell = new PdfPCell(); 54 _cell.setColspan(4); 55 PdfPTable _tabGoods = new PdfPTable(7); 56 // 添加標題行 57 _tabGoods.setHeaderRows(1); 58 _tabGoods.addCell(new Paragraph("序號", new Font(_baseFont))); 59 _tabGoods.addCell(new Paragraph("商品名稱", new Font(_baseFont))); 60 _tabGoods.addCell(new Paragraph("自定義碼", new Font(_baseFont))); 61 _tabGoods.addCell(new Paragraph("規格", new Font(_baseFont))); 62 _tabGoods.addCell(new Paragraph("數量", new Font(_baseFont))); 63 _tabGoods.addCell(new Paragraph("單價", new Font(_baseFont))); 64 _tabGoods.addCell(new Paragraph("小計", new Font(_baseFont))); 65 Object[] _outTrades = _otl.getOutTrades().toArray();
66 // 將商品銷售詳細信息加入表格 67 for(int i = 0; i < _outTrades.length;) { 68 if((_outTrades[i] != null) && (_outTrades[i] instanceof OutTrade)) { 69 OutTrade _ot = (OutTrade) _outTrades[i]; 70 Goods _goods = _ot.getGoods(); 71 _tabGoods.addCell(String.valueOf((++i))); 72 _tabGoods.addCell(new Paragraph(_goods.getName(), new Font(_baseFont))); 73 _tabGoods.addCell(_goods.getUserCode()); 74 _tabGoods.addCell(_goods.getEtalon()); 75 _tabGoods.addCell(String.valueOf(_ot.getNum())); 76 _tabGoods.addCell(String.valueOf(_ot.getPrice())); 77 _tabGoods.addCell(String.valueOf((_ot.getNum() * _ot.getPrice()))); 78 } 79 } 80 _cell.addElement(_tabGoods); 81 _table.addCell(_cell); 82 83 _table.addCell(new Paragraph("總計", new Font(_baseFont))); 84 _cell = new PdfPCell(new Paragraph(_otl.getAllPrice().toString())); 85 _cell.setColspan(3); 86 _table.addCell(_cell); 87 88 _table.addCell(new Paragraph("操作員", new Font(_baseFont))); 89 _cell = new PdfPCell(new Paragraph(_otl.getProcure())); 90 _cell.setColspan(3); 91 _table.addCell(_cell); 92 93 // 5.向文檔中添加內容,將表格加入文檔中 94 _document.add(_table); 95 96 // 6.關閉文檔 97 _document.close(); 98 System.out.println(_fileName); 99 this.setPdfFilePath(_fileName); 100 System.out.println("3.搞定"); 101 // return SUCCESS; 102 return null;
以上代碼是寫在 Struts2 的 Action 中的,當用戶發送了請求之后直接將生成的PDF文件用輸出流寫入到客戶端,瀏覽器收到服務器的響應之后就會詢問用戶打開方式。
當然,我們也可以將文件寫入磁盤等等。
以下是效果圖:

雖然磕磣了點,不過最基本表格已經按照我們預設的樣子出現了。
好了,先寫到這里,有什么問題大家可以多查查API文檔,剩下的樣式問題自己可以慢慢調整。
iText 還有很多強大的功能,由於筆者也是邊學邊用,在這里也只是把學習的結果記錄下來給小菜們作為一些參考,如果有什么不正確的地方,還請各位不吝賜教。
