依赖
<!-- pdf实现 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
Controller
public void epsOtderPrint(@PathVariable List<String> uuids, HttpServletRequest request, HttpServletResponse response) throws Exception {
int printTime = 0;
Resource font = new ClassPathResource("static/simsun.ttc");
String f = font.getURI().toString();
String fonts = URLDecoder.decode(f, String.valueOf(StandardCharsets.UTF_8));
BaseFont bfChinese = BaseFont.createFont(fonts + ",0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
OutputStream os = response.getOutputStream();
response.setContentType("application/pdf");
Document document = new Document(PageSize.A4, 20, 20, 20, 5);
PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
// 小四字体
Font xiaosi = new Font(bfChinese, 12);
// 五号字体
Font wuhao = new Font(bfChinese, (float) 10.5);
// 小五字体
Font xiaowu = new Font(bfChinese, 9);
document.open();
//获取要货单单头信息
for (int k = 0; k < uuids.size(); k++) {
String mstUuid = uuids.get(k);
PurchaseOrderMst orderMst = Optional.ofNullable(mstService.getById(mstUuid)).orElseThrow(() -> new ResourceNotFoundException(PurchaseOrderMst.class, mstUuid));
//获取要货人和要货单位
Map map = mstService.findEmplNameAndCompanyByUuid(orderMst.getUuid());
//通过单头uuid获取全部的要货单身
List<PurchaseOrderDtl> orderDtls = dtlService.list(Wrappers.<PurchaseOrderDtl>lambdaQuery().eq(PurchaseOrderDtl::getPurchaseOrderMstUuid, mstUuid));
// 打印次数默认为2
int printTimes = 2;
for (int j = 0; j < printTimes; j++) {
if (printTime != 0 && printTime % 2 == 0) {
document.newPage();
}
if (printTime != 0 && printTime % 2 == 1) {
Paragraph ph = new Paragraph();
// LineSeparator lineSeparator = new LineSeparator();
// lineSeparator.setPercentage();
ph.add(new DottedLineSeparator());
ph.setSpacingBefore(30);
ph.setSpacingAfter(35);
document.add(ph);
}
printTime = printTime + 1;
PdfPTable tab1 = new PdfPTable(1);
int[] width1 = {100};
tab1.setWidths(width1);
tab1.setWidthPercentage(100); //宽度百分比
tab1.setSpacingAfter(-15); //后间距
PdfPCell pdfPCell11 = new PdfPCell(new Paragraph(" XXXX有限公司要货计划单 ", xiaosi));
pdfPCell11.setColspan(1); //合并单元格
pdfPCell11.setHorizontalAlignment(Element.ALIGN_CENTER); //水平对齐
pdfPCell11.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直对齐
pdfPCell11.setBorder(0); //边框
tab1.addCell(pdfPCell11);
document.add(tab1);
PdfPTable tab2 = new PdfPTable(1);
int[] width2 = {100};
tab2.setWidths(width2);
tab2.setWidthPercentage(100);
PdfPCell pdfPCell21 = new PdfPCell(new Paragraph("No:" + orderMst.getNo(), xiaowu));
pdfPCell21.setColspan(1);
pdfPCell21.setHorizontalAlignment(Element.ALIGN_RIGHT);
pdfPCell21.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell21.setBorder(0);
tab2.addCell(pdfPCell21);
document.add(tab2);
PdfPTable tab3 = new PdfPTable(2);
int[] width3 = {60, 40};
tab3.setWidths(width3);
tab3.setWidthPercentage(100);
tab3.setSpacingBefore(30);
tab3.setSpacingAfter(-10);
PdfPCell pdfPCell31 = new PdfPCell(new Paragraph(new SimpleDateFormat("yyyy年MM月dd日").format(orderMst.getDeliveryDate()), wuhao));
pdfPCell31.setColspan(1);
pdfPCell31.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell31.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell31.setBorder(0);
tab3.addCell(pdfPCell31);
PdfPCell pdfPCell32 = new PdfPCell(new Paragraph("要货单位:" + map.get("chanName"), wuhao));
pdfPCell32.setColspan(1);
pdfPCell32.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell32.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell32.setBorder(0);
tab3.addCell(pdfPCell32);
document.add(tab3);
// 分割线一
Paragraph ph2 = new Paragraph();
ph2.add(new LineSeparator());
document.add(ph2);
PdfPTable tab4 = new PdfPTable(11);
int[] width4 = {25, 1, 20, 1, 12, 1, 12, 1, 12, 1, 14};
tab4.setWidths(width4);
tab4.setWidthPercentage(100);
tab4.setSpacingBefore(25);
PdfPCell pdfPCell41 = new PdfPCell(new Paragraph("品名", wuhao));
pdfPCell41.setColspan(1);
pdfPCell41.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell41.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell41.setBorder(0);
tab4.addCell(pdfPCell41);
PdfPCell pdfPCell42 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell42.setColspan(1);
pdfPCell42.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell42.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell42.setBorder(0);
tab4.addCell(pdfPCell42);
PdfPCell pdfPCell43 = new PdfPCell(new Paragraph("规格", wuhao));
pdfPCell43.setColspan(1);
pdfPCell43.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell43.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell43.setBorder(0);
tab4.addCell(pdfPCell43);
PdfPCell pdfPCell44 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell44.setColspan(1);
pdfPCell44.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell44.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell44.setBorder(0);
tab4.addCell(pdfPCell44);
PdfPCell pdfPCell45 = new PdfPCell(new Paragraph("单位", wuhao));
pdfPCell45.setColspan(1);
pdfPCell45.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell45.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell45.setBorder(0);
tab4.addCell(pdfPCell45);
PdfPCell pdfPCell46 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell46.setColspan(1);
pdfPCell46.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell46.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell46.setBorder(0);
tab4.addCell(pdfPCell46);
PdfPCell pdfPCell47 = new PdfPCell(new Paragraph("数量", wuhao));
pdfPCell47.setColspan(1);
pdfPCell47.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell47.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell47.setBorder(0);
tab4.addCell(pdfPCell47);
PdfPCell pdfPCell48 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell48.setColspan(1);
pdfPCell48.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell48.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell48.setBorder(0);
tab4.addCell(pdfPCell48);
PdfPCell pdfPCell49 = new PdfPCell(new Paragraph("单价", wuhao));
pdfPCell49.setColspan(1);
pdfPCell49.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell49.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell49.setBorder(0);
tab4.addCell(pdfPCell49);
PdfPCell pdfPCell410 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell410.setColspan(1);
pdfPCell410.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell410.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell410.setBorder(0);
tab4.addCell(pdfPCell410);
PdfPCell pdfPCell411 = new PdfPCell(new Paragraph("备注", wuhao));
pdfPCell411.setColspan(1);
pdfPCell411.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell411.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell411.setBorder(0);
tab4.addCell(pdfPCell411);
document.add(tab4);
PdfPTable tab5 = new PdfPTable(11);
int[] width5 = {25, 1, 20, 1, 12, 1, 12, 1, 12, 1, 14};
tab5.setWidths(width5);
tab5.setWidthPercentage(100);
tab5.setSpacingBefore(15);
tab5.setSpacingAfter(-10);
PdfPCell pdfPCell51 = new PdfPCell();
pdfPCell51.setColspan(1);
pdfPCell51.addElement(new LineSeparator());
pdfPCell51.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell51.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell51.setBorder(0);
tab5.addCell(pdfPCell51);
PdfPCell pdfPCell52 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell52.setColspan(1);
pdfPCell52.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell52.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell52.setBorder(0);
tab5.addCell(pdfPCell52);
PdfPCell pdfPCell53 = new PdfPCell();
pdfPCell53.setColspan(1);
pdfPCell53.addElement(new LineSeparator());
pdfPCell53.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell53.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell53.setBorder(0);
tab5.addCell(pdfPCell53);
PdfPCell pdfPCell54 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell54.setColspan(1);
pdfPCell54.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell54.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell54.setBorder(0);
tab5.addCell(pdfPCell54);
PdfPCell pdfPCell55 = new PdfPCell();
pdfPCell55.setColspan(1);
pdfPCell55.addElement(new LineSeparator());
pdfPCell55.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell55.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell55.setBorder(0);
tab5.addCell(pdfPCell55);
PdfPCell pdfPCell56 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell56.setColspan(1);
pdfPCell56.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell56.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell56.setBorder(0);
tab5.addCell(pdfPCell56);
PdfPCell pdfPCell57 = new PdfPCell();
pdfPCell57.setColspan(1);
pdfPCell57.addElement(new LineSeparator());
pdfPCell57.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell57.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell57.setBorder(0);
tab5.addCell(pdfPCell57);
PdfPCell pdfPCell58 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell58.setColspan(1);
pdfPCell58.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell58.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell58.setBorder(0);
tab5.addCell(pdfPCell58);
PdfPCell pdfPCell59 = new PdfPCell();
pdfPCell59.setColspan(1);
pdfPCell59.addElement(new LineSeparator());
pdfPCell59.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell59.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell59.setBorder(0);
tab5.addCell(pdfPCell59);
PdfPCell pdfPCell510 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell510.setColspan(1);
pdfPCell510.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell510.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell510.setBorder(0);
tab5.addCell(pdfPCell510);
PdfPCell pdfPCell511 = new PdfPCell();
pdfPCell511.setColspan(1);
pdfPCell511.addElement(new LineSeparator());
pdfPCell511.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell511.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell511.setBorder(0);
tab5.addCell(pdfPCell511);
document.add(tab5);
int count = 12;
if (orderDtls.size() > count) {
count = orderDtls.size();
}
for (int i = 0; i < count; i++) {
PurchaseOrderDtlInput orderDtlInput = new PurchaseOrderDtlInput();
if (orderDtls.size() > i) {
orderDtlInput.setItemName(orderDtls.get(i).getItemName());
orderDtlInput.setStandard(orderDtls.get(i).getStandard());
orderDtlInput.setUnitDesc(orderDtls.get(i).getUnitDesc());
orderDtlInput.setQty(String.valueOf(orderDtls.get(i).getPurchaseQty()));
orderDtlInput.setPrice(String.valueOf(orderDtls.get(i).getPurPrice()));
orderDtlInput.setRemark(orderDtls.get(i).getRemark());
}
PdfPTable tab6 = new PdfPTable(11);
int[] width6 = {25, 1, 20, 1, 12, 1, 12, 1, 12, 1, 14};
tab6.setWidths(width6);
tab6.setWidthPercentage(100);
if (i == 0) {
tab6.setSpacingBefore(5);
} else {
tab6.setSpacingBefore(0);
}
tab6.setSpacingAfter(0);
PdfPCell pdfPCell61 = new PdfPCell();
pdfPCell61.setColspan(1);
pdfPCell61.addElement(new Paragraph(orderDtlInput.getItemName(), wuhao));
pdfPCell61.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell61.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell61.setBorder(0);
pdfPCell61.setMinimumHeight(20);
tab6.addCell(pdfPCell61);
PdfPCell pdfPCell62 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell62.setColspan(1);
pdfPCell62.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell62.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell62.setBorder(0);
pdfPCell62.setMinimumHeight(20);
tab6.addCell(pdfPCell62);
PdfPCell pdfPCell63 = new PdfPCell();
pdfPCell63.setColspan(1);
pdfPCell63.addElement(new Paragraph(orderDtlInput.getStandard(), wuhao));
pdfPCell63.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell63.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell63.setBorder(0);
pdfPCell63.setMinimumHeight(20);
tab6.addCell(pdfPCell63);
PdfPCell pdfPCell64 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell64.setColspan(1);
pdfPCell64.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell64.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell64.setBorder(0);
pdfPCell64.setMinimumHeight(20);
tab6.addCell(pdfPCell64);
PdfPCell pdfPCell65 = new PdfPCell();
pdfPCell65.setColspan(1);
pdfPCell65.addElement(new Paragraph(orderDtlInput.getUnitDesc(), wuhao));
pdfPCell65.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell65.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell65.setBorder(0);
pdfPCell65.setMinimumHeight(20);
tab6.addCell(pdfPCell65);
PdfPCell pdfPCell66 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell66.setColspan(1);
pdfPCell66.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell66.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell66.setBorder(0);
pdfPCell66.setMinimumHeight(20);
tab6.addCell(pdfPCell66);
PdfPCell pdfPCell67 = new PdfPCell();
pdfPCell67.setColspan(1);
pdfPCell67.addElement(new Paragraph(orderDtlInput.getQty(), wuhao));
pdfPCell67.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell67.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell67.setBorder(0);
pdfPCell67.setMinimumHeight(20);
tab6.addCell(pdfPCell67);
PdfPCell pdfPCell68 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell68.setColspan(1);
pdfPCell68.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell68.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell68.setBorder(0);
pdfPCell68.setMinimumHeight(20);
tab6.addCell(pdfPCell68);
PdfPCell pdfPCell69 = new PdfPCell();
pdfPCell69.setColspan(1);
pdfPCell69.addElement(new Paragraph(orderDtlInput.getPrice(), wuhao));
pdfPCell69.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell69.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell69.setBorder(0);
pdfPCell69.setMinimumHeight(20);
tab6.addCell(pdfPCell69);
PdfPCell pdfPCell610 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell610.setColspan(1);
pdfPCell610.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell610.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell610.setBorder(0);
pdfPCell610.setMinimumHeight(20);
tab6.addCell(pdfPCell610);
PdfPCell pdfPCell611 = new PdfPCell();
pdfPCell611.setColspan(1);
pdfPCell611.addElement(new Paragraph(orderDtlInput.getRemark(), wuhao));
pdfPCell611.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell611.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell611.setBorder(0);
pdfPCell611.setMinimumHeight(20);
tab6.addCell(pdfPCell611);
document.add(tab6);
}
// 分割线二
Paragraph ph3 = new Paragraph();
ph3.add(new LineSeparator());
ph3.setSpacingBefore(-10);
ph3.setSpacingAfter(30);
document.add(ph3);
PdfPTable tab7 = new PdfPTable(2);
int[] width7 = {60, 40};
tab7.setWidths(width7);
tab7.setWidthPercentage(100);
tab7.setSpacingAfter(-20);
PdfPCell pdfPCell71 = new PdfPCell(new Paragraph("", wuhao));
pdfPCell71.setColspan(1);
pdfPCell71.setHorizontalAlignment(Element.ALIGN_RIGHT);
pdfPCell71.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell71.setBorder(0);
tab7.addCell(pdfPCell71);
PdfPCell pdfPCell72 = new PdfPCell(new Paragraph("要货人:" + map.get("name"), wuhao));
pdfPCell72.setColspan(1);
pdfPCell72.setHorizontalAlignment(Element.ALIGN_LEFT);
pdfPCell72.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfPCell72.setBorder(0);
tab7.addCell(pdfPCell72);
document.add(tab7);
}
}
document.close();
os.flush();
os.close();
}
}
效果