项目需求
在OA项目中需要导出PDF格式的员工请假单
导入jar包
<!--pdf导出-->
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- 进行导出接口文档pdf解决汉字无法显示问题 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
itextpdf 用户操作pdf文档,itext-asian用于解决pdf文件内汉字无法显示问题
代码示例
response.reset();
response.setCharacterEncoding("utf-8");
//导出文件名称为汉字
response.setHeader("content-disposition", "attachment; filename="+new String("请假申请单.pdf".getBytes("utf-8"),"ISO8859-1"));
response.setContentType("application/pdf");
//ByteArrayOutputStream流存储pdf内容
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document=new Document();
PdfWriter writer=PdfWriter.getInstance(document, baos);
document.open();
//解决中文无法显示问题
BaseFont bfChn=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//设置字体大小,以及字体样式
Font fontChnTitle=new Font(bfChn,12,Font.BOLD);
Font fontChnText=new Font(bfChn,10,Font.NORMAL);
//设置字体颜色
fontChnText.setColor(BaseColor.BLACK);
//添加申请信息
this.addRequestTable(fontChnText, requestResult, fontChnTitle, document);
//关闭文档流
document.close();
baos.writeTo(response.getOutputStream());
//关闭输出流
baos.close();
填充pdf
private void addRequestTable(Font fontChn1,Request request,Font titleFont,Document document) throws DocumentException{
//基本信息
PdfPTable table1 = new PdfPTable(1); //只有一列的table
PdfPCell cell=new PdfPCell(new Phrase("请假申请单",titleFont));
cell.setFixedHeight(titleHeight); //设置行高
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //设置剧中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直对齐
table1.addCell(cell);
//表头信息
PdfPTable table2 = new PdfPTable(4); //设置有四列的表格
table2.setWidths(headWidth); //{30,70,30,70};
cell=new PdfPCell(new Phrase("申请人",fontChn1));
cell.setFixedHeight(height); //设置固定行高 注意设置固定行高后表格中文字便不会自动换行
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(cell);
cell=new PdfPCell(new Phrase(workerName,fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPaddingLeft(20f); //距离左表格边框 20f
table2.addCell(cell);
cell=new PdfPCell(new Phrase("申请类型",fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(cell);
cell=new PdfPCell(new Phrase(requestTypeName,fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPaddingLeft(20f);
table2.addCell(cell);
cell=new PdfPCell(new Phrase("开始时间",fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(cell);
cell=new PdfPCell(new Phrase(beginDate,fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPaddingLeft(20f);
table2.addCell(cell);
cell=new PdfPCell(new Phrase("结束时间",fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(cell);
cell=new PdfPCell(new Phrase(endDate,fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPaddingLeft(20f);
table2.addCell(cell);
cell=new PdfPCell(new Phrase("请假天数",fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(cell);
cell=new PdfPCell(new Phrase(dateSpan,fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPaddingLeft(20f);
table2.addCell(cell);
cell=new PdfPCell(new Phrase("请假类型",fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(cell);
cell=new PdfPCell(new Phrase(leaveTypeName,fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPaddingLeft(20f);
table2.addCell(cell);
PdfPTable table3 = new PdfPTable(2);
table3.setWidths(bodyWidth);
cell=new PdfPCell(new Phrase("地点",fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table3.addCell(cell);
Chunk chunk = new Chunk(place,fontChn1);
Paragraph paragraph=new Paragraph();
paragraph.add(chunk);
cell=new PdfPCell(paragraph);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPaddingLeft(20f);
cell.setPaddingBottom(6f); //设置距离底部6f
cell.setLeading(3f,1f); //设置单元格中文字行距为3f
table3.addCell(cell);
cell=new PdfPCell(new Phrase("原因",fontChn1));
cell.setFixedHeight(height);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table3.addCell(cell);
chunk = new Chunk(reason,fontChn1);
paragraph=new Paragraph();
paragraph.add(chunk);
cell=new PdfPCell(paragraph);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPaddingLeft(20f);
cell.setPaddingBottom(6f);
cell.setLeading(3f,1f);
table3.addCell(cell);
document.add(table1);
document.add(table2);
document.add(table3);
}
注意事项
-
导出文件名称为汉字时 response.setHeader(“content-disposition”, “attachment; filename=”+new String(“请假申请单.pdf”.getBytes(“utf-8”),“ISO8859-1”))需要进行一下编码格式的转换
-
cell.setFixedHeight(height)单元格中设置固定行高后,内容就不会进行自动换行 ,如果文字内容很多需要自动换行,不需要设置固定行高
-
文字自定换行后,可以通过设置cell.setLeading(3f,1f)设置行距