Java动态数据生成PDF文档及下载

查了很多 aozbbs.com Q1446595067 资料都没有我想要的pdf,于是根据iText基础知识(这里是看了宝爷的笔记点击打开链接)自行编辑了一个简单的表格pdf文档

运用MVC springCXF

需要的jar包:https://download.csdn.net/download/weixin_40989555/10487913

jar包已经解决中文的编码问题,可以直接使用。

若编写多行多列表格(maven依赖):
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>

    </dependency>

话不多说,下面进行编码。

jsp页面

js /** * 点击“是”进行PDF打印 */ function pdfPrint(){ var aab001 = qdw_aab001; document.getElementById("form_delayWarn").action = "./lrs/querydelay/delWarnPdfPrint.do?aab001=" + aab001; return doConfirm("下载", function() { document.getElementById("form_delayWarn").submit(); }); /* 直接新开一个页面显示不下载 (js2方法) var url = "./lrs/querydelay/delWarnPdfPrint.do?aab001=" + aab001; window.open(url, "xxxxxxx"); */ } Controller层 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.itextpdf.text.Element; import com.lowagie.text.Chapter; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Section; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; private QueryDelayServiceq qdService; @SuppressWarnings("unchecked") @RequestMapping(value = "/delWarnPdfPrint" , method = RequestMethod.POST) @ResponseBody public String delWarnPdfPrint(String aab001, HttpServletRequest req, HttpServletResponse resp) throws Exception { log.info("xxxxPDF打印-返回数据"); //返回list集合数据 String alllist = qdService.delWarnPdfPrint(aab001); File file = null; InputStream fin = null; ServletOutputStream out = null; List list = new ArrayList (); List pdflist = new ArrayList (); try { list = (List ) JacksonUtil.toList(alllist, ArrayList.class, BC90.class);//这是我自定义的String转list集合方法,跳过 for (BC90 bc90 : list) { DWPdf dpdf = new DWPdf(); dpdf.setRownum(bc90.getRownum()); dpdf.setAac003(bc90.getAac003()); dpdf.setAac002(bc90.getAac002()); dpdf.setAbc939(bc90.getAbc939()); dpdf.setAbc913(bc90.getAbc913()); dpdf.setAbc914(bc90.getAbc914()); dpdf.setAbc909(bc90.getAbc909()); pdflist.add(dpdf); } } catch (Exception e) { e.printStackTrace(); log.error("逾期合同json转换出错",e); } /*******pdf文件内容生成开始*********/ Document document = new Document(PageSize.A4, 25, 25, 25, 25); //创建文件夹 String filePar = null; Date nowTime = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd.hh.mm.ss"); String time = sdf.format(nowTime);//获取当前时间字符串形式 filePar = "C:/企业逾期合同"+time;// 文件夹路径 File myPath = new File( filePar ); file = new File(filePar+"/"+"企业逾期合同.pdf"); if (!myPath.exists()){ myPath.mkdir(); } /* * import com.lowagie.text.pdf.PdfWriter; */ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));// throws FileNotFoundException, // DocumentException document.open(); /* * 设中文字体 */ BaseFont bfChinese = null;// FontFactory.getFont(FontFactory.COURIER, // 14, Font.BOLD, new CMYKColor(0, 255, 0, 0);//大小,粗细,颜色 try { bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); } catch (IOException e) { e.printStackTrace(); } //定义中文字体大小 Font f10 = new Font(bfChinese, 10, Font.NORMAL); Font f12 = new Font(bfChinese, 12, Font.NORMAL); Font f26 = new Font(bfChinese, 26, Font.NORMAL);//一号字体 /* * 创建标题 */ Paragraph title1 = new Paragraph("标 题", f26); title1.setAlignment(Element.ALIGN_CENTER); Chapter chapter1 = new Chapter(title1, 1); chapter1.setNumberDepth(0); Section section1 = chapter1; // 创建有7列的表格 int colNumber = 7; PdfPTable t = new PdfPTable(colNumber); t.setSpacingBefore(25);//设置段落上空白 t.setSpacingAfter(25);//设置段落上下空白 t.setHorizontalAlignment(Element.ALIGN_CENTER);// 居左 float[] cellsWidth = { 0.08f, 0.1355f, 0.2f, 0.3f, 0.2f, 0.2f , 0.145f }; // 定义表格的宽度 t.setWidths(cellsWidth);// 单元格宽度 t.setTotalWidth(500f);//表格的总宽度 t.setWidthPercentage(100);// 表格的宽度百分比 //设置表头 PdfPCell c1 = new PdfPCell(new Paragraph("序号",f12)); t.addCell(c1); PdfPCell c2 = new PdfPCell(new Paragraph("姓名",f12)); t.addCell(c2); PdfPCell c3 = new PdfPCell(new Paragraph("×××号",f12)); t.addCell(c3); PdfPCell c4 = new PdfPCell(new Paragraph("单位名称",f12)); t.addCell(c4); PdfPCell c5 = new PdfPCell(new Paragraph("合同开始日期",f12)); t.addCell(c5); PdfPCell c6 = new PdfPCell(new Paragraph("合同结束日期",f12)); t.addCell(c6); PdfPCell c7 = new PdfPCell(new Paragraph("合同状态",f12)); t.addCell(c7); //向表格类动态填充list集合数据 for(int i=0;i 0) { filename = URLEncoder.encode(filename, "UTF-8"); } else { filename = new String(filename.getBytes("UTF-8"), "ISO8859-1"); } //下载使用 删除可用(js2方法)直接在页面显示不下载 resp.addHeader("Content-Disposition", "attachment;filename=" + filename + ".pdf");//这里填想要的文件格式 out = resp.getOutputStream(); byte[] buffer = new byte[512]; // 缓冲区 int bytesToRead = -1; // 通过循环将读入的Word文件的内容输出到浏览器中 while ((bytesToRead = fin.read(buffer)) != -1) { out.write(buffer, 0, bytesToRead); } } finally { if (fin != null) fin.close(); if (out != null) out.close(); if (file != null) file.delete(); // 删除临时文件 if (myPath != null) myPath.delete(); } return null; } Service层 public String delWarnPdfPrint(String aab001) { if(aab001 != null){ List delWrList = BC90Mapper.delWarnPdfPrint(aab001); Map pdfMap = new TreeMap (); Map allMap = new TreeMap (); allMap.put("delWrList", delWrList); return JsonUtil.toJson(delWrList);//这里是自定义方法转译维json字符串返回数据 } return respErrorMsg("请重新登入系统或刷新页面"); } mapper层 List delWarnPdfPrint(String aab001); 写了那么多来看看效果吧

猜你喜欢

转载自blog.51cto.com/13864280/2140290