java导出pdf(纯代码实现)
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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 com.sydata.fmimp.entity.enums.basicdata.SjlxEnum;
import com.sydata.fmimp.entity.enums.stock.FumigationZtEnum;
import com.sydata.fmimp.entity.po.*;
import com.sydata.fmimp.entity.util.CheckVerifyUtil;
import com.sydata.fmimp.stock.infrastructure.enums.CzbzEnum;
import com.sydata.zt.common.common.api.PageResult;
import com.sydata.zt.common.data.bind.annotation.DataBindFieldConvert;
import one.util.streamex.StreamEx;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import static org.apache.commons.lang3.ObjectUtils.isNotEmpty;
@Override
public void download(FumigationDTO fumigationDTO, HttpServletResponse response) throws IOException {
//要下载的数据查询数据部分我去掉了有需要自己根据业务取
FumigationDowloadVO fumigationDowloadVO = new FumigationDowloadVO();
// 定义全局的字体静态变量
Font titlefont;
Font headfont;
Font keyfont = null;
Font textfont = null;
Font content = null;
// 最大宽度
try {
// 不同字体(这里定义为同一种字体:包含不同字号、不同style)
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont = new Font(bfChinese, 16, Font.BOLD);
headfont = new Font(bfChinese, 14, Font.BOLD);
keyfont = new Font(bfChinese, 10, Font.BOLD);
textfont = new Font(bfChinese, 15, Font.NORMAL);
content = new Font(bfChinese, 10, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
BaseFont bf;
Font font = null;
try {
//创建字体
bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
//使用字体并给出颜色
font = new Font(bf,20,Font.BOLD,BaseColor.BLACK);
} catch (Exception e) {
e.printStackTrace();
}
Document document = new Document(new RectangleReadOnly(842F, 595F));
//设置页边距 60:左边距,60:右边距,72:上边距,72:下边距
document.setMargins(60, 60, 72, 72);
try {
PdfWriter writer = PdfWriter.getInstance(document,response.getOutputStream());
//添加页码
writer.setPageEvent(new PdfPageUtil());
//打开生成的pdf文件
document.open();
//设置内容
Paragraph paragraph = new Paragraph("熏蒸备案回执",font);
paragraph.setAlignment(1);
//引用字体
document.add(paragraph);
// 设置表格的列宽和列数
float[] widths = {25f,25f,25f,25f,25f,25f};
PdfPTable table = new PdfPTable(widths);
table.setSpacingBefore(20f);
// 设置表格宽度为100%
table.setWidthPercentage(100.0F);
table.setHeaderRows(1);
table.getDefaultCell().setHorizontalAlignment(1);
PdfPCell cell = null;
//第一行
cell = new PdfPCell(new Paragraph("熏蒸备案编码",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(30);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getXzbm()));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("熏蒸备案时间",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString4(fumigationDowloadVO.getSqxzrq())));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("申请备案单位",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getDwmc(),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
//第二行
cell = new PdfPCell(new Paragraph("熏蒸作业库点",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(30);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getKdmc(),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("负责人",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getFzr(),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("联系电话",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getFzrdh(),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
//第三行
cell = new PdfPCell(new Paragraph("单据状态",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(30);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(shzt(fumigationDowloadVO.getShzt()),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("审核时间",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString5(fumigationDowloadVO.getShsj()),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(" ",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(" ",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
// 设置表格的列宽和列数
float[] widths2 = {25f,25f,25f,25f,25f,25f};
PdfPTable table2 = new PdfPTable(widths2);
table2.setSpacingBefore(20f);
// 设置表格宽度为100%
table2.setWidthPercentage(100.0F);
table2.setHeaderRows(1);
table2.getDefaultCell().setHorizontalAlignment(1);
//人员列表-第四行
cell = new PdfPCell(new Paragraph("姓名",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(20);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("职务",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("职业资格",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("身体状况",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("熏蒸任务分工",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("是否外包",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
//人员列表数据-第五行
if(fumigationDowloadVO.getProples().size() > 0){
for (RecordFumigationPeople prople : fumigationDowloadVO.getProples()) {
PdfPCell cell1 = new PdfPCell(new Paragraph(prople.getXm(), content));
PdfPCell cell2 = new PdfPCell(new Paragraph(prople.getZw(), content));
PdfPCell cell3 = new PdfPCell(new Paragraph(prople.getZyzg(), content));
PdfPCell cell4 = new PdfPCell(new Paragraph(prople.getStzk(), content));
PdfPCell cell5 = new PdfPCell(new Paragraph(prople.getXzrwfg(), content));
PdfPCell cell6 = new PdfPCell(new Paragraph(prople.getSfwb(), content));
//单元格对齐方式
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setFixedHeight(20);
//单元格垂直对齐方式
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(cell1);
table2.addCell(cell2);
table2.addCell(cell3);
table2.addCell(cell4);
table2.addCell(cell5);
table2.addCell(cell6);
}
}
// 设置表格的列宽和列数
float[] widths3 = {25f,25f,25f,25f,25f};
PdfPTable table3 = new PdfPTable(widths3);
table3.setSpacingBefore(20f);
// 设置表格宽度为100%
table3.setWidthPercentage(100.0F);
table3.setHeaderRows(1);
table3.getDefaultCell().setHorizontalAlignment(1);
//实施储粮信息
cell = new PdfPCell(new Paragraph("仓房",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(20);
table3.addCell(cell);
cell = new PdfPCell(new Paragraph("货位",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table3.addCell(cell);
cell = new PdfPCell(new Paragraph("粮食品种",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table3.addCell(cell);
cell = new PdfPCell(new Paragraph("计划熏蒸开始时间",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table3.addCell(cell);
cell = new PdfPCell(new Paragraph("计划熏蒸结束时间",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table3.addCell(cell);
if(fumigationDowloadVO.getDtls().size() > 0){
for (RecordFumigationDtlVO dtl : fumigationDowloadVO.getDtls()) {
PdfPCell cell1 = new PdfPCell(new Paragraph(dtl.getCfmc(), content));
PdfPCell cell2 = new PdfPCell(new Paragraph(dtl.getHwmc(), content));
PdfPCell cell3 = new PdfPCell(new Paragraph(dtl.getLspzmc(), content));
PdfPCell cell4 = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString4(dtl.getJhxzksrq()), content));
PdfPCell cell5 = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString4(dtl.getJhxzjsrq()), content));
//设置居中
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setFixedHeight(20);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
table3.addCell(cell1);
table3.addCell(cell2);
table3.addCell(cell3);
table3.addCell(cell4);
table3.addCell(cell5);
}
}
document.add(new Paragraph("\n"));
document.add(new Paragraph("▋ 基本信息",content));
document.add(new Paragraph("\n"));
document.add(table);
document.add(new Paragraph("\n"));
document.add(new Paragraph("▋ 基本信息",content));
document.add(new Paragraph("\n"));
document.add(table2);
document.add(new Paragraph("\n"));
document.add(new Paragraph("▋ 熏蒸作业储粮粮情",content));
document.add(new Paragraph("\n"));
document.add(table3);
//关闭文档
document.close();
} catch (DocumentException e) {
e.printStackTrace();
log.error("导出pdf失败:{}",e);
}
}