Commit 0f28908e8dc96f0472e12028062fee8776d7e9b9

Authored by 房远帅
1 parent fee18ff4

订货单:改成导出pdf+excel

@@ -26,7 +26,9 @@ import com.lframework.xingyun.sc.handlers.TransactorHandler; @@ -26,7 +26,9 @@ import com.lframework.xingyun.sc.handlers.TransactorHandler;
26 import com.lframework.xingyun.sc.service.contract.ContractDistributorStandardService; 26 import com.lframework.xingyun.sc.service.contract.ContractDistributorStandardService;
27 import com.lframework.xingyun.sc.service.order.PurchaseOrderInfoService; 27 import com.lframework.xingyun.sc.service.order.PurchaseOrderInfoService;
28 import com.lframework.xingyun.sc.service.order.PurchaseOrderLineService; 28 import com.lframework.xingyun.sc.service.order.PurchaseOrderLineService;
  29 +import com.lframework.xingyun.sc.utils.ExcelUtil;
29 import com.lframework.xingyun.sc.utils.LatexFormulaExcelExporterUtil; 30 import com.lframework.xingyun.sc.utils.LatexFormulaExcelExporterUtil;
  31 +import com.lframework.xingyun.sc.utils.ResponseUtil;
30 import com.lframework.xingyun.sc.vo.order.CreatePurchaseOrderInfoVo; 32 import com.lframework.xingyun.sc.vo.order.CreatePurchaseOrderInfoVo;
31 import com.lframework.xingyun.sc.vo.order.QueryPurchaseOrderInfoVo; 33 import com.lframework.xingyun.sc.vo.order.QueryPurchaseOrderInfoVo;
32 import com.lframework.xingyun.sc.vo.order.QueryPurchaseOrderLineVo; 34 import com.lframework.xingyun.sc.vo.order.QueryPurchaseOrderLineVo;
@@ -355,16 +357,13 @@ public class PurchaseOrderInfoController extends DefaultBaseController { @@ -355,16 +357,13 @@ public class PurchaseOrderInfoController extends DefaultBaseController {
355 357
356 @ApiOperation("订货单打印") 358 @ApiOperation("订货单打印")
357 @GetMapping("/printPurchaseOrder") 359 @GetMapping("/printPurchaseOrder")
358 - public void printPurchaseOrder(@NotBlank(message = "id不能为空") String id, HttpServletResponse response) throws IOException { 360 + public void printPurchaseOrder(@NotBlank(message = "id不能为空") String id, String exportType, HttpServletResponse response) throws IOException {
359 PurchaseOrderInfo data = purchaseOrderInfoService.findById(id); 361 PurchaseOrderInfo data = purchaseOrderInfoService.findById(id);
360 362
361 Wrapper<PurchaseOrderLine> purchaseOrderLineWrapper = Wrappers.lambdaQuery(PurchaseOrderLine.class) 363 Wrapper<PurchaseOrderLine> purchaseOrderLineWrapper = Wrappers.lambdaQuery(PurchaseOrderLine.class)
362 .eq(PurchaseOrderLine::getPurchaseOrderId, id); 364 .eq(PurchaseOrderLine::getPurchaseOrderId, id);
363 List<PurchaseOrderLine> purchaseOrderLineList = purchaseOrderLineService.list(purchaseOrderLineWrapper); 365 List<PurchaseOrderLine> purchaseOrderLineList = purchaseOrderLineService.list(purchaseOrderLineWrapper);
364 366
365 - File tempExcel = null;  
366 - File pdfFile = null;  
367 -  
368 try { 367 try {
369 // 加载模板文件 368 // 加载模板文件
370 ClassPathResource templateResource = new ClassPathResource("templates/purchaseOrderTemplate.xlsx"); 369 ClassPathResource templateResource = new ClassPathResource("templates/purchaseOrderTemplate.xlsx");
@@ -466,51 +465,53 @@ public class PurchaseOrderInfoController extends DefaultBaseController { @@ -466,51 +465,53 @@ public class PurchaseOrderInfoController extends DefaultBaseController {
466 465
467 processTemplate(workbook, dataMap); 466 processTemplate(workbook, dataMap);
468 467
469 - // === 4. 写入临时 .xlsx 文件 ===  
470 - tempExcel = File.createTempFile("purchase_order_" + data.getOrderNo(), ".xlsx");  
471 - try (FileOutputStream fos = new FileOutputStream(tempExcel)) {  
472 - workbook.write(fos); 468 + if ("PDF".equals(exportType)) {
  469 + ResponseUtil.setPDFResponseHead(response, data.getOrderNo() + "-订货单打印.pdf");
  470 +
  471 + // === 写入临时 .xlsx 文件 ===
  472 + File tempExcel = File.createTempFile("purchase_order_" + data.getOrderNo(), ".xlsx");
  473 + try (FileOutputStream fos = new FileOutputStream(tempExcel)) {
  474 + workbook.write(fos);
  475 + }
  476 +
  477 + // === 调用 LibreOffice 转 PDF ===
  478 + File pdfFile = ExcelUtil.convertExcelToPdf(tempExcel, "/usr/bin/libreoffice --headless --convert-to pdf --outdir %s %s");
  479 +
  480 + // === 输出 PDF 到浏览器 ===
  481 + try (InputStream pdfIn = new FileInputStream(pdfFile)) {
  482 + byte[] buffer = new byte[8192];
  483 + int len;
  484 + while ((len = pdfIn.read(buffer)) != -1) {
  485 + response.getOutputStream().write(buffer, 0, len);
  486 + }
  487 + response.getOutputStream().flush();
  488 + } finally {
  489 + if (tempExcel != null && tempExcel.exists()) {
  490 + Files.delete(tempExcel.toPath());
  491 + }
  492 + if (pdfFile != null && pdfFile.exists()) {
  493 + Files.delete(pdfFile.toPath());
  494 + }
  495 + }
  496 + } else {
  497 + ResponseUtil.setExcelResponseHead(response, data.getOrderNo() + "-订货单打印.xlsx");
  498 + // 写入响应流
  499 + workbook.write(response.getOutputStream());
  500 + response.getOutputStream().flush();
473 } 501 }
474 502
475 } finally { 503 } finally {
476 IOUtils.closeQuietly(workbook); 504 IOUtils.closeQuietly(workbook);
477 } 505 }
478 - }  
479 506
480 - // === 5. 调用 LibreOffice 转 PDF ===  
481 - pdfFile = convertExcelToPdf(tempExcel);  
482 -  
483 - // === 6. 设置 PDF 响应头 ===  
484 - String fileName = data.getOrderNo() + "-订货单打印.pdf";  
485 - String encodedFileName;  
486 - try {  
487 - encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");  
488 - } catch (UnsupportedEncodingException e) {  
489 - // UTF-8 是标准编码,理论上不会抛出,但 Java 8 要求处理  
490 - throw new RuntimeException("UTF-8 encoding not supported", e);  
491 - }  
492 - response.setContentType("application/pdf");  
493 - response.setCharacterEncoding("UTF-8");  
494 - response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName);  
495 - response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");  
496 -  
497 - // === 7. 输出 PDF 到浏览器 ===  
498 - try (InputStream pdfIn = new FileInputStream(pdfFile)) {  
499 - byte[] buffer = new byte[8192];  
500 - int len;  
501 - while ((len = pdfIn.read(buffer)) != -1) {  
502 - response.getOutputStream().write(buffer, 0, len);  
503 - }  
504 - response.getOutputStream().flush(); 507 + } catch (FileNotFoundException e) {
  508 + throw new RuntimeException("模板文件不存在: templates/purchaseOrderTemplate.xlsx", e);
  509 + } catch (IOException e) {
  510 + throw new RuntimeException("无法读取模板文件: templates/purchaseOrderTemplate.xlsx", e);
505 } 511 }
506 -  
507 } catch (Exception e) { 512 } catch (Exception e) {
508 - log.error("订货单导出 PDF 失败: {}", e.getMessage(), e);  
509 - throw new RuntimeException("订货单导出失败,请联系管理员", e);  
510 - } finally {  
511 - // === 8. 清理临时文件 ===  
512 - deleteQuietly(tempExcel);  
513 - deleteQuietly(pdfFile); 513 + log.error("订货单导出失败: {}", e.getMessage(), e);
  514 + throw new RuntimeException("订货单导出失败:", e);
514 } 515 }
515 } 516 }
516 517