|
|
|
|
@ -53,16 +53,14 @@ public class PDFGenerator {
|
|
|
|
|
private static final float CELL_PADDING = 5;
|
|
|
|
|
private static final float[] COLUMN_WIDTHS = {LEFT_COL_WIDTH, RIGHT_COL_WIDTH};
|
|
|
|
|
private static final float MIN_IMAGE_HEIGHT = 50;
|
|
|
|
|
public static void generateInstallationPDF(DeviceInstallInfo entity, OutputStream outputStream) throws IOException {
|
|
|
|
|
|
|
|
|
|
// 首先生成PDF内容到字节数组
|
|
|
|
|
ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
|
|
public static void generateInstallationPDFBytes(DeviceInstallInfo entity, ByteArrayOutputStream outputStream) throws IOException {
|
|
|
|
|
try (PDDocument document = new PDDocument()) {
|
|
|
|
|
// 加载字体(需替换实际路径)
|
|
|
|
|
// 加载字体
|
|
|
|
|
// PDType0Font font = PDType0Font.load(document, new File("D:\\NotoSansCJK-Regular.ttf"));
|
|
|
|
|
//PDType0Font font = PDType0Font.load(document, new File("/ttf/simsun.ttf"));
|
|
|
|
|
PDType0Font font = PDType0Font.load(document, new File("/ttf/NotoSansCJK-Regular.ttf"));
|
|
|
|
|
|
|
|
|
|
// 初始化第一页
|
|
|
|
|
PDPage currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
|
document.addPage(currentPage);
|
|
|
|
|
@ -78,13 +76,11 @@ public class PDFGenerator {
|
|
|
|
|
|
|
|
|
|
// 2. 绘制基本信息表格
|
|
|
|
|
String[][] baseData = {
|
|
|
|
|
// {"公司名称", entity.getCorporateName()},
|
|
|
|
|
{"申请时间", entity.getProposerTime().toString()},
|
|
|
|
|
{"申请人", entity.getProposer()},
|
|
|
|
|
{"申请人班组", entity.getProposerTeam()},
|
|
|
|
|
{"小区名字", entity.getCommunityName()},
|
|
|
|
|
{"用户姓名", entity.getUserName()},
|
|
|
|
|
// {"用户姓名", entity.getUserName()+"-" + entity.getCommunityName()+entity.getBuildingUnit()+entity.getRoomNo()},
|
|
|
|
|
{"电话", entity.getUserIpone()},
|
|
|
|
|
{"楼栋单元号", entity.getBuildingUnit()},
|
|
|
|
|
{"房间号", entity.getRoomNo()},
|
|
|
|
|
@ -92,14 +88,11 @@ public class PDFGenerator {
|
|
|
|
|
{"切断阀编号", entity.getShutValueNumber()},
|
|
|
|
|
{"燃气表号", entity.getGasMeterNumber()},
|
|
|
|
|
{"备注", entity.getBuildingUnit()}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
drawTable(cs, font, TABLE_MARGIN, currentY, baseData, COLUMN_WIDTHS);
|
|
|
|
|
currentY -= (ROW_HEIGHT * baseData.length) + 20f;
|
|
|
|
|
// @ApiModelProperty(value = "安装前图片")
|
|
|
|
|
// 创建后续页面 - 图片部分
|
|
|
|
|
// 图片处理(带自动分页)
|
|
|
|
|
// 图片处理(带自动分页)
|
|
|
|
|
|
|
|
|
|
// 3. 图片处理(带自动分页)
|
|
|
|
|
List<String[]> imageGroups = Arrays.asList(
|
|
|
|
|
new String[]{entity.getDeviceInfoImage(), "设备信息图片"},
|
|
|
|
|
new String[]{entity.getBeforeInstallationImage(), "安装前图片"},
|
|
|
|
|
@ -184,37 +177,201 @@ public class PDFGenerator {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
cs.close();
|
|
|
|
|
}
|
|
|
|
|
// 将PDF保存到ByteArrayOutputStream
|
|
|
|
|
document.save(pdfBaos);
|
|
|
|
|
|
|
|
|
|
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
// document.save(baos);
|
|
|
|
|
// outputStream.flush();
|
|
|
|
|
// baos.writeTo(outputStream);
|
|
|
|
|
// 将PDF保存到输出流
|
|
|
|
|
document.save(outputStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void generateInstallationPDF(DeviceInstallInfo entity, OutputStream outputStream) throws IOException {
|
|
|
|
|
ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
|
|
|
|
|
generateInstallationPDFBytes(entity, pdfBaos);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// 创建ZIP压缩包并将PDF写入输出流
|
|
|
|
|
// 创建ZIP压缩包
|
|
|
|
|
try (ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
|
|
|
|
|
// 设置最高压缩级别
|
|
|
|
|
zipOut.setLevel(Deflater.BEST_COMPRESSION);
|
|
|
|
|
// 创建ZIP条目
|
|
|
|
|
ZipEntry entry = new ZipEntry(entity.getUserName() + entity.getCommunityName() + entity.getBuildingUnit() + entity.getRoomNo() + ".pdf");
|
|
|
|
|
zipOut.putNextEntry(entry);
|
|
|
|
|
|
|
|
|
|
// 将PDF数据写入ZIP
|
|
|
|
|
byte[] pdfBytes = pdfBaos.toByteArray();
|
|
|
|
|
zipOut.write(pdfBytes, 0, pdfBytes.length);
|
|
|
|
|
|
|
|
|
|
// 关闭当前条目
|
|
|
|
|
zipOut.closeEntry();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 辅助方法:生成文件名
|
|
|
|
|
public static String generateFileName(DeviceInstallInfo entity) {
|
|
|
|
|
return entity.getUserName() + entity.getCommunityName() +
|
|
|
|
|
entity.getBuildingUnit() + entity.getRoomNo() + ".pdf";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public static void generateInstallationPDF(DeviceInstallInfo entity, OutputStream outputStream) throws IOException {
|
|
|
|
|
//
|
|
|
|
|
// // 首先生成PDF内容到字节数组
|
|
|
|
|
// ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
|
|
|
|
|
//
|
|
|
|
|
// try (PDDocument document = new PDDocument()) {
|
|
|
|
|
// // 加载字体(需替换实际路径)
|
|
|
|
|
//// PDType0Font font = PDType0Font.load(document, new File("D:\\NotoSansCJK-Regular.ttf"));
|
|
|
|
|
// //PDType0Font font = PDType0Font.load(document, new File("/ttf/simsun.ttf"));
|
|
|
|
|
// PDType0Font font = PDType0Font.load(document, new File("/ttf/NotoSansCJK-Regular.ttf"));
|
|
|
|
|
// // 初始化第一页
|
|
|
|
|
// PDPage currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
|
// document.addPage(currentPage);
|
|
|
|
|
// PDPageContentStream cs = new PDPageContentStream(document, currentPage);
|
|
|
|
|
// float currentY = PDRectangle.A4.getHeight() - TABLE_MARGIN;
|
|
|
|
|
//
|
|
|
|
|
// try {
|
|
|
|
|
// // 1. 绘制标题(居中)
|
|
|
|
|
// drawCenteredText(cs, font, FONT_SIZE+10,
|
|
|
|
|
// "安装信息录入表",
|
|
|
|
|
// PDRectangle.A4.getWidth()/2, currentY);
|
|
|
|
|
// currentY -= ROW_HEIGHT * 1.5f;
|
|
|
|
|
//
|
|
|
|
|
// // 2. 绘制基本信息表格
|
|
|
|
|
// String[][] baseData = {
|
|
|
|
|
// // {"公司名称", entity.getCorporateName()},
|
|
|
|
|
// {"申请时间", entity.getProposerTime().toString()},
|
|
|
|
|
// {"申请人", entity.getProposer()},
|
|
|
|
|
// {"申请人班组", entity.getProposerTeam()},
|
|
|
|
|
// {"小区名字", entity.getCommunityName()},
|
|
|
|
|
// {"用户姓名", entity.getUserName()},
|
|
|
|
|
// // {"用户姓名", entity.getUserName()+"-" + entity.getCommunityName()+entity.getBuildingUnit()+entity.getRoomNo()},
|
|
|
|
|
// {"电话", entity.getUserIpone()},
|
|
|
|
|
// {"楼栋单元号", entity.getBuildingUnit()},
|
|
|
|
|
// {"房间号", entity.getRoomNo()},
|
|
|
|
|
// {"报警器编号", entity.getDeviceName()},
|
|
|
|
|
// {"切断阀编号", entity.getShutValueNumber()},
|
|
|
|
|
// {"燃气表号", entity.getGasMeterNumber()},
|
|
|
|
|
// {"备注", entity.getBuildingUnit()}
|
|
|
|
|
//
|
|
|
|
|
// };
|
|
|
|
|
// drawTable(cs, font, TABLE_MARGIN, currentY, baseData, COLUMN_WIDTHS);
|
|
|
|
|
// currentY -= (ROW_HEIGHT * baseData.length) + 20f;
|
|
|
|
|
//// @ApiModelProperty(value = "安装前图片")
|
|
|
|
|
// // 创建后续页面 - 图片部分
|
|
|
|
|
// // 图片处理(带自动分页)
|
|
|
|
|
// // 图片处理(带自动分页)
|
|
|
|
|
// List<String[]> imageGroups = Arrays.asList(
|
|
|
|
|
// new String[]{entity.getDeviceInfoImage(), "设备信息图片"},
|
|
|
|
|
// new String[]{entity.getBeforeInstallationImage(), "安装前图片"},
|
|
|
|
|
// new String[]{entity.getWorkingOfTheDetectorImage(), "安装完成探测器工作图片"},
|
|
|
|
|
// new String[]{entity.getSideLeakageImage(), "测漏图片"},
|
|
|
|
|
// new String[]{entity.getFormSideLeakageImage(), "泡沫水测漏图片"},
|
|
|
|
|
// new String[]{entity.getIgnitionPictureImage(), "点火图片"},
|
|
|
|
|
// new String[]{entity.getInstallThePanoramicImage(), "安装完成全景图片"},
|
|
|
|
|
// new String[]{entity.getOfGasMeterImage(), "燃气表号图片"},
|
|
|
|
|
// new String[]{entity.getWorkOrderImage(), "工单图片"},
|
|
|
|
|
// new String[]{entity.getPunchingImage(), "打孔图片"},
|
|
|
|
|
// new String[]{entity.getFiexImage(), "安装电源线图片"},
|
|
|
|
|
// new String[]{entity.getHouseNumberImage(), "门牌号图片"}
|
|
|
|
|
// );
|
|
|
|
|
//
|
|
|
|
|
// for (String[] group : imageGroups) {
|
|
|
|
|
// String urls = group[0];
|
|
|
|
|
// String title = group[1];
|
|
|
|
|
//
|
|
|
|
|
// if (urls != null && !urls.trim().isEmpty()) {
|
|
|
|
|
// List<String> validUrls = Arrays.stream(urls.split(","))
|
|
|
|
|
// .map(String::trim)
|
|
|
|
|
// .filter(url -> !url.isEmpty() && url.startsWith("http"))
|
|
|
|
|
// .collect(Collectors.toList());
|
|
|
|
|
//
|
|
|
|
|
// if (!validUrls.isEmpty()) {
|
|
|
|
|
// // 处理可能需要跨页的多图片情况
|
|
|
|
|
// if (validUrls.size() > 2) {
|
|
|
|
|
// // 对于多于2张的图片,逐个或分组处理以支持跨页
|
|
|
|
|
// for (int i = 0; i < validUrls.size(); i += 2) { // 每次处理最多2张图片
|
|
|
|
|
// List<String> subList = validUrls.subList(i,
|
|
|
|
|
// Math.min(i + 2, validUrls.size()));
|
|
|
|
|
//
|
|
|
|
|
// List<String> wrappedTitleLines = wrapText(title,LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
|
// float titleHeight = wrappedTitleLines.size() * LINE_HEIGHT;
|
|
|
|
|
// // 计算图片组的自适应高度
|
|
|
|
|
// float imagesHeight = calculateAdjustedImagesHeight(document, subList,
|
|
|
|
|
// RIGHT_COL_WIDTH - 2 * CELL_PADDING,
|
|
|
|
|
// PDRectangle.A4.getHeight() - 2 * TABLE_MARGIN - titleHeight);
|
|
|
|
|
//
|
|
|
|
|
// float totalHeight = Math.max(imagesHeight, titleHeight);
|
|
|
|
|
//
|
|
|
|
|
// if (currentY < TABLE_MARGIN + Math.max(totalHeight, titleHeight)) {
|
|
|
|
|
// cs.close();
|
|
|
|
|
// currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
|
// document.addPage(currentPage);
|
|
|
|
|
// cs = new PDPageContentStream(document, currentPage);
|
|
|
|
|
// currentY = PDRectangle.A4.getHeight() - TABLE_MARGIN;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // 添加标识,表明这是多图片的一部分
|
|
|
|
|
// String subTitle = title + " (" + (i+1) + "-" + Math.min(i+2, validUrls.size()) + "/" + validUrls.size() + ")";
|
|
|
|
|
// List<String> subWrappedTitleLines = wrapText(subTitle, LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
|
//
|
|
|
|
|
// drawImageGroup(cs, document, font, subWrappedTitleLines, subList, currentY,
|
|
|
|
|
// Math.max(totalHeight, titleHeight));
|
|
|
|
|
// currentY -= Math.max(totalHeight, titleHeight);
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// // 原有的单/双图片处理逻辑
|
|
|
|
|
// List<String> wrappedTitleLines = wrapText(title,LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
|
// float titleHeight = wrappedTitleLines.size() * LINE_HEIGHT;
|
|
|
|
|
// // 计算图片组的自适应高度
|
|
|
|
|
// float imagesHeight = calculateAdjustedImagesHeight(document, validUrls,
|
|
|
|
|
// RIGHT_COL_WIDTH - 2 * CELL_PADDING,
|
|
|
|
|
// PDRectangle.A4.getHeight() - 2 * TABLE_MARGIN - titleHeight);
|
|
|
|
|
//
|
|
|
|
|
// float totalHeight = Math.max(imagesHeight, titleHeight);
|
|
|
|
|
//
|
|
|
|
|
// if (currentY < TABLE_MARGIN + Math.max(totalHeight, titleHeight)) {
|
|
|
|
|
// cs.close();
|
|
|
|
|
// currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
|
// document.addPage(currentPage);
|
|
|
|
|
// cs = new PDPageContentStream(document, currentPage);
|
|
|
|
|
// currentY = PDRectangle.A4.getHeight() - TABLE_MARGIN;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// drawImageGroup(cs, document, font, wrappedTitleLines, validUrls, currentY,
|
|
|
|
|
// Math.max(totalHeight, titleHeight));
|
|
|
|
|
// currentY -= Math.max(totalHeight, titleHeight);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
////
|
|
|
|
|
// } finally {
|
|
|
|
|
// cs.close();
|
|
|
|
|
// }
|
|
|
|
|
// // 将PDF保存到ByteArrayOutputStream
|
|
|
|
|
// document.save(pdfBaos);
|
|
|
|
|
//
|
|
|
|
|
//// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
//// document.save(baos);
|
|
|
|
|
//// outputStream.flush();
|
|
|
|
|
//// baos.writeTo(outputStream);
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// // 创建ZIP压缩包并将PDF写入输出流
|
|
|
|
|
// try (ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
|
|
|
|
|
// // 设置最高压缩级别
|
|
|
|
|
// zipOut.setLevel(Deflater.BEST_COMPRESSION);
|
|
|
|
|
// // 创建ZIP条目
|
|
|
|
|
// ZipEntry entry = new ZipEntry(entity.getUserName()+entity.getCommunityName()+entity.getBuildingUnit()+entity.getRoomNo()+".pdf");
|
|
|
|
|
// zipOut.putNextEntry(entry);
|
|
|
|
|
//
|
|
|
|
|
// // 将PDF数据写入ZIP
|
|
|
|
|
// byte[] pdfBytes = pdfBaos.toByteArray();
|
|
|
|
|
// zipOut.write(pdfBytes, 0, pdfBytes.length);
|
|
|
|
|
//
|
|
|
|
|
// // 关闭当前条目
|
|
|
|
|
// zipOut.closeEntry();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// 加载字体,确保使用正确的文档实例
|
|
|
|
|
private static PDType0Font loadFont(PDDocument document) throws IOException {
|
|
|
|
|
// 尝试多个可能的字体路径
|
|
|
|
|
@ -855,6 +1012,7 @@ public class PDFGenerator {
|
|
|
|
|
currentY -= fontSize * 1.2f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 图片高度预估方法
|
|
|
|
|
private static float estimateImageHeight(String imageUrl, float targetWidth) throws IOException {
|
|
|
|
|
try (InputStream is = new URL(imageUrl).openStream()) {
|
|
|
|
|
@ -992,8 +1150,6 @@ public class PDFGenerator {
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void drawTable(PDPageContentStream cs, PDType0Font font,
|
|
|
|
|
float x, float y, String[][] data, float[] colWidths) throws IOException {
|
|
|
|
|
|
|
|
|
|
|