導出頁面表格和echart報表圖
1.PDFUtil.javajavascript
package com.mysteel.income.util; import java.awt.Color; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Set; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.codec.Base64; import com.mysteel.income.consts.WebConst; public class PDFUtil { public static void createPdfImg(String imgUrl,String title,String year){ if(imgUrl == null) return; try { String[] url = imgUrl.split(","); imgUrl = url[1]; byte[] buffer = Base64.decode(imgUrl); //生成圖片 // OutputStream out = new FileOutputStream(new File(request.getRealPath("pic/"+fileName+".jpg"))); OutputStream out = new FileOutputStream(WebConst.PDF_IMAGE_PATH); out.write(buffer); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return; } public static void createPdfTable(String json,String title,String paramYear) throws Exception { String pdfTitle = getFileName(title, paramYear); //建立一個文檔對象紙張大小爲A4 Document doc = new Document(PageSize.A4, 50, 50, 50, 50); //設置要輸出到磁盤上的文件名稱 PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File(WebConst.PDF_PATH))); //設置做者信息 doc.addAuthor("income"); //設置文檔建立日期 doc.addCreationDate(); //設置標題 doc.addTitle("財務統計"); //設置值主題 doc.addSubject("報表展現"); //構建頁腳 HeaderFooter footer = new HeaderFooter(new Phrase(), true); //設置頁腳是否有邊框 //0表示無 //1上邊框 //2下邊框 //3上下邊框都有 默認都有 //設置頁腳是否有邊框 footer.setBorder(0); // footer.setBorder(1); // footer.setBorder(2); // footer.setBorder(3); //設置頁腳的對齊方式 footer.setAlignment(Element.ALIGN_CENTER); //將頁腳添加到文檔中 doc.setFooter(footer); //打開文檔開始寫內容 doc.open(); //構建一段落 Paragraph par3 = new Paragraph(pdfTitle, ChineseFont()); //設置局中對齊 par3.setAlignment(Element.ALIGN_CENTER); //添加到文檔 doc.add(par3); //建立一個四列的表格 Table table = new Table(5); //設置邊框 table.setBorder(1); //建立表頭 Cell cell1 = new Cell(new Phrase("日期", ChineseFont())); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_CENTER); cell1.setHeader(true); cell1.setBackgroundColor(Color.RED); Cell cell2 = new Cell(new Phrase("收費類型", ChineseFont())); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_CENTER); cell2.setHeader(true); cell2.setBackgroundColor(Color.RED); Cell cell3 = new Cell(new Phrase("金額", ChineseFont())); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_CENTER); cell3.setHeader(true); cell3.setBackgroundColor(Color.RED); Cell cell4 = new Cell(new Phrase("佔比", ChineseFont())); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setVerticalAlignment(Element.ALIGN_CENTER); cell4.setHeader(true); cell4.setBackgroundColor(Color.RED); Cell cell5 = new Cell(new Phrase("環比", ChineseFont())); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell5.setVerticalAlignment(Element.ALIGN_CENTER); cell5.setHeader(true); cell5.setBackgroundColor(Color.RED); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); //添加此代碼後每頁都會顯示錶頭 table.endHeaders(); //====================================== JSONObject jsonObject = JSON.parseObject(json); Set<String> keySet = jsonObject.keySet(); for (String year : keySet) { JSONObject row = (JSONObject)jsonObject.get(year); JSONArray sflxArray = (JSONArray)row.get("sflx"); int sflxLength = sflxArray.size(); Cell cell11 = new Cell(new Phrase(year, ChineseFont())); cell11.setRowspan(sflxLength); table.addCell(cell11); for (int i=0; i<sflxLength; i++) { JSONObject oneSflx = (JSONObject)sflxArray.get(i); Cell cell22 = new Cell(new Phrase(oneSflx.getString("name"), ChineseFont())); Cell cell33 = new Cell(new Phrase(oneSflx.getString("je"), ChineseFont())); Cell cell44 = new Cell(new Phrase(oneSflx.getString("zb"), ChineseFont())); Cell cell55 = new Cell(new Phrase(oneSflx.getString("hb"), ChineseFont())); table.addCell(cell22); table.addCell(cell33); table.addCell(cell44); table.addCell(cell55); } //HJ JSONObject hjJson = (JSONObject)row.get("hj"); Cell cellHj = new Cell(new Phrase("合計", ChineseFont())); Cell cellHsSflx = new Cell(new Phrase("--", ChineseFont())); Cell cellHjJe = new Cell(new Phrase((String)hjJson.get("zje"), ChineseFont())); Cell cellHjZb = new Cell(new Phrase((String)hjJson.get("zzb"), ChineseFont())); Cell cellHjHb = new Cell(new Phrase((String)hjJson.get("zhb"), ChineseFont())); table.addCell(cellHj); table.addCell(cellHsSflx); table.addCell(cellHjJe); table.addCell(cellHjZb); table.addCell(cellHjHb); } //====================================== //將表格添加到新的文檔 doc.add(table); //構建一段落 Paragraph par4 = new Paragraph("\n\n\n\n", ChineseFont()); //設置局中對齊 par4.setAlignment(Element.ALIGN_CENTER); //添加到文檔 doc.add(par4); //建立新的一頁 //doc.newPage(); //添加圖片 Image image = Image.getInstance(WebConst.PDF_IMAGE_PATH); image.scalePercent(45);//依照比例縮放 //添加到文檔 doc.add(image); //設置對象方式 image.setAlignment(Element.ALIGN_CENTER); doc.close(); writer.close(); } //pdf文檔中文字符處理 public static Font ChineseFont() { BaseFont baseFont = null; try { // baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true); baseFont = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Font chineseFont = new Font(baseFont, 8, Font.NORMAL, Color.BLUE); return chineseFont; } public static void createPdfDetailTable(String jsonTable,String title,String year) { String pdfTitle = getFileName(title, year); try { Document doc = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File(WebConst.PDF_PATH))); doc.addAuthor("income"); doc.addCreationDate(); doc.addTitle("財務統計"); doc.addSubject("報表展現"); HeaderFooter footer = new HeaderFooter(new Phrase(), true); footer.setBorder(0); footer.setAlignment(Element.ALIGN_CENTER); doc.setFooter(footer); doc.open(); Paragraph par3 = new Paragraph(pdfTitle, ChineseFont()); par3.setAlignment(Element.ALIGN_CENTER); doc.add(par3); Table table = new Table(9); table.setBorder(1); Cell cell1 = new Cell(new Phrase("日期", ChineseFont())); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_CENTER); cell1.setHeader(true); cell1.setBackgroundColor(Color.RED); Cell cell2 = new Cell(new Phrase("收入結構", ChineseFont())); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_CENTER); cell2.setHeader(true); cell2.setBackgroundColor(Color.RED); Cell cell3 = new Cell(new Phrase("金額", ChineseFont())); cell3.setColspan(2); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_CENTER); cell3.setHeader(true); cell3.setBackgroundColor(Color.RED); Cell cell4 = new Cell(new Phrase("佔比", ChineseFont())); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setVerticalAlignment(Element.ALIGN_CENTER); cell4.setHeader(true); cell4.setBackgroundColor(Color.RED); Cell cell5 = new Cell(new Phrase("環比", ChineseFont())); cell5.setColspan(2); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell5.setVerticalAlignment(Element.ALIGN_CENTER); cell5.setHeader(true); cell5.setBackgroundColor(Color.RED); Cell cell6 = new Cell(new Phrase("同比", ChineseFont())); cell6.setColspan(2); cell6.setHorizontalAlignment(Element.ALIGN_CENTER); cell6.setVerticalAlignment(Element.ALIGN_CENTER); cell6.setHeader(true); cell6.setBackgroundColor(Color.RED); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); table.addCell(cell6); //添加此代碼後每頁都會顯示錶頭 table.endHeaders(); JSONObject jsonData = (JSONObject)JSONObject.parse(jsonTable); Set<String> years = jsonData.keySet(); for (String yearOrHj : years) { if(!"hj".equals(yearOrHj)){ JSONObject oneSrjgDetail = (JSONObject)jsonData.get(yearOrHj); JSONArray syjgArray = (JSONArray)oneSrjgDetail.get("syjg"); int syjgLength = syjgArray.size(); Cell cell11 = new Cell(new Phrase(yearOrHj, ChineseFont())); cell11.setRowspan(syjgLength); table.addCell(cell11); String zhb = (String)oneSrjgDetail.get("zhb"); String zje = (String)oneSrjgDetail.get("zje"); String ztb = (String)oneSrjgDetail.get("ztb"); Cell cell99 = null; Cell cell77 = null; Cell cell44 = null; for (int i = 0;i < syjgLength; i++) { JSONObject oneSRJG = (JSONObject)syjgArray.get(i); if(i == 0){ Cell cell22 = new Cell(new Phrase(oneSRJG.getString("name"), ChineseFont())); table.addCell(cell22); Cell cell33 = new Cell(new Phrase(oneSRJG.getString("je"), ChineseFont())); table.addCell(cell33); cell44 = new Cell(new Phrase(zje, ChineseFont())); cell44.setRowspan(syjgLength); table.addCell(cell44); Cell cell55 = new Cell(new Phrase(oneSRJG.getString("zb"), ChineseFont())); table.addCell(cell55); Cell cell66 = new Cell(new Phrase(oneSRJG.getString("hb"), ChineseFont())); table.addCell(cell66); cell77 = new Cell(new Phrase(zhb, ChineseFont())); cell77.setRowspan(syjgLength); table.addCell(cell77); Cell cell88 = new Cell(new Phrase(oneSRJG.getString("tb"), ChineseFont())); table.addCell(cell88); cell99 = new Cell(new Phrase(ztb, ChineseFont())); cell99.setRowspan(syjgLength); table.addCell(cell99); }else{ Cell cell22 = new Cell(new Phrase(oneSRJG.getString("name"), ChineseFont())); table.addCell(cell22); Cell cell33 = new Cell(new Phrase(oneSRJG.getString("je"), ChineseFont())); table.addCell(cell33); Cell cell55 = new Cell(new Phrase(oneSRJG.getString("zb"), ChineseFont())); table.addCell(cell55); Cell cell66 = new Cell(new Phrase(oneSRJG.getString("hb"), ChineseFont())); table.addCell(cell66); Cell cell88 = new Cell(new Phrase(oneSRJG.getString("tb"), ChineseFont())); table.addCell(cell88); } } } } //將表格添加到新的文檔 doc.add(table); //構建一段落 Paragraph par4 = new Paragraph("\n\n\n\n", ChineseFont()); //設置局中對齊 par4.setAlignment(Element.ALIGN_CENTER); //添加到文檔 doc.add(par4); //建立新的一頁 //doc.newPage(); //添加圖片 Image image = Image.getInstance(WebConst.PDF_IMAGE_PATH); image.scalePercent(45);//依照比例縮放 //添加到文檔 doc.add(image); //設置對象方式 image.setAlignment(Element.ALIGN_CENTER); doc.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } private static String getFileName(String title,String year){ String pdfTitle = ""; if("3".equals(year) || "5".equals(year)){ pdfTitle = "近"+year+"年收入報表" + pdfTitle; }else{ pdfTitle = year+"年收入報表" + pdfTitle; } return pdfTitle; } }
效果圖:
html
頁面JavaScript代碼
java
var isFirst = $isFirst; function loadPdf(){ var jsonTable = null; var jsonBar = null; var year = $('#myTab').find('li[class="active"]').find('a').data('year'); var imgUrl = chart.getDataURL();//獲取echart圖片的base64信息 if(year == 5){ html2canvas(document.getElementById("area-chart-5year"), { //調用html2canvas.js插件將普通html轉換成可視化圖片 allowTaint: true, taintTest: false, onrendered: function(canvas) { canvas.id = "mycanvas"; //生成base64圖片數據 imgUrl = canvas.toDataURL();//獲取base64信息 } }); } if(isFirst == 1){ if(year == 3){ jsonTable = $load3YearSYBAllData.table; jsonBar = $load3YearSYBAllData.bar; }else if(year == 5){ jsonTable = $load5YearSYBAllData.table; jsonBar = $load5YearSYBAllData.bar; } }else{ if(year == 3){ jsonTable = btnMsg.load3YearSYBAllData.table; jsonBar = btnMsg.load3YearSYBAllData.bar; }else if(year == 5){ jsonTable = btnMsg.load5YearSYBAllData.table; jsonBar = btnMsg.load5YearSYBAllData.bar; } } $.ajax({ method : 'post', url : '/pdf/converPdf', data : { jsonTable : JSON.stringify(jsonTable), jsonBar : JSON.stringify(jsonBar), // title : title, imgUrl : imgUrl, year : year }, success : function(msg){ } }) }