feat: bug-779 生产订单入库需要可多个生产订单合并打印
This commit is contained in:
parent
924bf4c5ff
commit
04b68647b3
|
|
@ -31,6 +31,7 @@ import jakarta.annotation.Resource;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
|
@ -304,23 +305,34 @@ public class InProduceOrderController extends BaseController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 导出报工单PDF
|
* 导出报工单PDF(单个)
|
||||||
* @param id 订单id
|
* @param id 订单id
|
||||||
*/
|
*/
|
||||||
@GetMapping("exportOrderPdf")
|
@GetMapping("exportOrderPdf")
|
||||||
public void exportDeliverOrder(HttpServletResponse response, @Valid @RequestParam @NotNull Long id) throws Exception {
|
public void exportDeliverOrder(HttpServletResponse response, @Valid @RequestParam @NotNull Long id) throws Exception {
|
||||||
|
PdfGeneratorUtil.generatePdf("生产入库单", exportHtml(id), response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出报工单PDF(批量)
|
||||||
|
* @param ids 订单id列表
|
||||||
|
*/
|
||||||
|
@PostMapping("exportOrderPdf1")
|
||||||
|
public void exportDeliverOrder1(HttpServletResponse response, @Valid @RequestBody @NotEmpty List<Long> ids) throws Exception {
|
||||||
|
List<String> htmls = new ArrayList<>();
|
||||||
|
for (Long id : ids) {
|
||||||
|
htmls.add(exportHtml(id));
|
||||||
|
}
|
||||||
|
PdfGeneratorUtil.generatePdf("生产入库单", htmls, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String exportHtml(Long id) {
|
||||||
WmsInProduceOrder order = produceOrderService.getById(id);
|
WmsInProduceOrder order = produceOrderService.getById(id);
|
||||||
VUtil.trueThrowBusinessError(Objects.isNull(order)).throwMessage("订单不存在");
|
VUtil.trueThrowBusinessError(Objects.isNull(order)).throwMessage("订单不存在");
|
||||||
List<InProduceOrderItemVO> list = produceOrderItemService.getVOByOrderId(id);
|
List<InProduceOrderItemVO> list = produceOrderItemService.getVOByOrderId(id);
|
||||||
if (order.getList()) {
|
if (order.getList()) {
|
||||||
list.removeIf(item -> Objects.equals(item.getParentId(), 0L));
|
list.removeIf(item -> Objects.equals(item.getParentId(), 0L));
|
||||||
}
|
}
|
||||||
// WmsInProduceOrder order = new WmsInProduceOrder()
|
|
||||||
// .setNo(NoUtil.getInProduceNo());
|
|
||||||
// List<InProduceOrderItemVO> list = new ArrayList<>();
|
|
||||||
// for (int i = 0, count = RandomUtil.randomInt(1, 100); i < count; i++) {
|
|
||||||
// list.add(new InProduceOrderItemVO().setMaterialNo(RandomUtil.randomNumbers(10)).setMaterialDesc("非金属联件和紧固件等 M6 DIN 982 不锈钢304"));
|
|
||||||
// }
|
|
||||||
InProduceOrderItemVO first = list.get(0);
|
InProduceOrderItemVO first = list.get(0);
|
||||||
Map<String, String> base = new HashMap<>();
|
Map<String, String> base = new HashMap<>();
|
||||||
base.put("date", DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd"));
|
base.put("date", DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd"));
|
||||||
|
|
@ -331,8 +343,7 @@ public class InProduceOrderController extends BaseController {
|
||||||
variables.put("info", order);
|
variables.put("info", order);
|
||||||
variables.put("base", base);
|
variables.put("base", base);
|
||||||
variables.put("pages", PdfPageDTO.create(list, 17, new InProduceOrderItemVO()));
|
variables.put("pages", PdfPageDTO.create(list, 17, new InProduceOrderItemVO()));
|
||||||
String html = ThymeleafUtil.generator("/template/", "生产入库单", ".html", variables);
|
return ThymeleafUtil.generator("/template/", "生产入库单", ".html", variables);
|
||||||
PdfGeneratorUtil.generatePdf("生产入库单" + order.getNo(), html, response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,9 @@
|
||||||
package com.nflg.wms.admin.util;
|
package com.nflg.wms.admin.util;
|
||||||
|
|
||||||
|
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfReader;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||||
|
import com.itextpdf.kernel.utils.PdfMerger;
|
||||||
import com.lowagie.text.pdf.BaseFont;
|
import com.lowagie.text.pdf.BaseFont;
|
||||||
import com.nflg.wms.common.util.VUtil;
|
import com.nflg.wms.common.util.VUtil;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
@ -9,19 +13,14 @@ import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.xhtmlrenderer.pdf.ITextRenderer;
|
import org.xhtmlrenderer.pdf.ITextRenderer;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class PdfGeneratorUtil {
|
public class PdfGeneratorUtil {
|
||||||
|
|
||||||
|
|
@ -58,6 +57,37 @@ public class PdfGeneratorUtil {
|
||||||
renderer.createPDF(output);
|
renderer.createPDF(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void generatePdf(String name, List<String> htmls, HttpServletResponse response) throws Exception {
|
||||||
|
response.setContentType(MediaType.APPLICATION_PDF_VALUE);
|
||||||
|
String encode = URLEncoder.encode(name + ".pdf", StandardCharsets.UTF_8);
|
||||||
|
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline;filename*=UTF-8''" + encode);
|
||||||
|
URL baseUrl = new ClassPathResource("template/").getURL();
|
||||||
|
List<byte[]> pdfParts = new ArrayList<>();
|
||||||
|
for (String html : htmls) {
|
||||||
|
ITextRenderer renderer = new ITextRenderer();
|
||||||
|
loadFonts(renderer);
|
||||||
|
renderer.setDocumentFromString(html, baseUrl.toString());
|
||||||
|
renderer.layout();
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
renderer.createPDF(baos);
|
||||||
|
pdfParts.add(baos.toByteArray());
|
||||||
|
baos.close();
|
||||||
|
renderer.finishPDF();
|
||||||
|
}
|
||||||
|
mergePdfs(pdfParts, response.getOutputStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void mergePdfs(List<byte[]> pdfBytesList, OutputStream outputStream) throws IOException {
|
||||||
|
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream));
|
||||||
|
PdfMerger merger = new PdfMerger(pdfDoc);
|
||||||
|
for (byte[] bytes : pdfBytesList) {
|
||||||
|
PdfDocument srcDoc = new PdfDocument(new PdfReader(new ByteArrayInputStream(bytes)));
|
||||||
|
merger.merge(srcDoc, 1, srcDoc.getNumberOfPages());
|
||||||
|
srcDoc.close();
|
||||||
|
}
|
||||||
|
pdfDoc.close();
|
||||||
|
}
|
||||||
|
|
||||||
private static void loadFonts(ITextRenderer renderer) throws IOException {
|
private static void loadFonts(ITextRenderer renderer) throws IOException {
|
||||||
Path fontsDir = Paths.get(PathUtils.getPath(), "fonts");
|
Path fontsDir = Paths.get(PathUtils.getPath(), "fonts");
|
||||||
VUtil.trueThrowBusinessError(!Files.exists(fontsDir) || !Files.isDirectory(fontsDir))
|
VUtil.trueThrowBusinessError(!Files.exists(fontsDir) || !Files.isDirectory(fontsDir))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue