package cn.piesat.tj.common.utils;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import jxl.write.Label;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @Description: pdf工具类
* @author: lygigsh
* @create: 2022-01-5 16:22
**/
public class PDFUtil {
/**
* 获取指定内容与字体的单元格
* @param string
* @param font
* @return
*/
public static PdfPCell getPDFCell(String string, Font font)
{
//创建单元格对象,将内容与字体放入段落中作为单元格内容
PdfPCell cell=new PdfPCell(new Paragraph(string,font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//设置最小单元格高度
cell.setMinimumHeight(25);
return cell;
}
/**
* 合并行的静态函数
* @param str
* @param font
* @param i
* @return
*/
public static PdfPCell mergeRow(String str,Font font,int i) {
//创建单元格对象,将内容及字体传入
PdfPCell cell=new PdfPCell(new Paragraph(str,font));
//设置单元格内容居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//将该单元格所在列包括该单元格在内的i行单元格合并为一个单元格
cell.setRowspan(i);
return cell;
}
/**
* 合并列的静态函数
* @param str
* @param font
* @param i
* @return
*/
public static PdfPCell mergeCol(String str,Font font,int i) {
PdfPCell cell=new PdfPCell(new Paragraph(str,font));
cell.setMinimumHeight(25);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//将该单元格所在行包括该单元格在内的i列单元格合并为一个单元格
cell.setColspan(i);
return cell;
}
public static void createPDF(HttpServletResponse response, List prList, String[] columns,String titleName) {
// 新建document对象
Document document = new Document(PageSize.A4,36,36,126,36);
try {
//添加中文字体
BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//设置字体样式
Font textFont = new Font(bfChinese,12,Font.NORMAL);//正常
Font firsetTitleFont = new Font(bfChinese,22,Font.BOLD); //一级标题
//设置表格样式,大小
PdfPTable dataTable = new PdfPTable(columns.length);
dataTable.setSpacingBefore(10);
dataTable.setHorizontalAlignment(Element.ALIGN_CENTER);
dataTable.setTotalWidth(500); //设置列宽
dataTable.setLockedWidth(true); //锁定列宽
dataTable.setSpacingAfter(30);
List<String> thead= Arrays.asList(columns);
//创建输出流
PdfWriter.getInstance(document, response.getOutputStream());
//判断是否打开
if (!document.isOpen()){
document.open();
}
//开始写入数据
Paragraph paragraph = new Paragraph(10);// 边距
// 1 2 3 中右左
paragraph.setAlignment(1); // 对齐方式
paragraph.setFont(firsetTitleFont);// 设置段落字体
Chunk chunk = new Chunk(titleName);
paragraph.add(chunk);
document.add(paragraph);
//添加表头
for(String p:thead){
PdfPCell cell=new PdfPCell(new Phrase(p,textFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(new BaseColor(216, 218, 220));
dataTable.addCell(cell);
}
//添加表内容
//判断表中是否有数据
if (prList != null && prList.size() > 0) {
//循环写入表中数据
for (int i = 0; i < prList.size(); i++) {
//转换成map集合{activyName:测试功能,count:2}
Map map = (Map)prList.get(i);
//循环输出map中的子集:既列值
int j=0;
for(Object o:map.keySet()){
//ps:因为要“”通用”“导出功能,所以这里循环的时候不是get("Name"),而是通过map.get(o)
if (StringUtils.isEmpty(map.get(o))){
map.put(o,"");
}
PdfPCell cell=new PdfPCell(new Phrase(String.valueOf(map.get(o)),textFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
dataTable.addCell(cell);
j++;
}
}
}
document.add(dataTable);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally {
document.close();
}
}
public static void exportPDF(HttpServletResponse response,String filename,List listData,String[] columns,String titleName)throws Exception
{
//调用上面的方法、生成Excel文件
response.setContentType("application/pdf;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+new String( filename.getBytes("gb2312"), "ISO8859-1" ));
createPDF(response,listData,columns,titleName);
}
}
调用exportPDF();传入对应的参数即可