wujianwei
2025-09-09 deb890911f16f28d0caf0edd7b96e8082bdd306d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.ruoyi.cwgl.utils;
 
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.cwgl.domain.vo.DispatchOrderAttachmentVo;
 
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
 
 
public class MultiPagePdfWithImageUtils {
 
 
    public static String createPdf(List<DispatchOrderAttachmentVo> dispatchOrderAttachmentVos, String no) throws Exception {
        String fileName = no+ "_凭证_"+ UUID.randomUUID() +".pdf";
        String path = RuoYiConfig.getDownloadPath() + fileName;
 
        Document document = new Document(PageSize.A4, 36, 36, 36, 36);
        PdfWriter writer = PdfWriter.getInstance(document, Files.newOutputStream(Paths.get(path)));
        document.open();
 
        BaseFont baseFont = BaseFont.createFont("simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font = new Font(baseFont, 11, Font.NORMAL);
 
 
        int totalPages = dispatchOrderAttachmentVos.size();
        for (int i = 0; i < totalPages; i++) {
            if (i > 0) document.newPage();
 
            DispatchOrderAttachmentVo dispatchOrderAttachmentVo = dispatchOrderAttachmentVos.get(i);
 
            String currency = Optional.ofNullable(dispatchOrderAttachmentVo.getCurrency()).orElse("人民币");
            String currentStr;
            if ("人民币".equals(currency)){
                currentStr = "总金额:港币 0;人民币:"+dispatchOrderAttachmentVo.getAccount()+";";
            }else{
                currentStr = "总金额:港币 "+dispatchOrderAttachmentVo.getAccount()+";人民币:0;";
            }
 
 
            PdfPTable wrapperTable = new PdfPTable(1);
            wrapperTable.setWidthPercentage(100);
            wrapperTable.setKeepTogether(true);
 
            // ▶ 页眉表格
            PdfPTable headerTable = new PdfPTable(2);
            headerTable.setWidthPercentage(100);
            headerTable.setWidths(new int[]{3, 2});
            headerTable.addCell(createCell("调度单号:"+no, font));
            headerTable.addCell(createCell(currentStr, font));
            PdfPCell headerCell = new PdfPCell(headerTable);
            headerCell.setBorder(Rectangle.BOX);
            wrapperTable.addCell(headerCell);
            // ▶ 图片
            Image image = Image.getInstance(new URL(dispatchOrderAttachmentVo.getImageUrl()));
 
            // 限制图片最大尺寸
            float maxImgWidth = 500f;
            float maxImgHeight = 700f;
            image.scaleToFit(maxImgWidth, maxImgHeight);
            image.setAlignment(Image.ALIGN_CENTER);
 
            // 创建图片单元格并设置固定高度(确保整体表格不分页)
            PdfPCell imageCell = new PdfPCell();
            imageCell.setPadding(10);
            imageCell.setBorder(Rectangle.BOX);
            imageCell.setFixedHeight(maxImgHeight + 20); // 加一点 padding 空间
            imageCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            imageCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            imageCell.addElement(image);
            wrapperTable.addCell(imageCell);
 
            // ▶ 页脚表格
            PdfPTable footerTable = new PdfPTable(4);
            footerTable.setWidthPercentage(100);
            footerTable.setWidths(new int[]{3, 2, 2, 3});
            footerTable.addCell(createCell("费用类:"+dispatchOrderAttachmentVo.getFeeItem(), font));
            footerTable.addCell(createCell("金额:"+dispatchOrderAttachmentVo.getAccount(), font));
            footerTable.addCell(createCell("币制:"+dispatchOrderAttachmentVo.getCurrency(), font));
            footerTable.addCell(createCell("第 " + (i + 1) + " 页 / 共 " + totalPages + " 页", font));
            PdfPCell footerCell = new PdfPCell(footerTable);
            footerCell.setBorder(Rectangle.BOX);
            wrapperTable.addCell(footerCell);
 
            // 添加总表格
            document.add(wrapperTable);
 
        }
        document.close();
        writer.close();
        System.out.println("PDF 生成成功!");
        return path;
    }
 
    public static void main(String[] args) throws Exception {
        Document document = new Document(PageSize.A4, 36, 36, 36, 36);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("bordered_receipt.pdf"));
        document.open();
 
        BaseFont baseFont = BaseFont.createFont("simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font = new Font(baseFont, 11, Font.NORMAL);
 
        List<String> imageUrls = Arrays.asList(
                "https://xiongbenda-shengzheng.oss-cn-shenzhen.aliyuncs.com/cz/ImageViewServlet%20%281%29.gif",
                "https://xiongbenda-shengzheng.oss-cn-shenzhen.aliyuncs.com/cz/ImageViewServlet.gif"
        );
 
        int totalPages = imageUrls.size();
 
        for (int i = 0; i < totalPages; i++) {
            if (i > 0) document.newPage();
 
            // ▶ 构造一个总表格(3行,1列),包住所有内容,控制边框与分页
            PdfPTable wrapperTable = new PdfPTable(1);
            wrapperTable.setWidthPercentage(100);
            wrapperTable.setKeepTogether(true);
 
            // ▶ 页眉表格
            PdfPTable headerTable = new PdfPTable(2);
            headerTable.setWidthPercentage(100);
            headerTable.setWidths(new int[]{3, 2});
            headerTable.addCell(createCell("调度单号:LD2507260028", font));
            headerTable.addCell(createCell("总金额:港币 280;人民币:0", font));
            PdfPCell headerCell = new PdfPCell(headerTable);
            headerCell.setBorder(Rectangle.BOX);
            wrapperTable.addCell(headerCell);
 
            // ▶ 图片
            Image image = Image.getInstance(new URL(imageUrls.get(i)));
 
// 限制图片最大尺寸
            float maxImgWidth = 500f;
            float maxImgHeight = 700f;
            image.scaleToFit(maxImgWidth, maxImgHeight);
            image.setAlignment(Image.ALIGN_CENTER);
 
// 创建图片单元格并设置固定高度(确保整体表格不分页)
            PdfPCell imageCell = new PdfPCell();
            imageCell.setPadding(10);
            imageCell.setBorder(Rectangle.BOX);
            imageCell.setFixedHeight(maxImgHeight + 20); // 加一点 padding 空间
            imageCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            imageCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            imageCell.addElement(image);
            wrapperTable.addCell(imageCell);
 
            // ▶ 页脚表格
            PdfPTable footerTable = new PdfPTable(4);
            footerTable.setWidthPercentage(100);
            footerTable.setWidths(new int[]{3, 2, 2, 3});
            footerTable.addCell(createCell("费用类:登记费 HKD", font));
            footerTable.addCell(createCell("金额:250.00", font));
            footerTable.addCell(createCell("币制:港币", font));
            footerTable.addCell(createCell("第 " + (i + 1) + " 页 / 共 " + totalPages + " 页", font));
            PdfPCell footerCell = new PdfPCell(footerTable);
            footerCell.setBorder(Rectangle.BOX);
            wrapperTable.addCell(footerCell);
 
            // 添加总表格
            document.add(wrapperTable);
        }
 
        document.close();
        writer.close();
        System.out.println("PDF 生成成功!");
    }
 
 
 
 
    // 默认左对齐、含内边距、含边框
    private static PdfPCell createCell(String text, Font font) {
        PdfPCell cell = new PdfPCell(new Phrase(text, font));
        cell.setPadding(6);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }
}