优化
This commit is contained in:
@@ -23,9 +23,9 @@ public class FbaPalletRenderer {
|
||||
PdfUnits units = new PdfUnits(210);
|
||||
try (PDPageContentStream stream = new PDPageContentStream(pdf, page)) {
|
||||
stream.setNonStrokingColor(0, 0, 0);
|
||||
centered(stream, units, font, label.fbaWarehouseCode(), 96, 49);
|
||||
barcodeRenderer.draw(stream, barcode, PdfUnits.mmToPt(33.5), units.topDownMmToPdfY(140),
|
||||
PdfUnits.mmToPt(230), PdfUnits.mmToPt(55));
|
||||
centered(stream, units, font, label.fbaWarehouseCode(), 280, 60);
|
||||
barcodeRenderer.draw(stream, barcode, PdfUnits.mmToPt(33.5), units.topDownMmToPdfY(146),
|
||||
PdfUnits.mmToPt(230), PdfUnits.mmToPt(30));
|
||||
centered(stream, units, font, label.palletCode(), 64, 174);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,29 @@ info:
|
||||
version: 1.0.0
|
||||
description: Internal stateless PDF rendering API. This path must not be exposed through Gateway.
|
||||
paths:
|
||||
/internal/print/v1/pdfs/fba-pallet-labels:
|
||||
post:
|
||||
summary: Generate shared domestic and overseas FBA pallet labels as landscape A4 pages
|
||||
operationId: generateFbaPalletLabels
|
||||
description: One label per page in input order. The destination is the FBA code such as SCK8; the stored pallet code (including internal warehouse number such as 141) is printed verbatim and encoded as Code 128. No business lookup or new numbering.
|
||||
security: [{ internalServiceToken: [] }]
|
||||
parameters: [{ $ref: '#/components/parameters/RequestId' }]
|
||||
requestBody:
|
||||
required: true
|
||||
content: { application/json: { schema: { $ref: '#/components/schemas/FbaPalletPdfRequest' } } }
|
||||
responses:
|
||||
'200':
|
||||
description: PDF generation succeeded, or a business error was returned as ApiResponse JSON.
|
||||
headers:
|
||||
Content-Disposition: { schema: { type: string } }
|
||||
Cache-Control: { schema: { type: string, example: no-store } }
|
||||
X-Print-Page-Count: { schema: { type: integer, minimum: 1, maximum: 2000 } }
|
||||
content:
|
||||
application/pdf: { schema: { type: string, format: binary } }
|
||||
application/json: { schema: { $ref: '#/components/schemas/ApiError' } }
|
||||
'413':
|
||||
description: Request body exceeds the configured limit.
|
||||
content: { application/json: { schema: { $ref: '#/components/schemas/ApiError' } } }
|
||||
/internal/print/v1/pdfs/inbound-marks:
|
||||
post:
|
||||
summary: Generate an inbound-mark PDF
|
||||
@@ -153,6 +176,28 @@ components:
|
||||
schema:
|
||||
type: string
|
||||
schemas:
|
||||
FbaPalletPdfRequest:
|
||||
type: object
|
||||
required: [templateCode, pageSpec, downloadFileName, labels]
|
||||
properties:
|
||||
templateCode: { type: string, enum: [FBA_PALLET_A4_LANDSCAPE_V1] }
|
||||
pageSpec:
|
||||
type: object
|
||||
required: [widthMm, heightMm]
|
||||
properties:
|
||||
widthMm: { type: number, enum: [297] }
|
||||
heightMm: { type: number, enum: [210] }
|
||||
downloadFileName: { type: string, minLength: 4, maxLength: 120, description: PDF filename without path separators or control characters }
|
||||
labels:
|
||||
type: array
|
||||
minItems: 1
|
||||
maxItems: 2000
|
||||
items:
|
||||
type: object
|
||||
required: [fbaWarehouseCode, palletCode]
|
||||
properties:
|
||||
fbaWarehouseCode: { type: string, pattern: '^[A-Z0-9][A-Z0-9-]{0,31}$', example: SCK8 }
|
||||
palletCode: { type: string, minLength: 1, maxLength: 80, description: Printable ASCII with no surrounding spaces; unchanged text and Code 128 payload, example: MSCU6639870-141-001 }
|
||||
InboundMarkPdfRequest:
|
||||
type: object
|
||||
required: [templateCode, pageSpec, labels]
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.baiyue.print.service.FreightA4PdfService;
|
||||
import com.baiyue.print.service.DispatchBolPdfService;
|
||||
import com.baiyue.print.service.DropshipPalletA4PdfService;
|
||||
import com.baiyue.print.service.InboundMarkPdfService;
|
||||
import com.baiyue.print.service.FbaPalletPdfService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -49,6 +50,8 @@ class InternalPdfControllerTest {
|
||||
private DispatchBolPdfService dispatchBolPdfService;
|
||||
@MockBean
|
||||
private DropshipPalletA4PdfService dropshipPalletA4PdfService;
|
||||
@MockBean
|
||||
private FbaPalletPdfService fbaPalletPdfService;
|
||||
@Resource
|
||||
private InternalPdfController controller;
|
||||
private MockMvc mockMvc;
|
||||
@@ -60,6 +63,24 @@ class InternalPdfControllerTest {
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void protectsFbaLabelsAndReturnsPdfHeaders() throws Exception {
|
||||
String json = """
|
||||
{"templateCode":"FBA_PALLET_A4_LANDSCAPE_V1","pageSpec":{"widthMm":297,"heightMm":210},
|
||||
"downloadFileName":"fba.pdf","labels":[{"fbaWarehouseCode":"SCK8","palletCode":"MSCU6639870-141-001"}]}
|
||||
""";
|
||||
mockMvc.perform(post("/internal/print/v1/pdfs/fba-pallet-labels").contentType(MediaType.APPLICATION_JSON).content(json))
|
||||
.andExpect(jsonPath("$.code").value("A0403"));
|
||||
verify(fbaPalletPdfService, never()).generate(any());
|
||||
byte[] pdf = "%PDF-fba".getBytes(StandardCharsets.US_ASCII);
|
||||
when(fbaPalletPdfService.generate(any())).thenReturn(new FbaPalletPdfService.GeneratedPdf(pdf, "fba.pdf", 1));
|
||||
mockMvc.perform(post("/internal/print/v1/pdfs/fba-pallet-labels").header(InternalPdfController.INTERNAL_TOKEN_HEADER, TOKEN)
|
||||
.contentType(MediaType.APPLICATION_JSON).content(json))
|
||||
.andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_PDF))
|
||||
.andExpect(content().bytes(pdf)).andExpect(header().string("X-Print-Page-Count", "1"))
|
||||
.andExpect(header().string("Cache-Control", "no-store"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsPdfWithDownloadHeaders() throws Exception {
|
||||
byte[] pdf = "%PDF-test".getBytes(StandardCharsets.US_ASCII);
|
||||
|
||||
@@ -52,6 +52,16 @@ class PrintRequestSizeFilterTest {
|
||||
assertTooLarge(response);
|
||||
}
|
||||
|
||||
@Test
|
||||
void capsFbaPalletRequestsBeforeDeserialization() throws Exception {
|
||||
var response = new MockHttpServletResponse();
|
||||
AtomicReference<Boolean> called = new AtomicReference<>(false);
|
||||
filter.doFilter(request("12345678901", true, "/internal/print/v1/pdfs/fba-pallet-labels"), response,
|
||||
(ignoredRequest, ignoredResponse) -> called.set(true));
|
||||
assertThat(called).hasValue(false);
|
||||
assertTooLarge(response);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsOversizedDeclaredContentLengthWithoutCallingDownstream() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest() {
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.baiyue.print.service;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import com.baiyue.common.core.exception.BizException;
|
||||
import com.baiyue.print.config.PrintPdfProperties;
|
||||
import com.baiyue.print.dto.FbaPalletPdfDtos;
|
||||
import com.baiyue.print.renderer.FbaPalletRenderer;
|
||||
import com.baiyue.print.renderer.PdfBarcodeRenderer;
|
||||
import com.baiyue.print.renderer.PdfUnits;
|
||||
import com.google.zxing.*;
|
||||
import com.google.zxing.common.HybridBinarizer;
|
||||
import jakarta.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.pdfbox.Loader;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.apache.pdfbox.text.PDFTextStripper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
@SpringJUnitConfig(classes = {FbaPalletPdfService.class, PrintPdfProperties.class,
|
||||
PdfGenerationLimiter.class, FbaPalletRenderer.class, PdfBarcodeRenderer.class})
|
||||
class FbaPalletPdfServiceTest {
|
||||
@Resource private FbaPalletPdfService service;
|
||||
|
||||
@Test
|
||||
void rendersLandscapePagesWithOnlyFbaCodeAndUnchangedDecodablePalletIdentity() throws Exception {
|
||||
var labels = List.of(new FbaPalletPdfDtos.Label("SCK8", "MSCU6639870-141-001"),
|
||||
new FbaPalletPdfDtos.Label("ONT8", "MSCU6639870-CADF-002"),
|
||||
new FbaPalletPdfDtos.Label("SCK8", "A".repeat(80)));
|
||||
var result = service.generate(request(labels));
|
||||
assertThat(result.pageCount()).isEqualTo(3);
|
||||
assertThat(result.fileName()).isEqualTo("FBA托盘标.pdf");
|
||||
try (var pdf = Loader.loadPDF(result.content())) {
|
||||
assertThat(pdf.getNumberOfPages()).isEqualTo(labels.size());
|
||||
var renderer = new PDFRenderer(pdf);
|
||||
for (int i = 0; i < labels.size(); i++) {
|
||||
var page = pdf.getPage(i);
|
||||
assertThat(page.getMediaBox().getWidth()).isCloseTo(PdfUnits.mmToPt(297), within(.002f));
|
||||
assertThat(page.getMediaBox().getHeight()).isCloseTo(PdfUnits.mmToPt(210), within(.002f));
|
||||
assertThat(page.getRotation()).isZero();
|
||||
var stripper = new PDFTextStripper();
|
||||
stripper.setStartPage(i + 1); stripper.setEndPage(i + 1);
|
||||
assertThat(stripper.getText(pdf).strip().lines().toList())
|
||||
.containsExactly(labels.get(i).fbaWarehouseCode(), labels.get(i).palletCode());
|
||||
for (var name : page.getResources().getFontNames()) {
|
||||
assertThat(page.getResources().getFont(name).isEmbedded()).isTrue();
|
||||
}
|
||||
var image = renderer.renderImageWithDPI(i, 300);
|
||||
var source = new RGBLuminanceSource(image.getWidth(), image.getHeight(),
|
||||
image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth()));
|
||||
var decoded = new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(source)),
|
||||
Map.of(DecodeHintType.POSSIBLE_FORMATS, List.of(BarcodeFormat.CODE_128)));
|
||||
assertThat(decoded.getText()).isEqualTo(labels.get(i).palletCode());
|
||||
assertThat(decoded.getBarcodeFormat()).isEqualTo(BarcodeFormat.CODE_128);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsInvalidIdentifiersWithoutChangingThem() {
|
||||
for (String code : new String[] {"", "托盘001", "A".repeat(81), " PALLET", "PALLET\n"}) {
|
||||
assertThatThrownBy(() -> service.generate(request(List.of(new FbaPalletPdfDtos.Label("SCK8", code)))))
|
||||
.isInstanceOf(BizException.class).hasMessageContaining("palletCode");
|
||||
}
|
||||
assertThatThrownBy(() -> service.generate(request(List.of(new FbaPalletPdfDtos.Label(null, "MSCU6639870-141-001")))))
|
||||
.isInstanceOf(BizException.class).hasMessageContaining("fbaWarehouseCode");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsPortraitWrongTemplateEmptyBatchAndUnsafeFilename() {
|
||||
var valid = request(List.of(new FbaPalletPdfDtos.Label("SCK8", "MSCU6639870-141-001")));
|
||||
assertThatThrownBy(() -> service.generate(new FbaPalletPdfDtos.Request(valid.templateCode(),
|
||||
new FbaPalletPdfDtos.PageSpec(new BigDecimal("210"), new BigDecimal("297")), valid.downloadFileName(), valid.labels())))
|
||||
.isInstanceOf(BizException.class).hasMessageContaining("landscape A4");
|
||||
assertThatThrownBy(() -> service.generate(new FbaPalletPdfDtos.Request("INBOUND_MARK_V1", valid.pageSpec(), valid.downloadFileName(), valid.labels())))
|
||||
.isInstanceOf(BizException.class).hasMessageContaining("templateCode");
|
||||
assertThatThrownBy(() -> service.generate(request(List.of())))
|
||||
.isInstanceOf(BizException.class).hasMessageContaining("labels count");
|
||||
assertThatThrownBy(() -> service.generate(new FbaPalletPdfDtos.Request(valid.templateCode(), valid.pageSpec(), "../label.pdf", valid.labels())))
|
||||
.isInstanceOf(BizException.class).hasMessageContaining("downloadFileName");
|
||||
}
|
||||
|
||||
private FbaPalletPdfDtos.Request request(List<FbaPalletPdfDtos.Label> labels) {
|
||||
return new FbaPalletPdfDtos.Request(FbaPalletPdfDtos.TEMPLATE,
|
||||
new FbaPalletPdfDtos.PageSpec(new BigDecimal("297"), new BigDecimal("210")), "FBA托盘标.pdf", labels);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user