feat(生产入库单申请):

1.自动进行分页
This commit is contained in:
leiqiaozhen 2025-02-20 14:30:30 +08:00
parent b57f3dcefc
commit 8497fd6cd7

View File

@ -916,49 +916,23 @@ public class ZStoragRequestServiceImpl extends ServiceImpl<ZStoragRequestMapper,
// 创建 PDF 文档对象,并设置页边距(左, 右, 上, 下)
// com.itextpdf.text.Document document = new com.itextpdf.text.Document(2,2,2,2); // 左, 右, 上, 下
com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4, 36, 36, 36, 36); // 左, 右, 上, 下
// com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4, 12, 12, 12, 12); // 左, 右, 上, 下
try {
// 获取当前请求的属性
// 获取当前请求的属性
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = attributes.getResponse();
HttpServletResponse response = attributes.getResponse();
// 设置响应字符编码和内容类型
response.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/pdf");
// 设置响应头,指定文件名
response.setHeader("Content-Disposition", "inline;filename=" + fileName);
response.setHeader("Content-Disposition", "inline;filename=" + fileName);
// 刷新缓冲区
response.flushBuffer();
// 获取输出流
response.flushBuffer();
// 获取输出流
ServletOutputStream outputStream = response.getOutputStream();
//---------------------------------------------------------------------------------------------------------
// 创建 PdfWriter 实例,将文档与输出流关联
PdfWriter.getInstance(document, outputStream);
PdfWriter.getInstance(document, outputStream);
// 打开文档
document.open();
//---------------------------------------------------------------------------------------------------------
//getBarCode(documentNo);
// // 打包部署到 Docker 容器后 获取不到静态资源
// ClassPathResource resource = new ClassPathResource("");
// // 获取输入流
// InputStream is = resource.getInputStream();
// // 创建字节数组输出流
// ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
// // 缓冲区
// byte[] buff = new byte[1024];
// int rc;
// // 读取输入流中的数据并写入字节数组输出流
// while ((rc = is.read(buff, 0, 1024)) > 0) {
// swapStream.write(buff, 0, rc);
// }
// // 将字节数组转换为图像对象
// Image image = Image.getInstance(swapStream.toByteArray());
// // 设置图像对齐方式
// image.setAlignment(Image.ALIGN_LEFT);
// // 按比例缩放图像
// image.scalePercent(40);
document.open();
//选择条形码类型(好多类型可供选择)
Code128Bean bean = new Code128Bean();
@ -979,7 +953,7 @@ public class ZStoragRequestServiceImpl extends ServiceImpl<ZStoragRequestMapper,
Image barCodeImage = Image.getInstance(buffer.toByteArray());
barCodeImage.setAlignment(Image.ALIGN_LEFT);
barCodeImage.scalePercent(40); //依照比例缩放
//---------------------------------------------------------------------------------------------------------
PdfPTable table1 = new PdfPTable(3);
table1.setWidthPercentage(100); // 设置表格宽度为页面宽度的 100%
PdfPTable table4 = new PdfPTable(3);
@ -989,9 +963,8 @@ public class ZStoragRequestServiceImpl extends ServiceImpl<ZStoragRequestMapper,
PdfPTable table2 = new PdfPTable(2);
table2.setWidthPercentage(100); // 设置表格宽度为页面宽度的 100%
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//字体
Font font = new Font(bfChinese, 8, Font.NORMAL);
Font pFont = new Font(bfChinese, 16, Font.NORMAL);
Font nFont = new Font(bfChinese, 9, Font.NORMAL);
@ -1006,42 +979,37 @@ public class ZStoragRequestServiceImpl extends ServiceImpl<ZStoragRequestMapper,
Paragraph paragraph1 = new Paragraph(" ", font);
paragraph.setAlignment(1); //设置文字居中 0靠左 1,居中 2,靠右
PdfPCell cell = new PdfPCell();
PdfPCell cell = new PdfPCell();
cell = new PdfPCell(paragraph);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(15);
tableHead.addCell(cell);
cell = new PdfPCell(barCodeImage);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.disableBorderSide(15);
tableHead.addCell(cell);
// 设置表格列宽
// table1.setWidths(new int[]{300, 300, 320, 240, 240});
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(15);
tableHead.addCell(cell);
cell = new PdfPCell(barCodeImage);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.disableBorderSide(15);
tableHead.addCell(cell);
table1.addCell(createCell("工厂: " + siteDescription, hFont, Element.ALIGN_LEFT));
table1.addCell(createCell("申请人: " + name, hFont, Element.ALIGN_LEFT));
table1.addCell(createCell("申请日期: " + date, hFont, Element.ALIGN_LEFT));
table4.addCell(createCell("单据总条目数: " + storagRequestDetailList.size(), hFont, Element.ALIGN_LEFT));
table4.addCell(createCell("单据汇总数量: " + sumQty, hFont, Element.ALIGN_LEFT));
table4.addCell(createCell(" " + "", hFont, Element.ALIGN_LEFT));
// table3.setWidths(new int[]{1400});
table3.addCell(createCell("备注: " + masterNotes, hFont, Element.ALIGN_LEFT));
// table2.setWidths(new int[]{700, 700});
table2.addCell(createCell("接收人: ", hFont, Element.ALIGN_LEFT));
table2.addCell(createCell("接收时间: ", hFont, Element.ALIGN_LEFT));
String[] headers = {
"序号", "托盘号", "条码号", "工单号", "物料号", "物料描述",
"产品型号", "质检状态", "数量", "重量", "宽度", "厚度",
"喷码样式", "销售订单", "备注"
};
// 初始化每列的最大宽度为表头文字的宽度
float[] maxColumnWidths = new float[headers.length];
for (int i = 0; i < headers.length; i++) {
maxColumnWidths[i] = headers[i].length() * 5; // 5 是比例因子,可以根据需要调整
}
String listLength = String.valueOf(storagRequestDetailList.size());
// 遍历 storagRequestDetailList,计算每列的最大宽度
for (ZStoragRequestDetail detail : storagRequestDetailList) {
maxColumnWidths[0] = Math.max(maxColumnWidths[0], 18);
maxColumnWidths[1] = Math.max(maxColumnWidths[1], StrUtil.isNotEmpty(detail.getCarrierNumber()) ? detail.getCarrierNumber().length() * 5 : 5);
@ -1053,15 +1021,13 @@ public class ZStoragRequestServiceImpl extends ServiceImpl<ZStoragRequestMapper,
maxColumnWidths[7] = Math.max(maxColumnWidths[7], StrUtil.isNotEmpty(detail.getQualityInspectionStatus()) ? detail.getQualityInspectionStatus().length() * 5 : 5);
maxColumnWidths[8] = Math.max(maxColumnWidths[8], StrUtil.isNotEmpty(detail.getQty()) ? detail.getQty().length() * 5 : 5);
maxColumnWidths[9] = Math.max(maxColumnWidths[9], StrUtil.isNotEmpty(detail.getWeigth()) ? detail.getWeigth().length() * 5 : 5);
maxColumnWidths[10] = Math.max(maxColumnWidths[10], StrUtil.isNotEmpty(detail.getWidth()) ? detail.getWidth().length() * 5 : 5);
maxColumnWidths[11] = Math.max(maxColumnWidths[11], StrUtil.isNotEmpty(detail.getThickness()) ? detail.getThickness().length() * 5 : 5);
maxColumnWidths[10] = Math.max(maxColumnWidths[10], StrUtil.isNotEmpty(detail.getWidth()) ? detail.getWidth().length() * 5 : 5);
maxColumnWidths[11] = Math.max(maxColumnWidths[11], StrUtil.isNotEmpty(detail.getThickness()) ? detail.getThickness().length() * 5 : 5);
maxColumnWidths[12] = Math.max(maxColumnWidths[12], StrUtil.isNotEmpty(detail.getSprayCodeStyle()) ? detail.getSprayCodeStyle().length() * 5 : 5);
maxColumnWidths[13] = Math.max(maxColumnWidths[13], StrUtil.isNotEmpty(detail.getDnOrder()) ? detail.getDnOrder().length() * 5 : 5);
maxColumnWidths[14] = Math.max(maxColumnWidths[14], StrUtil.isNotEmpty(detail.getNotes()) ? detail.getNotes().length() * 5 : 5);
}
int colNum = 30; // 仍然保留colNum作为每页的最大行数,但不再固定使用
PdfPTable table = new PdfPTable(15);
table.setWidthPercentage(100);
table.setWidths(maxColumnWidths); // 使用计算出的列宽
@ -1100,46 +1066,27 @@ public class ZStoragRequestServiceImpl extends ServiceImpl<ZStoragRequestMapper,
table.addCell(createCell1(storagRequestDetail.getSprayCodeStyle(), font, Element.ALIGN_CENTER));
table.addCell(createCell1(storagRequestDetail.getDnOrder(), font, Element.ALIGN_CENTER));
table.addCell(createCell1(storagRequestDetail.getNotes(), font, Element.ALIGN_CENTER));
// 检查是否需要换页
if (table.size() >= colNum) {
document.add(tableHead);
document.add(paragraph1);
document.add(table1);
// document.add(paragraph1);
document.add(table4);
// document.add(paragraph1);
document.add(table3);
document.add(paragraph1);
document.add(table);
document.add(paragraph1);
document.add(table2);
document.newPage();
table = new PdfPTable(15);
table.setWidthPercentage(100);
table.setWidths(maxColumnWidths);
}
}
// 添加剩余的表格内容
if (table.size() > 0) {
document.add(tableHead);
document.add(paragraph1);
document.add(table1);
// document.add(paragraph1);
document.add(table4);
// document.add(paragraph1);
document.add(table3);
document.add(paragraph1);
document.add(table);
document.add(paragraph1);
document.add(table2);
}
// 添加表格内容到文档
document.add(tableHead);
// document.add(paragraph1);
document.add(table1);
// document.add(paragraph1);
document.add(table4);
// document.add(paragraph1);
document.add(table3);
// document.add(paragraph1);
document.add(table);
// document.add(paragraph1);
document.add(table2);
document.close();
outputStream.flush();
} catch (Exception e) {
throw BusinessException.build(e.getLocalizedMessage());
}
}
/**