Java 利用iText生成pdf并导出

来源:互联网 发布:jwplayer.js 编辑:程序博客网 时间:2024/04/29 02:57

前言

  项目中需要导出pdf文档,以前没有做过这块儿的功能,于是上网搜索了一下,目前有好几个构件支持该项功能,结合项目要求,很快就确定使用iText来实现。

  那么接下来怎么着手呢,我第一反应是上官网查看官方提供的api和demo,通常官方的例子都很简炼并且不会有复杂的上下文,很容易上手。

  官网http://developers.itextpdf.com/examples 上有很多实例,基本上整合一下各个实例的api就能满足。


实现(iText5.5.9)

 1. 利用iTest生成pdf,保存在服务器上的临时目录

   整体结构

String pdfFilePath = projectPdfFolderPath + "/" + DateUtil.format(new Date(), DateUtil.YYYYMMDDHHMMSS) + ".pdf";//设置pdf文件输出路径Document document = new Document();FileOutputStream  outStream = new FileOutputStream(pdfFilePath);PdfWriter.getInstance(document, outStream);document.open();//添加pdf内容document.close();return pdfFilePath;

  生成pdf文档的标题 

BaseFont b1 = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);Font title = new Font(b1,24,Font.BOLD);Paragraph titleParagraph = new Paragraph("服务接口定义 v1.0.1",title); titleParagraph.setAlignment(Element.TITLE);document.add(titleParagraph);

  生成表格

Font text = new Font(b1,12);Paragraph blank = new Paragraph(" ",text);PdfPTable baseTable = new PdfPTable(2);//表格为两例baseTable.setWidthPercentage(90);//宽度为90%baseTable.setWidths(new int[]{1,4}); //两列宽度为1:4baseTable.setPaddingTop(100);//方法路径PdfPCell pathCell1 = new PdfPCell(new Paragraph("方法路径",text));baseTable.addCell(pathCell1);String path = servicePath+"/"+protocolPath+"/"+methodPath;path= path.replaceAll("\\/{2,}","/");//path的值为/user/get/{id}PdfPCell pathCell2 = new PdfPCell(new Paragraph(path,text));baseTable.addCell(pathCell2);//请求类型PdfPCell typeCell1 = new PdfPCell(new Paragraph("请求类型",text));baseTable.addCell(typeCell1);PdfPCell typeCell2 = new PdfPCell(new Paragraph(methodType,text));baseTable.addCell(typeCell2);//methodType为get……document.add(blank);document.add(baseTable);

 生成带Rowspan的表格

PdfPTable paramTable = new PdfPTable(3);//表格为3列paramTable.setWidthPercentage(90);//表格宽度为文档的90%paramTable.setWidths(new int[]{1,1,4});//列宽比例为1:1:4……PdfPCell paramNameCell = new PdfPCell(new Paragraph(paramName,text));paramNameCell.setRowspan(3);//设置Rowspan为3paramTable.addCell(paramNameCell);PdfPCell paramTypeCell1 = new PdfPCell(new Paragraph("参数类型",text));paramTable.addCell(paramTypeCell1);PdfPCell paramTypeCell2 = new PdfPCell(new Paragraph(paramType,text));paramTable.addCell(paramTypeCell2);PdfPCell paramSourceCell1 = new PdfPCell(new Paragraph("参数来源",text));paramTable.addCell(paramSourceCell1);PdfPCell paramSourceCell2 = new PdfPCell(new Paragraph(paramSource,text));paramTable.addCell(paramSourceCell2);PdfPCell paramDescCell1 = new PdfPCell(new Paragraph("参数说明",text));paramTable.addCell(paramDescCell1);PdfPCell paramDescCell2 = new PdfPCell(new Paragraph(paramDesc,text));paramTable.addCell(paramDescCell2);document.add(blank);document.add(paramTable);

2. web下载导出

该web项目是由springMVC框架实现,因此需要在controller方法中设置文件下载

//生成pdf文档在服务器的临时目录,并返回pdf文档的完整路径String filePath =generateApiBusinessInfoPdf(version, protocols, sb);//下载该pdf文档InputStream bis = null;OutputStream bos = null;try {File file = new File(filePath);response.setHeader("Content-disposition","attachment; filename="+ new String(URLEncoder.encode("测试pdf文档.pdf", "UTF-8").getBytes("UTF-8"), "ISO8859-1"));response.setHeader("Content-Length", String.valueOf(file.length()));bis = new BufferedInputStream(new FileInputStream(file));bos = new BufferedOutputStream(response.getOutputStream());byte[] buff = new byte[2048];int bytesRead;while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {bos.write(buff, 0, bytesRead);}} catch (Exception e) {throw e;} finally {try {if (bis!= null) {bis.close(); }} catch (IOException ioe) {}try {if (bos!= null) {bos.close(); }} catch (IOException ioe) {}}



3. 导出文档结果


  

 Note:  1.需要合理地设置pdf的字体,否则中文会显示不出来

             2.设置下载文件文件名的中文字符

 本文链接:http://blog.csdn.net/musa875643dn/article/details/51291356

1 0
原创粉丝点击