导出页面表格和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){ } }) }