Showing
4 changed files
with
41 additions
and
6 deletions
| @@ -61,6 +61,8 @@ import java.util.stream.Collectors; | @@ -61,6 +61,8 @@ import java.util.stream.Collectors; | ||
| 61 | import java.util.zip.ZipEntry; | 61 | import java.util.zip.ZipEntry; |
| 62 | import java.util.zip.ZipOutputStream; | 62 | import java.util.zip.ZipOutputStream; |
| 63 | 63 | ||
| 64 | +import static com.lframework.xingyun.sc.utils.ExcelUtil.convertExcelToPdf; | ||
| 65 | + | ||
| 64 | /** | 66 | /** |
| 65 | * 发货单 Controller | 67 | * 发货单 Controller |
| 66 | * | 68 | * |
| @@ -307,8 +309,8 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { | @@ -307,8 +309,8 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { | ||
| 307 | // 按发货单ID分组明细数据 | 309 | // 按发货单ID分组明细数据 |
| 308 | Map<String, List<ShipmentsPlanDetail>> shipmentsOrderDetailMap = detailList.stream() | 310 | Map<String, List<ShipmentsPlanDetail>> shipmentsOrderDetailMap = detailList.stream() |
| 309 | .collect(Collectors.groupingBy(ShipmentsPlanDetail::getShipmentOrderId)); | 311 | .collect(Collectors.groupingBy(ShipmentsPlanDetail::getShipmentOrderId)); |
| 310 | - // 为每个发货单生成Excel文件 | ||
| 311 | - List<File> excelFiles = new ArrayList<>(); | 312 | + // 为每个发货单生成pdf文件 |
| 313 | + List<File> pdfFiles = new ArrayList<>(); | ||
| 312 | for (ShipmentsOrderInfo orderInfo : orderInfoList) { | 314 | for (ShipmentsOrderInfo orderInfo : orderInfoList) { |
| 313 | String id = orderInfo.getId(); | 315 | String id = orderInfo.getId(); |
| 314 | List<ShipmentsPlanDetail> details = shipmentsOrderDetailMap.get(id); | 316 | List<ShipmentsPlanDetail> details = shipmentsOrderDetailMap.get(id); |
| @@ -317,12 +319,37 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { | @@ -317,12 +319,37 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { | ||
| 317 | } | 319 | } |
| 318 | // 生成单个Excel文件(使用模板引擎) | 320 | // 生成单个Excel文件(使用模板引擎) |
| 319 | File excelFile = generateExcelWithTemplate(orderInfo, details, templatePath, tempDir, vo.getType()); | 321 | File excelFile = generateExcelWithTemplate(orderInfo, details, templatePath, tempDir, vo.getType()); |
| 320 | - if (excelFile != null) { | ||
| 321 | - excelFiles.add(excelFile); | 322 | + // === 5. 调用 LibreOffice 转 PDF === |
| 323 | + try { | ||
| 324 | + // 2. 转换为 PDF | ||
| 325 | + File pdfFile = convertExcelToPdf(excelFile, "/usr/bin/libreoffice --headless --convert-to pdf --outdir %s %s"); | ||
| 326 | + | ||
| 327 | + // 【关键修改 2】严格检查:必须是 PDF 且存在,否则报错 | ||
| 328 | + if (pdfFile != null && pdfFile.exists() && pdfFile.getName().endsWith(".pdf")) { | ||
| 329 | + pdfFiles.add(pdfFile); // 只添加 PDF | ||
| 330 | + | ||
| 331 | + // 【关键修改 3】立即删除临时 Excel,防止残留干扰 | ||
| 332 | + excelFile.delete(); | ||
| 333 | + log.info("发货单 [{}] 转换成功,已加入 PDF 列表,临时 Excel 已删除", id); | ||
| 334 | + } else { | ||
| 335 | + // 如果转换失败,直接抛异常,阻止生成包含错误文件的 ZIP | ||
| 336 | + log.error("发货单 [{}] 转换 PDF 失败。文件存在: {}, 文件名: {}", | ||
| 337 | + id, (pdfFile != null && pdfFile.exists()), (pdfFile != null ? pdfFile.getName() : "null")); | ||
| 338 | + throw new DefaultClientException("发货单 [" + id + "] 生成 PDF 失败,请检查服务端 LibreOffice 状态。"); | ||
| 339 | + } | ||
| 340 | + } catch (Exception e) { | ||
| 341 | + // 发生异常时,尝试清理当前生成的 excelFile | ||
| 342 | + if (excelFile != null && excelFile.exists()) { | ||
| 343 | + excelFile.delete(); | ||
| 344 | + } | ||
| 345 | + throw e; | ||
| 322 | } | 346 | } |
| 323 | } | 347 | } |
| 348 | + if (pdfFiles.isEmpty()) { | ||
| 349 | + throw new DefaultClientException("没有成功生成任何 PDF 文件。"); | ||
| 350 | + } | ||
| 324 | // 创建ZIP压缩包 | 351 | // 创建ZIP压缩包 |
| 325 | - zipFile = createZipFile(excelFiles, tempDir); | 352 | + zipFile = createZipFile(pdfFiles, tempDir); |
| 326 | // 将ZIP文件写入响应流 | 353 | // 将ZIP文件写入响应流 |
| 327 | writeZipToResponse(zipFile, response); | 354 | writeZipToResponse(zipFile, response); |
| 328 | } catch (FileNotFoundException e) { | 355 | } catch (FileNotFoundException e) { |
| @@ -428,6 +455,14 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { | @@ -428,6 +455,14 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { | ||
| 428 | } | 455 | } |
| 429 | // 准备模板数据 | 456 | // 准备模板数据 |
| 430 | Map<String, Object> dataMap = JsonUtil.parseMap(JsonUtil.toJsonString(orderInfo), String.class, Object.class); | 457 | Map<String, Object> dataMap = JsonUtil.parseMap(JsonUtil.toJsonString(orderInfo), String.class, Object.class); |
| 458 | + String orderId = detailList.get(0).getOrderId(); | ||
| 459 | + //开票要求 | ||
| 460 | + if (StringUtils.isNotEmpty(orderId)) { | ||
| 461 | + PurchaseOrderInfo info = purchaseOrderInfoService.findById(orderId); | ||
| 462 | + dataMap.put("invoicingStatus", info.getInvoicingStatus() == null ? "" : info.getInvoicingStatus()); | ||
| 463 | + } else { | ||
| 464 | + dataMap.put("invoicingStatus", ""); | ||
| 465 | + } | ||
| 431 | dataMap.put("createDate", dateFormatter.format(orderInfo.getCreateTime())); | 466 | dataMap.put("createDate", dateFormatter.format(orderInfo.getCreateTime())); |
| 432 | SysUser user = sysUserService.findById(orderInfo.getCreateById()); | 467 | SysUser user = sysUserService.findById(orderInfo.getCreateById()); |
| 433 | dataMap.put("createBy", user.getName()); | 468 | dataMap.put("createBy", user.getName()); |
| @@ -255,7 +255,7 @@ public class ExcelUtil { | @@ -255,7 +255,7 @@ public class ExcelUtil { | ||
| 255 | if (!excelFile.exists()) { | 255 | if (!excelFile.exists()) { |
| 256 | throw new IllegalArgumentException("Excel 文件不存在: " + excelFile.getAbsolutePath()); | 256 | throw new IllegalArgumentException("Excel 文件不存在: " + excelFile.getAbsolutePath()); |
| 257 | } | 257 | } |
| 258 | - String pdfPath = excelFile.getAbsolutePath().replaceAll("\\.xls$", ".pdf"); | 258 | + String pdfPath = excelFile.getAbsolutePath().replaceAll("\\.xlsx?$", ".pdf"); |
| 259 | File pdfFile = new File(pdfPath); | 259 | File pdfFile = new File(pdfPath); |
| 260 | // 使用绝对路径 /usr/bin/libreoffice(CentOS 7 默认位置) | 260 | // 使用绝对路径 /usr/bin/libreoffice(CentOS 7 默认位置) |
| 261 | String command = String.format( | 261 | String command = String.format( |
No preview for this file type
No preview for this file type