|
|
|
@ -13,34 +13,13 @@ import javax.imageio.ImageIO;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.*;
|
|
|
|
import java.io.*;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
|
|
|
import java.util.zip.Deflater;
|
|
|
|
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class PDFGenerator {
|
|
|
|
public class PDFGenerator {
|
|
|
|
|
|
|
|
|
|
|
|
// 缓存字体实例以避免重复加载
|
|
|
|
|
|
|
|
private static volatile PDType0Font cachedFont = null;
|
|
|
|
|
|
|
|
private static final Object fontLock = new Object();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 缓存已处理的图片以避免重复下载和处理
|
|
|
|
|
|
|
|
private static final ConcurrentHashMap<String, PDImageXObject> imageCache = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 线程池用于并发处理图片
|
|
|
|
|
|
|
|
private static final ExecutorService imageProcessingPool = Executors.newFixedThreadPool(4);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 表格参数配置
|
|
|
|
// 1. 表格参数配置
|
|
|
|
|
|
|
|
|
|
|
|
// 参数配置(优化后)
|
|
|
|
// 参数配置(优化后)
|
|
|
|
@ -53,14 +32,12 @@ public class PDFGenerator {
|
|
|
|
private static final float CELL_PADDING = 5;
|
|
|
|
private static final float CELL_PADDING = 5;
|
|
|
|
private static final float[] COLUMN_WIDTHS = {LEFT_COL_WIDTH, RIGHT_COL_WIDTH};
|
|
|
|
private static final float[] COLUMN_WIDTHS = {LEFT_COL_WIDTH, RIGHT_COL_WIDTH};
|
|
|
|
private static final float MIN_IMAGE_HEIGHT = 50;
|
|
|
|
private static final float MIN_IMAGE_HEIGHT = 50;
|
|
|
|
|
|
|
|
public static void generateInstallationPDF(DeviceInstallInfo entity, OutputStream outputStream) throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
public static void generateInstallationPDFBytes(DeviceInstallInfo entity, ByteArrayOutputStream outputStream) throws IOException {
|
|
|
|
|
|
|
|
try (PDDocument document = new PDDocument()) {
|
|
|
|
try (PDDocument document = new PDDocument()) {
|
|
|
|
// 加载字体
|
|
|
|
// 加载字体(需替换实际路径)
|
|
|
|
// PDType0Font font = PDType0Font.load(document, new File("D:\\NotoSansCJK-Regular.ttf"));
|
|
|
|
//PDType0Font font = PDType0Font.load(document, new File("D:\\NotoSansCJK-Regular.ttf"));
|
|
|
|
|
|
|
|
//PDType0Font font = PDType0Font.load(document, new File("/ttf/simsun.ttf"));
|
|
|
|
PDType0Font font = PDType0Font.load(document, new File("/ttf/NotoSansCJK-Regular.ttf"));
|
|
|
|
PDType0Font font = PDType0Font.load(document, new File("/ttf/NotoSansCJK-Regular.ttf"));
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化第一页
|
|
|
|
// 初始化第一页
|
|
|
|
PDPage currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
PDPage currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
document.addPage(currentPage);
|
|
|
|
document.addPage(currentPage);
|
|
|
|
@ -69,18 +46,20 @@ public class PDFGenerator {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// 1. 绘制标题(居中)
|
|
|
|
// 1. 绘制标题(居中)
|
|
|
|
drawCenteredText(cs, font, FONT_SIZE + 10,
|
|
|
|
drawCenteredText(cs, font, FONT_SIZE+10,
|
|
|
|
"安装信息录入表",
|
|
|
|
"安装信息录入表",
|
|
|
|
PDRectangle.A4.getWidth() / 2, currentY);
|
|
|
|
PDRectangle.A4.getWidth()/2, currentY);
|
|
|
|
currentY -= ROW_HEIGHT * 1.5f;
|
|
|
|
currentY -= ROW_HEIGHT * 1.5f;
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 绘制基本信息表格
|
|
|
|
// 2. 绘制基本信息表格
|
|
|
|
String[][] baseData = {
|
|
|
|
String[][] baseData = {
|
|
|
|
|
|
|
|
// {"公司名称", entity.getCorporateName()},
|
|
|
|
{"申请时间", entity.getProposerTime().toString()},
|
|
|
|
{"申请时间", entity.getProposerTime().toString()},
|
|
|
|
{"申请人", entity.getProposer()},
|
|
|
|
{"申请人", entity.getProposer()},
|
|
|
|
{"申请人班组", entity.getProposerTeam()},
|
|
|
|
{"申请人班组", entity.getProposerTeam()},
|
|
|
|
{"小区名字", entity.getCommunityName()},
|
|
|
|
{"小区名字", entity.getCommunityName()},
|
|
|
|
{"用户姓名", entity.getUserName()},
|
|
|
|
{"用户姓名", entity.getUserName()},
|
|
|
|
|
|
|
|
// {"用户姓名", entity.getUserName()+"-" + entity.getCommunityName()+entity.getBuildingUnit()+entity.getRoomNo()},
|
|
|
|
{"电话", entity.getUserIpone()},
|
|
|
|
{"电话", entity.getUserIpone()},
|
|
|
|
{"楼栋单元号", entity.getBuildingUnit()},
|
|
|
|
{"楼栋单元号", entity.getBuildingUnit()},
|
|
|
|
{"房间号", entity.getRoomNo()},
|
|
|
|
{"房间号", entity.getRoomNo()},
|
|
|
|
@ -88,24 +67,24 @@ public class PDFGenerator {
|
|
|
|
{"切断阀编号", entity.getShutValueNumber()},
|
|
|
|
{"切断阀编号", entity.getShutValueNumber()},
|
|
|
|
{"燃气表号", entity.getGasMeterNumber()},
|
|
|
|
{"燃气表号", entity.getGasMeterNumber()},
|
|
|
|
{"备注", entity.getBuildingUnit()}
|
|
|
|
{"备注", entity.getBuildingUnit()}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
drawTable(cs, font, TABLE_MARGIN, currentY, baseData, COLUMN_WIDTHS);
|
|
|
|
drawTable(cs, font, TABLE_MARGIN, currentY, baseData, COLUMN_WIDTHS);
|
|
|
|
currentY -= (ROW_HEIGHT * baseData.length) + 20f;
|
|
|
|
currentY -= (ROW_HEIGHT * baseData.length) + 20f;
|
|
|
|
|
|
|
|
// @ApiModelProperty(value = "安装前图片")
|
|
|
|
// 3. 图片处理(带自动分页)
|
|
|
|
// 创建后续页面 - 图片部分
|
|
|
|
|
|
|
|
// 图片处理(带自动分页)
|
|
|
|
|
|
|
|
// 图片处理(带自动分页)
|
|
|
|
List<String[]> imageGroups = Arrays.asList(
|
|
|
|
List<String[]> imageGroups = Arrays.asList(
|
|
|
|
new String[]{entity.getDeviceInfoImage(), "设备信息图片"},
|
|
|
|
|
|
|
|
new String[]{entity.getBeforeInstallationImage(), "安装前图片"},
|
|
|
|
new String[]{entity.getBeforeInstallationImage(), "安装前图片"},
|
|
|
|
new String[]{entity.getWorkingOfTheDetectorImage(), "安装完成探测器工作图片"},
|
|
|
|
new String[]{entity.getWorkingOfTheDetectorImage(), "安装完成探测器工作图片"},
|
|
|
|
new String[]{entity.getSideLeakageImage(), "测漏图片"},
|
|
|
|
new String[]{entity.getSideLeakageImage(), "测漏图片"},
|
|
|
|
new String[]{entity.getFormSideLeakageImage(), "泡沫水测漏图片"},
|
|
|
|
|
|
|
|
new String[]{entity.getIgnitionPictureImage(), "点火图片"},
|
|
|
|
new String[]{entity.getIgnitionPictureImage(), "点火图片"},
|
|
|
|
new String[]{entity.getInstallThePanoramicImage(), "安装完成全景图片"},
|
|
|
|
new String[]{entity.getInstallThePanoramicImage(), "安装完成全景图片"},
|
|
|
|
new String[]{entity.getOfGasMeterImage(), "燃气表号图片"},
|
|
|
|
new String[]{entity.getOfGasMeterImage(), "燃气表图片"},
|
|
|
|
new String[]{entity.getWorkOrderImage(), "工单图片"},
|
|
|
|
new String[]{entity.getWorkOrderImage(), "工单图片"},
|
|
|
|
new String[]{entity.getPunchingImage(), "打孔图片"},
|
|
|
|
new String[]{entity.getPunchingImage(), "打孔图片"},
|
|
|
|
new String[]{entity.getFiexImage(), "安装电源线图片"},
|
|
|
|
new String[]{entity.getFiexImage(), "安装电源线图片"}
|
|
|
|
new String[]{entity.getHouseNumberImage(), "门牌号图片"}
|
|
|
|
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
for (String[] group : imageGroups) {
|
|
|
|
for (String[] group : imageGroups) {
|
|
|
|
@ -119,40 +98,7 @@ public class PDFGenerator {
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
|
|
if (!validUrls.isEmpty()) {
|
|
|
|
if (!validUrls.isEmpty()) {
|
|
|
|
// 处理可能需要跨页的多图片情况
|
|
|
|
// float totalHeight = calculateTotalHeight(validUrls);
|
|
|
|
if (validUrls.size() > 2) {
|
|
|
|
|
|
|
|
// 对于多于2张的图片,逐个或分组处理以支持跨页
|
|
|
|
|
|
|
|
for (int i = 0; i < validUrls.size(); i += 2) { // 每次处理最多2张图片
|
|
|
|
|
|
|
|
List<String> subList = validUrls.subList(i,
|
|
|
|
|
|
|
|
Math.min(i + 2, validUrls.size()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<String> wrappedTitleLines = wrapText(title,LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
|
|
|
|
float titleHeight = wrappedTitleLines.size() * LINE_HEIGHT;
|
|
|
|
|
|
|
|
// 计算图片组的自适应高度
|
|
|
|
|
|
|
|
float imagesHeight = calculateAdjustedImagesHeight(document, subList,
|
|
|
|
|
|
|
|
RIGHT_COL_WIDTH - 2 * CELL_PADDING,
|
|
|
|
|
|
|
|
PDRectangle.A4.getHeight() - 2 * TABLE_MARGIN - titleHeight);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float totalHeight = Math.max(imagesHeight, titleHeight);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (currentY < TABLE_MARGIN + Math.max(totalHeight, titleHeight)) {
|
|
|
|
|
|
|
|
cs.close();
|
|
|
|
|
|
|
|
currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
|
|
|
|
document.addPage(currentPage);
|
|
|
|
|
|
|
|
cs = new PDPageContentStream(document, currentPage);
|
|
|
|
|
|
|
|
currentY = PDRectangle.A4.getHeight() - TABLE_MARGIN;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加标识,表明这是多图片的一部分
|
|
|
|
|
|
|
|
String subTitle = title + " (" + (i+1) + "-" + Math.min(i+2, validUrls.size()) + "/" + validUrls.size() + ")";
|
|
|
|
|
|
|
|
List<String> subWrappedTitleLines = wrapText(subTitle, LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
drawImageGroup(cs, document, font, subWrappedTitleLines, subList, currentY,
|
|
|
|
|
|
|
|
Math.max(totalHeight, titleHeight));
|
|
|
|
|
|
|
|
currentY -= Math.max(totalHeight, titleHeight);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 原有的单/双图片处理逻辑
|
|
|
|
|
|
|
|
List<String> wrappedTitleLines = wrapText(title,LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
List<String> wrappedTitleLines = wrapText(title,LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
float titleHeight = wrappedTitleLines.size() * LINE_HEIGHT;
|
|
|
|
float titleHeight = wrappedTitleLines.size() * LINE_HEIGHT;
|
|
|
|
// 计算图片组的自适应高度
|
|
|
|
// 计算图片组的自适应高度
|
|
|
|
@ -176,263 +122,24 @@ public class PDFGenerator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
cs.close();
|
|
|
|
cs.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// document.save("D:/output.pdf");
|
|
|
|
// 将PDF保存到输出流
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
document.save(outputStream);
|
|
|
|
document.save(baos);
|
|
|
|
}
|
|
|
|
outputStream.flush();
|
|
|
|
}
|
|
|
|
baos.writeTo(outputStream);
|
|
|
|
|
|
|
|
// document.save(outputStream);
|
|
|
|
|
|
|
|
|
|
|
|
public static void generateInstallationPDF(DeviceInstallInfo entity, OutputStream outputStream) throws IOException {
|
|
|
|
|
|
|
|
ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
generateInstallationPDFBytes(entity, pdfBaos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建ZIP压缩包
|
|
|
|
|
|
|
|
try (ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
|
|
|
|
|
|
|
|
zipOut.setLevel(Deflater.BEST_COMPRESSION);
|
|
|
|
|
|
|
|
ZipEntry entry = new ZipEntry(entity.getUserName() + entity.getCommunityName() + entity.getBuildingUnit() + entity.getRoomNo() + ".pdf");
|
|
|
|
|
|
|
|
zipOut.putNextEntry(entry);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byte[] pdfBytes = pdfBaos.toByteArray();
|
|
|
|
|
|
|
|
zipOut.write(pdfBytes, 0, pdfBytes.length);
|
|
|
|
|
|
|
|
zipOut.closeEntry();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 辅助方法:生成文件名
|
|
|
|
|
|
|
|
public static String generateFileName(DeviceInstallInfo entity) {
|
|
|
|
|
|
|
|
return entity.getUserName() + entity.getCommunityName() +
|
|
|
|
|
|
|
|
entity.getBuildingUnit() + entity.getRoomNo() + ".pdf";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public static void generateInstallationPDF(DeviceInstallInfo entity, OutputStream outputStream) throws IOException {
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 首先生成PDF内容到字节数组
|
|
|
|
|
|
|
|
// ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// try (PDDocument document = new PDDocument()) {
|
|
|
|
|
|
|
|
// // 加载字体(需替换实际路径)
|
|
|
|
|
|
|
|
//// PDType0Font font = PDType0Font.load(document, new File("D:\\NotoSansCJK-Regular.ttf"));
|
|
|
|
|
|
|
|
// //PDType0Font font = PDType0Font.load(document, new File("/ttf/simsun.ttf"));
|
|
|
|
|
|
|
|
// PDType0Font font = PDType0Font.load(document, new File("/ttf/NotoSansCJK-Regular.ttf"));
|
|
|
|
|
|
|
|
// // 初始化第一页
|
|
|
|
|
|
|
|
// PDPage currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
|
|
|
|
// document.addPage(currentPage);
|
|
|
|
|
|
|
|
// PDPageContentStream cs = new PDPageContentStream(document, currentPage);
|
|
|
|
|
|
|
|
// float currentY = PDRectangle.A4.getHeight() - TABLE_MARGIN;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// // 1. 绘制标题(居中)
|
|
|
|
|
|
|
|
// drawCenteredText(cs, font, FONT_SIZE+10,
|
|
|
|
|
|
|
|
// "安装信息录入表",
|
|
|
|
|
|
|
|
// PDRectangle.A4.getWidth()/2, currentY);
|
|
|
|
|
|
|
|
// currentY -= ROW_HEIGHT * 1.5f;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 2. 绘制基本信息表格
|
|
|
|
|
|
|
|
// String[][] baseData = {
|
|
|
|
|
|
|
|
// // {"公司名称", entity.getCorporateName()},
|
|
|
|
|
|
|
|
// {"申请时间", entity.getProposerTime().toString()},
|
|
|
|
|
|
|
|
// {"申请人", entity.getProposer()},
|
|
|
|
|
|
|
|
// {"申请人班组", entity.getProposerTeam()},
|
|
|
|
|
|
|
|
// {"小区名字", entity.getCommunityName()},
|
|
|
|
|
|
|
|
// {"用户姓名", entity.getUserName()},
|
|
|
|
|
|
|
|
// // {"用户姓名", entity.getUserName()+"-" + entity.getCommunityName()+entity.getBuildingUnit()+entity.getRoomNo()},
|
|
|
|
|
|
|
|
// {"电话", entity.getUserIpone()},
|
|
|
|
|
|
|
|
// {"楼栋单元号", entity.getBuildingUnit()},
|
|
|
|
|
|
|
|
// {"房间号", entity.getRoomNo()},
|
|
|
|
|
|
|
|
// {"报警器编号", entity.getDeviceName()},
|
|
|
|
|
|
|
|
// {"切断阀编号", entity.getShutValueNumber()},
|
|
|
|
|
|
|
|
// {"燃气表号", entity.getGasMeterNumber()},
|
|
|
|
|
|
|
|
// {"备注", entity.getBuildingUnit()}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// drawTable(cs, font, TABLE_MARGIN, currentY, baseData, COLUMN_WIDTHS);
|
|
|
|
|
|
|
|
// currentY -= (ROW_HEIGHT * baseData.length) + 20f;
|
|
|
|
|
|
|
|
//// @ApiModelProperty(value = "安装前图片")
|
|
|
|
|
|
|
|
// // 创建后续页面 - 图片部分
|
|
|
|
|
|
|
|
// // 图片处理(带自动分页)
|
|
|
|
|
|
|
|
// // 图片处理(带自动分页)
|
|
|
|
|
|
|
|
// List<String[]> imageGroups = Arrays.asList(
|
|
|
|
|
|
|
|
// new String[]{entity.getDeviceInfoImage(), "设备信息图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getBeforeInstallationImage(), "安装前图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getWorkingOfTheDetectorImage(), "安装完成探测器工作图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getSideLeakageImage(), "测漏图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getFormSideLeakageImage(), "泡沫水测漏图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getIgnitionPictureImage(), "点火图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getInstallThePanoramicImage(), "安装完成全景图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getOfGasMeterImage(), "燃气表号图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getWorkOrderImage(), "工单图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getPunchingImage(), "打孔图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getFiexImage(), "安装电源线图片"},
|
|
|
|
|
|
|
|
// new String[]{entity.getHouseNumberImage(), "门牌号图片"}
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// for (String[] group : imageGroups) {
|
|
|
|
|
|
|
|
// String urls = group[0];
|
|
|
|
|
|
|
|
// String title = group[1];
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (urls != null && !urls.trim().isEmpty()) {
|
|
|
|
|
|
|
|
// List<String> validUrls = Arrays.stream(urls.split(","))
|
|
|
|
|
|
|
|
// .map(String::trim)
|
|
|
|
|
|
|
|
// .filter(url -> !url.isEmpty() && url.startsWith("http"))
|
|
|
|
|
|
|
|
// .collect(Collectors.toList());
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (!validUrls.isEmpty()) {
|
|
|
|
|
|
|
|
// // 处理可能需要跨页的多图片情况
|
|
|
|
|
|
|
|
// if (validUrls.size() > 2) {
|
|
|
|
|
|
|
|
// // 对于多于2张的图片,逐个或分组处理以支持跨页
|
|
|
|
|
|
|
|
// for (int i = 0; i < validUrls.size(); i += 2) { // 每次处理最多2张图片
|
|
|
|
|
|
|
|
// List<String> subList = validUrls.subList(i,
|
|
|
|
|
|
|
|
// Math.min(i + 2, validUrls.size()));
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// List<String> wrappedTitleLines = wrapText(title,LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
|
|
|
|
// float titleHeight = wrappedTitleLines.size() * LINE_HEIGHT;
|
|
|
|
|
|
|
|
// // 计算图片组的自适应高度
|
|
|
|
|
|
|
|
// float imagesHeight = calculateAdjustedImagesHeight(document, subList,
|
|
|
|
|
|
|
|
// RIGHT_COL_WIDTH - 2 * CELL_PADDING,
|
|
|
|
|
|
|
|
// PDRectangle.A4.getHeight() - 2 * TABLE_MARGIN - titleHeight);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// float totalHeight = Math.max(imagesHeight, titleHeight);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (currentY < TABLE_MARGIN + Math.max(totalHeight, titleHeight)) {
|
|
|
|
|
|
|
|
// cs.close();
|
|
|
|
|
|
|
|
// currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
|
|
|
|
// document.addPage(currentPage);
|
|
|
|
|
|
|
|
// cs = new PDPageContentStream(document, currentPage);
|
|
|
|
|
|
|
|
// currentY = PDRectangle.A4.getHeight() - TABLE_MARGIN;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 添加标识,表明这是多图片的一部分
|
|
|
|
|
|
|
|
// String subTitle = title + " (" + (i+1) + "-" + Math.min(i+2, validUrls.size()) + "/" + validUrls.size() + ")";
|
|
|
|
|
|
|
|
// List<String> subWrappedTitleLines = wrapText(subTitle, LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// drawImageGroup(cs, document, font, subWrappedTitleLines, subList, currentY,
|
|
|
|
|
|
|
|
// Math.max(totalHeight, titleHeight));
|
|
|
|
|
|
|
|
// currentY -= Math.max(totalHeight, titleHeight);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// // 原有的单/双图片处理逻辑
|
|
|
|
|
|
|
|
// List<String> wrappedTitleLines = wrapText(title,LEFT_COL_WIDTH - 2 * CELL_PADDING);
|
|
|
|
|
|
|
|
// float titleHeight = wrappedTitleLines.size() * LINE_HEIGHT;
|
|
|
|
|
|
|
|
// // 计算图片组的自适应高度
|
|
|
|
|
|
|
|
// float imagesHeight = calculateAdjustedImagesHeight(document, validUrls,
|
|
|
|
|
|
|
|
// RIGHT_COL_WIDTH - 2 * CELL_PADDING,
|
|
|
|
|
|
|
|
// PDRectangle.A4.getHeight() - 2 * TABLE_MARGIN - titleHeight);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// float totalHeight = Math.max(imagesHeight, titleHeight);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (currentY < TABLE_MARGIN + Math.max(totalHeight, titleHeight)) {
|
|
|
|
|
|
|
|
// cs.close();
|
|
|
|
|
|
|
|
// currentPage = new PDPage(PDRectangle.A4);
|
|
|
|
|
|
|
|
// document.addPage(currentPage);
|
|
|
|
|
|
|
|
// cs = new PDPageContentStream(document, currentPage);
|
|
|
|
|
|
|
|
// currentY = PDRectangle.A4.getHeight() - TABLE_MARGIN;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// drawImageGroup(cs, document, font, wrappedTitleLines, validUrls, currentY,
|
|
|
|
|
|
|
|
// Math.max(totalHeight, titleHeight));
|
|
|
|
|
|
|
|
// currentY -= Math.max(totalHeight, titleHeight);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
// } finally {
|
|
|
|
|
|
|
|
// cs.close();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // 将PDF保存到ByteArrayOutputStream
|
|
|
|
|
|
|
|
// document.save(pdfBaos);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
//// document.save(baos);
|
|
|
|
|
|
|
|
//// outputStream.flush();
|
|
|
|
|
|
|
|
//// baos.writeTo(outputStream);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // 创建ZIP压缩包并将PDF写入输出流
|
|
|
|
|
|
|
|
// try (ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
|
|
|
|
|
|
|
|
// // 设置最高压缩级别
|
|
|
|
|
|
|
|
// zipOut.setLevel(Deflater.BEST_COMPRESSION);
|
|
|
|
|
|
|
|
// // 创建ZIP条目
|
|
|
|
|
|
|
|
// ZipEntry entry = new ZipEntry(entity.getUserName()+entity.getCommunityName()+entity.getBuildingUnit()+entity.getRoomNo()+".pdf");
|
|
|
|
|
|
|
|
// zipOut.putNextEntry(entry);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 将PDF数据写入ZIP
|
|
|
|
|
|
|
|
// byte[] pdfBytes = pdfBaos.toByteArray();
|
|
|
|
|
|
|
|
// zipOut.write(pdfBytes, 0, pdfBytes.length);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 关闭当前条目
|
|
|
|
|
|
|
|
// zipOut.closeEntry();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 加载字体,确保使用正确的文档实例
|
|
|
|
|
|
|
|
private static PDType0Font loadFont(PDDocument document) throws IOException {
|
|
|
|
|
|
|
|
// 尝试多个可能的字体路径
|
|
|
|
|
|
|
|
String[] fontPaths = {
|
|
|
|
|
|
|
|
"D:\\NotoSansCJK-Regular.ttf",
|
|
|
|
|
|
|
|
"C:\\Windows\\Fonts\\simsun.ttc",
|
|
|
|
|
|
|
|
"C:\\Windows\\Fonts\\msyh.ttc",
|
|
|
|
|
|
|
|
"/System/Library/Fonts/Helvetica.dfont", // macOS
|
|
|
|
|
|
|
|
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf" // Linux
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (String path : fontPaths) {
|
|
|
|
|
|
|
|
if (Files.exists(Paths.get(path))) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
return PDType0Font.load(document, new File(path));
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
continue; // 尝试下一个字体文件
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return PDType0Font.load(document, new File("/ttf/NotoSansCJK-Regular.ttf"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 缓存已处理的图片以避免重复下载和处理
|
|
|
|
|
|
|
|
private static final ConcurrentHashMap<String, byte[]> imageByteCache = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 预加载图片以避免重复下载
|
|
|
|
|
|
|
|
private static PDImageXObject loadImageFromUrl(PDDocument document, String url) throws IOException {
|
|
|
|
|
|
|
|
byte[] imageBytes = imageByteCache.computeIfAbsent(url, key -> {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
return new URL(key).openStream().readAllBytes();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
return null; // 或者抛出异常
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (imageBytes == null) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return PDImageXObject.createFromByteArray(document, imageBytes, "");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static List<String> wrapText(String text, float maxWidth) {
|
|
|
|
private static List<String> wrapText(String text, float maxWidth) {
|
|
|
|
List<String> lines = new ArrayList<>();
|
|
|
|
List<String> lines = new ArrayList<>();
|
|
|
|
if (text == null || text.isEmpty()) return lines;
|
|
|
|
int maxCharsPerLine = (int)(maxWidth / (FONT_SIZE * 0.5f)); // 中文字符估算
|
|
|
|
|
|
|
|
|
|
|
|
// 优化的文本换行逻辑,减少计算量
|
|
|
|
|
|
|
|
int maxCharsPerLine = (int) (maxWidth / (FONT_SIZE * 0.4f)); // 调整字符估算系数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (text.length() <= maxCharsPerLine) {
|
|
|
|
|
|
|
|
lines.add(text);
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder currentLine = new StringBuilder();
|
|
|
|
StringBuilder currentLine = new StringBuilder();
|
|
|
|
String[] words = text.split("");
|
|
|
|
for (String word : text.split("")) {
|
|
|
|
for (String word : words) {
|
|
|
|
|
|
|
|
if (currentLine.length() + word.length() <= maxCharsPerLine) {
|
|
|
|
if (currentLine.length() + word.length() <= maxCharsPerLine) {
|
|
|
|
currentLine.append(word);
|
|
|
|
currentLine.append(word);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
@ -445,24 +152,6 @@ public class PDFGenerator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// private static List<String> wrapText(String text, float maxWidth) {
|
|
|
|
|
|
|
|
// List<String> lines = new ArrayList<>();
|
|
|
|
|
|
|
|
// int maxCharsPerLine = (int)(maxWidth / (FONT_SIZE * 0.5f)); // 中文字符估算
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// StringBuilder currentLine = new StringBuilder();
|
|
|
|
|
|
|
|
// for (String word : text.split("")) {
|
|
|
|
|
|
|
|
// if (currentLine.length() + word.length() <= maxCharsPerLine) {
|
|
|
|
|
|
|
|
// currentLine.append(word);
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// lines.add(currentLine.toString());
|
|
|
|
|
|
|
|
// currentLine = new StringBuilder(word);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (currentLine.length() > 0) {
|
|
|
|
|
|
|
|
// lines.add(currentLine.toString());
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return lines;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static float calculateTotalHeight(List<String> imageUrls) throws IOException {
|
|
|
|
private static float calculateTotalHeight(List<String> imageUrls) throws IOException {
|
|
|
|
@ -572,7 +261,7 @@ public class PDFGenerator {
|
|
|
|
// 单图片模式:一页两行,每行占半页高度
|
|
|
|
// 单图片模式:一页两行,每行占半页高度
|
|
|
|
float singleImageHeight = imageAvailableHeight;
|
|
|
|
float singleImageHeight = imageAvailableHeight;
|
|
|
|
// 绘制标题(垂直居中)
|
|
|
|
// 绘制标题(垂直居中)
|
|
|
|
float titleY = startY - totalHeight / 2 + (titleLines.size() * LINE_HEIGHT) / 2;
|
|
|
|
float titleY = startY - totalHeight/2 + (titleLines.size() * LINE_HEIGHT)/2;
|
|
|
|
// 绘制第一行标题(顶部)
|
|
|
|
// 绘制第一行标题(顶部)
|
|
|
|
// float titleY = startY - CELL_PADDING - FONT_SIZE;
|
|
|
|
// float titleY = startY - CELL_PADDING - FONT_SIZE;
|
|
|
|
cs.beginText();
|
|
|
|
cs.beginText();
|
|
|
|
@ -586,8 +275,8 @@ public class PDFGenerator {
|
|
|
|
cs.endText();
|
|
|
|
cs.endText();
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制第一行图片(自动缩放)
|
|
|
|
// 绘制第一行图片(自动缩放)
|
|
|
|
PDImageXObject image1 = loadImageFromUrl(doc, imageUrls.get(0));
|
|
|
|
PDImageXObject image1 = PDImageXObject.createFromByteArray(doc,
|
|
|
|
if (image1 != null) {
|
|
|
|
new URL(imageUrls.get(0)).openStream().readAllBytes(), "embedded-image");
|
|
|
|
float scale1 = Math.min(
|
|
|
|
float scale1 = Math.min(
|
|
|
|
imageAvailableWidth / image1.getWidth(),
|
|
|
|
imageAvailableWidth / image1.getWidth(),
|
|
|
|
singleImageHeight / image1.getHeight()
|
|
|
|
singleImageHeight / image1.getHeight()
|
|
|
|
@ -599,14 +288,13 @@ public class PDFGenerator {
|
|
|
|
TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
startY - CELL_PADDING - scaledHeight1,
|
|
|
|
startY - CELL_PADDING - scaledHeight1,
|
|
|
|
scaledWidth1, scaledHeight1);
|
|
|
|
scaledWidth1, scaledHeight1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (imageUrls.size() == 2) {
|
|
|
|
} else if (imageUrls.size() == 2) {
|
|
|
|
// 双图片模式:改为上下布局
|
|
|
|
// 双图片模式:改为上下布局
|
|
|
|
float imageHeightPer = (imageAvailableHeight - CELL_PADDING) / 2;
|
|
|
|
float imageHeightPer = (imageAvailableHeight - CELL_PADDING) / 2;
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制标题(垂直居中)
|
|
|
|
// 绘制标题(垂直居中)
|
|
|
|
float titleY = startY - totalHeight / 2 + (titleLines.size() * LINE_HEIGHT) / 2;
|
|
|
|
float titleY = startY - totalHeight/2 + (titleLines.size() * LINE_HEIGHT)/2;
|
|
|
|
cs.beginText();
|
|
|
|
cs.beginText();
|
|
|
|
cs.setFont(font, FONT_SIZE);
|
|
|
|
cs.setFont(font, FONT_SIZE);
|
|
|
|
cs.newLineAtOffset(TABLE_MARGIN + CELL_PADDING, titleY);
|
|
|
|
cs.newLineAtOffset(TABLE_MARGIN + CELL_PADDING, titleY);
|
|
|
|
@ -618,25 +306,23 @@ public class PDFGenerator {
|
|
|
|
cs.endText();
|
|
|
|
cs.endText();
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制第一张图片(上)
|
|
|
|
// 绘制第一张图片(上)
|
|
|
|
PDImageXObject image1 = loadImageFromUrl(doc, imageUrls.get(0));
|
|
|
|
PDImageXObject image1 = PDImageXObject.createFromByteArray(doc,
|
|
|
|
float scaledHeight1 = 0;
|
|
|
|
new URL(imageUrls.get(0)).openStream().readAllBytes(), "embedded-image");
|
|
|
|
if (image1 != null) {
|
|
|
|
|
|
|
|
float scale1 = Math.min(
|
|
|
|
float scale1 = Math.min(
|
|
|
|
imageAvailableWidth / image1.getWidth(),
|
|
|
|
imageAvailableWidth / image1.getWidth(),
|
|
|
|
imageHeightPer / image1.getHeight()
|
|
|
|
imageHeightPer / image1.getHeight()
|
|
|
|
);
|
|
|
|
);
|
|
|
|
scaledHeight1 = image1.getHeight() * scale1;
|
|
|
|
float scaledHeight1 = image1.getHeight() * scale1;
|
|
|
|
float scaledWidth1 = image1.getWidth() * scale1;
|
|
|
|
float scaledWidth1 = image1.getWidth() * scale1;
|
|
|
|
|
|
|
|
|
|
|
|
cs.drawImage(image1,
|
|
|
|
cs.drawImage(image1,
|
|
|
|
TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
startY - CELL_PADDING - scaledHeight1,
|
|
|
|
startY - CELL_PADDING - scaledHeight1,
|
|
|
|
scaledWidth1, scaledHeight1);
|
|
|
|
scaledWidth1, scaledHeight1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制第二张图片(下)
|
|
|
|
// 绘制第二张图片(下)
|
|
|
|
PDImageXObject image2 = loadImageFromUrl(doc, imageUrls.get(1));
|
|
|
|
PDImageXObject image2 = PDImageXObject.createFromByteArray(doc,
|
|
|
|
if (image2 != null) {
|
|
|
|
new URL(imageUrls.get(1)).openStream().readAllBytes(), "embedded-image");
|
|
|
|
float scale2 = Math.min(
|
|
|
|
float scale2 = Math.min(
|
|
|
|
imageAvailableWidth / image2.getWidth(),
|
|
|
|
imageAvailableWidth / image2.getWidth(),
|
|
|
|
imageHeightPer / image2.getHeight()
|
|
|
|
imageHeightPer / image2.getHeight()
|
|
|
|
@ -648,7 +334,6 @@ public class PDFGenerator {
|
|
|
|
TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
startY - CELL_PADDING - scaledHeight1 - CELL_PADDING - scaledHeight2,
|
|
|
|
startY - CELL_PADDING - scaledHeight1 - CELL_PADDING - scaledHeight2,
|
|
|
|
scaledWidth2, scaledHeight2);
|
|
|
|
scaledWidth2, scaledHeight2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* // 双图片模式:一行并列显示
|
|
|
|
/* // 双图片模式:一行并列显示
|
|
|
|
float imageWidthPer = (imageAvailableWidth - CELL_PADDING) / 2;
|
|
|
|
float imageWidthPer = (imageAvailableWidth - CELL_PADDING) / 2;
|
|
|
|
|
|
|
|
|
|
|
|
@ -693,239 +378,8 @@ public class PDFGenerator {
|
|
|
|
TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING + imageWidthPer + CELL_PADDING,
|
|
|
|
TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING + imageWidthPer + CELL_PADDING,
|
|
|
|
startY - CELL_PADDING - scaledHeight2,
|
|
|
|
startY - CELL_PADDING - scaledHeight2,
|
|
|
|
scaledWidth2, scaledHeight2);*/
|
|
|
|
scaledWidth2, scaledHeight2);*/
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 多图片模式(超过2张图片):垂直堆叠显示,与双图片模式保持一致的样式
|
|
|
|
|
|
|
|
float imageHeightPer = (imageAvailableHeight - CELL_PADDING * (imageUrls.size() - 1)) / imageUrls.size();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制标题(垂直居中)
|
|
|
|
|
|
|
|
float titleY = startY - totalHeight / 2 + (titleLines.size() * LINE_HEIGHT) / 2;
|
|
|
|
|
|
|
|
cs.beginText();
|
|
|
|
|
|
|
|
cs.setFont(font, FONT_SIZE);
|
|
|
|
|
|
|
|
cs.newLineAtOffset(TABLE_MARGIN + CELL_PADDING, titleY);
|
|
|
|
|
|
|
|
for (String line : titleLines) {
|
|
|
|
|
|
|
|
cs.showText(line);
|
|
|
|
|
|
|
|
titleY -= LINE_HEIGHT;
|
|
|
|
|
|
|
|
cs.newLineAtOffset(0, -LINE_HEIGHT);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cs.endText();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制所有图片,采用与单/双图片一致的样式
|
|
|
|
|
|
|
|
float currentY = startY - CELL_PADDING;
|
|
|
|
|
|
|
|
for (int i = 0; i < imageUrls.size(); i++) {
|
|
|
|
|
|
|
|
PDImageXObject image = loadImageFromUrl(doc, imageUrls.get(i));
|
|
|
|
|
|
|
|
if (image == null) continue; // 跳过加载失败的图片
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float scale = Math.min(
|
|
|
|
|
|
|
|
imageAvailableWidth / image.getWidth(),
|
|
|
|
|
|
|
|
imageHeightPer / image.getHeight()
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
float scaledHeight = image.getHeight() * scale;
|
|
|
|
|
|
|
|
float scaledWidth = image.getWidth() * scale;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否需要跨页
|
|
|
|
|
|
|
|
if (currentY - scaledHeight - CELL_PADDING < TABLE_MARGIN) {
|
|
|
|
|
|
|
|
// 当前页面空间不足,需要跨页
|
|
|
|
|
|
|
|
// 这里需要在实际应用中处理跨页逻辑
|
|
|
|
|
|
|
|
// 由于在单个drawImageGroup方法中无法直接创建新页面,
|
|
|
|
|
|
|
|
// 需要在调用此方法的循环中处理跨页
|
|
|
|
|
|
|
|
break; // 暂停绘制剩余图片,由外部循环处理跨页
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制当前图片
|
|
|
|
|
|
|
|
cs.drawImage(image,
|
|
|
|
|
|
|
|
TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
|
|
|
|
currentY - scaledHeight,
|
|
|
|
|
|
|
|
scaledWidth, scaledHeight);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新下一个图片的起始Y坐标
|
|
|
|
|
|
|
|
currentY -= (scaledHeight + CELL_PADDING);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private static void drawImageGroup(PDPageContentStream cs, PDDocument doc, PDType0Font font,
|
|
|
|
|
|
|
|
// List<String> titleLines, List<String> imageUrls,
|
|
|
|
|
|
|
|
// float startY, float totalHeight) throws IOException {
|
|
|
|
|
|
|
|
// // 绘制单元格边框
|
|
|
|
|
|
|
|
// cs.setLineWidth(0.5f);
|
|
|
|
|
|
|
|
// cs.addRect(TABLE_MARGIN, startY - totalHeight, LEFT_COL_WIDTH, totalHeight);
|
|
|
|
|
|
|
|
// cs.addRect(TABLE_MARGIN + LEFT_COL_WIDTH, startY - totalHeight, RIGHT_COL_WIDTH, totalHeight);
|
|
|
|
|
|
|
|
// cs.stroke();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 计算每张图片的可用高度(根据图片数量动态调整)
|
|
|
|
|
|
|
|
// float imageAvailableHeight = totalHeight - 2 * CELL_PADDING;
|
|
|
|
|
|
|
|
// float imageAvailableWidth = RIGHT_COL_WIDTH - 2 * CELL_PADDING;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (imageUrls.size() == 1) {
|
|
|
|
|
|
|
|
// // 单图片模式:一页两行,每行占半页高度
|
|
|
|
|
|
|
|
// float singleImageHeight = imageAvailableHeight;
|
|
|
|
|
|
|
|
// // 绘制标题(垂直居中)
|
|
|
|
|
|
|
|
// float titleY = startY - totalHeight/2 + (titleLines.size() * LINE_HEIGHT)/2;
|
|
|
|
|
|
|
|
// // 绘制第一行标题(顶部)
|
|
|
|
|
|
|
|
// // float titleY = startY - CELL_PADDING - FONT_SIZE;
|
|
|
|
|
|
|
|
// cs.beginText();
|
|
|
|
|
|
|
|
// cs.setFont(font, FONT_SIZE);
|
|
|
|
|
|
|
|
// cs.newLineAtOffset(TABLE_MARGIN + CELL_PADDING, titleY);
|
|
|
|
|
|
|
|
// for (String line : titleLines) {
|
|
|
|
|
|
|
|
// cs.showText(line);
|
|
|
|
|
|
|
|
// titleY -= LINE_HEIGHT;
|
|
|
|
|
|
|
|
// cs.newLineAtOffset(0, -LINE_HEIGHT);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// cs.endText();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制第一行图片(自动缩放)
|
|
|
|
|
|
|
|
// PDImageXObject image1 = PDImageXObject.createFromByteArray(doc,
|
|
|
|
|
|
|
|
// new URL(imageUrls.get(0)).openStream().readAllBytes(), "embedded-image");
|
|
|
|
|
|
|
|
// float scale1 = Math.min(
|
|
|
|
|
|
|
|
// imageAvailableWidth / image1.getWidth(),
|
|
|
|
|
|
|
|
// singleImageHeight / image1.getHeight()
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// float scaledHeight1 = image1.getHeight() * scale1;
|
|
|
|
|
|
|
|
// float scaledWidth1 = image1.getWidth() * scale1;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// cs.drawImage(image1,
|
|
|
|
|
|
|
|
// TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
|
|
|
|
// startY - CELL_PADDING - scaledHeight1,
|
|
|
|
|
|
|
|
// scaledWidth1, scaledHeight1);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// } else if (imageUrls.size() == 2) {
|
|
|
|
|
|
|
|
// // 双图片模式:改为上下布局
|
|
|
|
|
|
|
|
// float imageHeightPer = (imageAvailableHeight - CELL_PADDING) / 2;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制标题(垂直居中)
|
|
|
|
|
|
|
|
// float titleY = startY - totalHeight/2 + (titleLines.size() * LINE_HEIGHT)/2;
|
|
|
|
|
|
|
|
// cs.beginText();
|
|
|
|
|
|
|
|
// cs.setFont(font, FONT_SIZE);
|
|
|
|
|
|
|
|
// cs.newLineAtOffset(TABLE_MARGIN + CELL_PADDING, titleY);
|
|
|
|
|
|
|
|
// for (String line : titleLines) {
|
|
|
|
|
|
|
|
// cs.showText(line);
|
|
|
|
|
|
|
|
// titleY -= LINE_HEIGHT;
|
|
|
|
|
|
|
|
// cs.newLineAtOffset(0, -LINE_HEIGHT);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// cs.endText();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制第一张图片(上)
|
|
|
|
|
|
|
|
// PDImageXObject image1 = PDImageXObject.createFromByteArray(doc,
|
|
|
|
|
|
|
|
// new URL(imageUrls.get(0)).openStream().readAllBytes(), "embedded-image");
|
|
|
|
|
|
|
|
// float scale1 = Math.min(
|
|
|
|
|
|
|
|
// imageAvailableWidth / image1.getWidth(),
|
|
|
|
|
|
|
|
// imageHeightPer / image1.getHeight()
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// float scaledHeight1 = image1.getHeight() * scale1;
|
|
|
|
|
|
|
|
// float scaledWidth1 = image1.getWidth() * scale1;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// cs.drawImage(image1,
|
|
|
|
|
|
|
|
// TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
|
|
|
|
// startY - CELL_PADDING - scaledHeight1,
|
|
|
|
|
|
|
|
// scaledWidth1, scaledHeight1);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制第二张图片(下)
|
|
|
|
|
|
|
|
// PDImageXObject image2 = PDImageXObject.createFromByteArray(doc,
|
|
|
|
|
|
|
|
// new URL(imageUrls.get(1)).openStream().readAllBytes(), "embedded-image");
|
|
|
|
|
|
|
|
// float scale2 = Math.min(
|
|
|
|
|
|
|
|
// imageAvailableWidth / image2.getWidth(),
|
|
|
|
|
|
|
|
// imageHeightPer / image2.getHeight()
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// float scaledHeight2 = image2.getHeight() * scale2;
|
|
|
|
|
|
|
|
// float scaledWidth2 = image2.getWidth() * scale2;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// cs.drawImage(image2,
|
|
|
|
|
|
|
|
// TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
|
|
|
|
// startY - CELL_PADDING - scaledHeight1 - CELL_PADDING - scaledHeight2,
|
|
|
|
|
|
|
|
// scaledWidth2, scaledHeight2);
|
|
|
|
|
|
|
|
// /* // 双图片模式:一行并列显示
|
|
|
|
|
|
|
|
// float imageWidthPer = (imageAvailableWidth - CELL_PADDING) / 2;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制标题(垂直居中)
|
|
|
|
|
|
|
|
// float titleY = startY - totalHeight/2 + (titleLines.size() * LINE_HEIGHT)/2;
|
|
|
|
|
|
|
|
// cs.beginText();
|
|
|
|
|
|
|
|
// cs.setFont(font, FONT_SIZE);
|
|
|
|
|
|
|
|
// cs.newLineAtOffset(TABLE_MARGIN + CELL_PADDING, titleY);
|
|
|
|
|
|
|
|
// for (String line : titleLines) {
|
|
|
|
|
|
|
|
// cs.showText(line);
|
|
|
|
|
|
|
|
// titleY -= LINE_HEIGHT;
|
|
|
|
|
|
|
|
// cs.newLineAtOffset(0, -LINE_HEIGHT);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// cs.endText();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制第一张图片(左)
|
|
|
|
|
|
|
|
// PDImageXObject image1 = PDImageXObject.createFromByteArray(doc,
|
|
|
|
|
|
|
|
// new URL(imageUrls.get(0)).openStream().readAllBytes(), "embedded-image");
|
|
|
|
|
|
|
|
// float scale1 = Math.min(
|
|
|
|
|
|
|
|
// imageWidthPer / image1.getWidth(),
|
|
|
|
|
|
|
|
// imageAvailableHeight / image1.getHeight()
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// float scaledHeight1 = image1.getHeight() * scale1;
|
|
|
|
|
|
|
|
// float scaledWidth1 = image1.getWidth() * scale1;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// cs.drawImage(image1,
|
|
|
|
|
|
|
|
// TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
|
|
|
|
// startY - CELL_PADDING - scaledHeight1,
|
|
|
|
|
|
|
|
// scaledWidth1, scaledHeight1);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制第二张图片(右)
|
|
|
|
|
|
|
|
// PDImageXObject image2 = PDImageXObject.createFromByteArray(doc,
|
|
|
|
|
|
|
|
// new URL(imageUrls.get(1)).openStream().readAllBytes(), "embedded-image");
|
|
|
|
|
|
|
|
// float scale2 = Math.min(
|
|
|
|
|
|
|
|
// imageWidthPer / image2.getWidth(),
|
|
|
|
|
|
|
|
// imageAvailableHeight / image2.getHeight()
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// float scaledHeight2 = image2.getHeight() * scale2;
|
|
|
|
|
|
|
|
// float scaledWidth2 = image2.getWidth() * scale2;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// cs.drawImage(image2,
|
|
|
|
|
|
|
|
// TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING + imageWidthPer + CELL_PADDING,
|
|
|
|
|
|
|
|
// startY - CELL_PADDING - scaledHeight2,
|
|
|
|
|
|
|
|
// scaledWidth2, scaledHeight2);*/
|
|
|
|
|
|
|
|
// }else {
|
|
|
|
|
|
|
|
// // 多图片模式(超过2张图片):垂直堆叠显示,与双图片模式保持一致的样式
|
|
|
|
|
|
|
|
// float imageHeightPer = (imageAvailableHeight - CELL_PADDING * (imageUrls.size() - 1)) / imageUrls.size();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制标题(垂直居中)
|
|
|
|
|
|
|
|
// float titleY = startY - totalHeight / 2 + (titleLines.size() * LINE_HEIGHT) / 2;
|
|
|
|
|
|
|
|
// cs.beginText();
|
|
|
|
|
|
|
|
// cs.setFont(font, FONT_SIZE);
|
|
|
|
|
|
|
|
// cs.newLineAtOffset(TABLE_MARGIN + CELL_PADDING, titleY);
|
|
|
|
|
|
|
|
// for (String line : titleLines) {
|
|
|
|
|
|
|
|
// cs.showText(line);
|
|
|
|
|
|
|
|
// titleY -= LINE_HEIGHT;
|
|
|
|
|
|
|
|
// cs.newLineAtOffset(0, -LINE_HEIGHT);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// cs.endText();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制所有图片,采用与单/双图片一致的样式
|
|
|
|
|
|
|
|
// float currentY = startY - CELL_PADDING;
|
|
|
|
|
|
|
|
// for (int i = 0; i < imageUrls.size(); i++) {
|
|
|
|
|
|
|
|
// PDImageXObject image = PDImageXObject.createFromByteArray(doc,
|
|
|
|
|
|
|
|
// new URL(imageUrls.get(i)).openStream().readAllBytes(), "embedded-image");
|
|
|
|
|
|
|
|
// float scale = Math.min(
|
|
|
|
|
|
|
|
// imageAvailableWidth / image.getWidth(),
|
|
|
|
|
|
|
|
// imageHeightPer / image.getHeight()
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// float scaledHeight = image.getHeight() * scale;
|
|
|
|
|
|
|
|
// float scaledWidth = image.getWidth() * scale;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 检查是否需要跨页
|
|
|
|
|
|
|
|
// if (currentY - scaledHeight - CELL_PADDING < TABLE_MARGIN) {
|
|
|
|
|
|
|
|
// // 当前页面空间不足,需要跨页
|
|
|
|
|
|
|
|
// // 这里需要在实际应用中处理跨页逻辑
|
|
|
|
|
|
|
|
// // 由于在单个drawImageGroup方法中无法直接创建新页面,
|
|
|
|
|
|
|
|
// // 需要在调用此方法的循环中处理跨页
|
|
|
|
|
|
|
|
// break; // 暂停绘制剩余图片,由外部循环处理跨页
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制当前图片
|
|
|
|
|
|
|
|
// cs.drawImage(image,
|
|
|
|
|
|
|
|
// TABLE_MARGIN + LEFT_COL_WIDTH + CELL_PADDING,
|
|
|
|
|
|
|
|
// currentY - scaledHeight,
|
|
|
|
|
|
|
|
// scaledWidth, scaledHeight);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 更新下一个图片的起始Y坐标
|
|
|
|
|
|
|
|
// currentY -= (scaledHeight + CELL_PADDING);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
private static float calculateAdjustedImagesHeight(PDDocument doc, List<String> imageUrls,
|
|
|
|
private static float calculateAdjustedImagesHeight(PDDocument doc, List<String> imageUrls,
|
|
|
|
float maxWidth, float maxHeight) throws IOException {
|
|
|
|
float maxWidth, float maxHeight) throws IOException {
|
|
|
|
if (imageUrls.isEmpty()) return 0;
|
|
|
|
if (imageUrls.isEmpty()) return 0;
|
|
|
|
@ -934,28 +388,14 @@ public class PDFGenerator {
|
|
|
|
if (imageUrls.size() == 1) {
|
|
|
|
if (imageUrls.size() == 1) {
|
|
|
|
// 单图片模式:占用半页高度
|
|
|
|
// 单图片模式:占用半页高度
|
|
|
|
return maxHeight / 2;
|
|
|
|
return maxHeight / 2;
|
|
|
|
} else if (imageUrls.size() == 2) {
|
|
|
|
|
|
|
|
// 双图片模式:整页高度
|
|
|
|
|
|
|
|
return maxHeight;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// 多图片模式:根据图片数量动态计算高度
|
|
|
|
// 双图片模式:整页高度
|
|
|
|
// 每张图片预留最小高度,防止页面过长
|
|
|
|
|
|
|
|
float minImageHeight = 50f; // 每张图片最小高度
|
|
|
|
|
|
|
|
float imageSpacing = 5f; // 图片间距
|
|
|
|
|
|
|
|
float totalSpacing = (imageUrls.size() - 1) * imageSpacing;
|
|
|
|
|
|
|
|
float estimatedHeight = (minImageHeight * imageUrls.size()) + totalSpacing;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果估算高度超过页面可用高度,则返回最大高度,需要分页处理
|
|
|
|
|
|
|
|
if (estimatedHeight > maxHeight) {
|
|
|
|
|
|
|
|
return maxHeight;
|
|
|
|
return maxHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return estimatedHeight;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 截断超长文本
|
|
|
|
// 截断超长文本
|
|
|
|
private static String truncateText(PDType0Font font, String text, float maxWidth) throws IOException {
|
|
|
|
private static String truncateText(PDType0Font font,String text, float maxWidth) throws IOException {
|
|
|
|
// PDType0Font font = ...; // 获取字体对象
|
|
|
|
// PDType0Font font = ...; // 获取字体对象
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
float currentWidth = 0;
|
|
|
|
float currentWidth = 0;
|
|
|
|
@ -981,8 +421,8 @@ public class PDFGenerator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 简单估算:假设每个中文字符宽度等于字体大小
|
|
|
|
// 简单估算:假设每个中文字符宽度等于字体大小
|
|
|
|
int charsPerLine = (int) (maxWidth / fontSize);
|
|
|
|
int charsPerLine = (int)(maxWidth / fontSize);
|
|
|
|
int lineCount = (int) Math.ceil((double) text.length() / charsPerLine);
|
|
|
|
int lineCount = (int)Math.ceil((double)text.length() / charsPerLine);
|
|
|
|
|
|
|
|
|
|
|
|
return lineCount * fontSize * 1.2f; // 行间距1.2倍
|
|
|
|
return lineCount * fontSize * 1.2f; // 行间距1.2倍
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -995,7 +435,7 @@ public class PDFGenerator {
|
|
|
|
cs.setLeading(fontSize * 1.2f); // 行间距
|
|
|
|
cs.setLeading(fontSize * 1.2f); // 行间距
|
|
|
|
|
|
|
|
|
|
|
|
// 简单换行实现
|
|
|
|
// 简单换行实现
|
|
|
|
int charsPerLine = (int) (maxWidth / (fontSize * 0.8)); // 中文字符估算
|
|
|
|
int charsPerLine = (int)(maxWidth / (fontSize * 0.8)); // 中文字符估算
|
|
|
|
int pos = 0;
|
|
|
|
int pos = 0;
|
|
|
|
float currentY = startY;
|
|
|
|
float currentY = startY;
|
|
|
|
|
|
|
|
|
|
|
|
@ -1012,7 +452,6 @@ public class PDFGenerator {
|
|
|
|
currentY -= fontSize * 1.2f;
|
|
|
|
currentY -= fontSize * 1.2f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 图片高度预估方法
|
|
|
|
// 图片高度预估方法
|
|
|
|
private static float estimateImageHeight(String imageUrl, float targetWidth) throws IOException {
|
|
|
|
private static float estimateImageHeight(String imageUrl, float targetWidth) throws IOException {
|
|
|
|
try (InputStream is = new URL(imageUrl).openStream()) {
|
|
|
|
try (InputStream is = new URL(imageUrl).openStream()) {
|
|
|
|
@ -1150,6 +589,8 @@ public class PDFGenerator {
|
|
|
|
}*/
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void drawTable(PDPageContentStream cs, PDType0Font font,
|
|
|
|
private static void drawTable(PDPageContentStream cs, PDType0Font font,
|
|
|
|
float x, float y, String[][] data, float[] colWidths) throws IOException {
|
|
|
|
float x, float y, String[][] data, float[] colWidths) throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
@ -1174,7 +615,7 @@ public class PDFGenerator {
|
|
|
|
// 绘制列分隔线
|
|
|
|
// 绘制列分隔线
|
|
|
|
float currentX = x;
|
|
|
|
float currentX = x;
|
|
|
|
for (int i = 1; i < colWidths.length; i++) {
|
|
|
|
for (int i = 1; i < colWidths.length; i++) {
|
|
|
|
currentX += colWidths[i - 1];
|
|
|
|
currentX += colWidths[i-1];
|
|
|
|
cs.moveTo(currentX, y);
|
|
|
|
cs.moveTo(currentX, y);
|
|
|
|
cs.lineTo(currentX, y - ROW_HEIGHT * data.length);
|
|
|
|
cs.lineTo(currentX, y - ROW_HEIGHT * data.length);
|
|
|
|
cs.stroke();
|
|
|
|
cs.stroke();
|
|
|
|
@ -1211,13 +652,13 @@ public class PDFGenerator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static float addImageRow(PDPageContentStream cs, PDDocument doc,
|
|
|
|
private static float addImageRow(PDPageContentStream cs, PDDocument doc,
|
|
|
|
PDType0Font font, String title, String imageUrl,
|
|
|
|
PDType0Font font, String title, String imageUrl,
|
|
|
|
float startX, float startY) throws IOException {
|
|
|
|
float startX, float startY) throws IOException {
|
|
|
|
// 加载图片
|
|
|
|
// 加载图片
|
|
|
|
PDImageXObject image = loadImageFromUrl(doc, imageUrl);
|
|
|
|
PDImageXObject image = PDImageXObject.createFromByteArray(doc,
|
|
|
|
if (image == null) return startY; // 如果图片加载失败,返回原位置
|
|
|
|
new URL(imageUrl).openStream().readAllBytes(), "embedded-image");
|
|
|
|
|
|
|
|
|
|
|
|
// 动态计算尺寸
|
|
|
|
// 动态计算尺寸
|
|
|
|
float imageWidth = RIGHT_COL_WIDTH - 10;
|
|
|
|
float imageWidth = RIGHT_COL_WIDTH - 10;
|
|
|
|
float scale = imageWidth / image.getWidth();
|
|
|
|
float scale = imageWidth / image.getWidth();
|
|
|
|
@ -1231,7 +672,7 @@ public class PDFGenerator {
|
|
|
|
cs.stroke();
|
|
|
|
cs.stroke();
|
|
|
|
|
|
|
|
|
|
|
|
// 垂直居中文本(改进算法)
|
|
|
|
// 垂直居中文本(改进算法)
|
|
|
|
float textY = startY - rowHeight / 2 + FONT_SIZE / 2;
|
|
|
|
float textY = startY - rowHeight/2 + FONT_SIZE/2;
|
|
|
|
//String title = "安装完成探测器工作图片\n检测时间:2025-08-14";
|
|
|
|
//String title = "安装完成探测器工作图片\n检测时间:2025-08-14";
|
|
|
|
|
|
|
|
|
|
|
|
/* if(title.contains("\n")){
|
|
|
|
/* if(title.contains("\n")){
|
|
|
|
@ -1239,7 +680,7 @@ public class PDFGenerator {
|
|
|
|
textY = startY - rowHeight/2 + FONT_SIZE/2 + (lines.length-1)*FONT_SIZE/2;
|
|
|
|
textY = startY - rowHeight/2 + FONT_SIZE/2 + (lines.length-1)*FONT_SIZE/2;
|
|
|
|
}*/
|
|
|
|
}*/
|
|
|
|
drawCenteredText(cs, font, FONT_SIZE, title,
|
|
|
|
drawCenteredText(cs, font, FONT_SIZE, title,
|
|
|
|
startX + LEFT_COL_WIDTH / 2, textY);
|
|
|
|
startX + LEFT_COL_WIDTH/2, textY);
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制图片(带5pt边距)
|
|
|
|
// 绘制图片(带5pt边距)
|
|
|
|
cs.drawImage(image,
|
|
|
|
cs.drawImage(image,
|
|
|
|
@ -1284,79 +725,6 @@ public class PDFGenerator {
|
|
|
|
return startY - dynamicRowHeight - 10;*/
|
|
|
|
return startY - dynamicRowHeight - 10;*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private static float addImageRow(PDPageContentStream cs, PDDocument doc,
|
|
|
|
|
|
|
|
// PDType0Font font, String title, String imageUrl,
|
|
|
|
|
|
|
|
// float startX, float startY) throws IOException {
|
|
|
|
|
|
|
|
// // 加载图片
|
|
|
|
|
|
|
|
// PDImageXObject image = PDImageXObject.createFromByteArray(doc,
|
|
|
|
|
|
|
|
// new URL(imageUrl).openStream().readAllBytes(), "embedded-image");
|
|
|
|
|
|
|
|
// // 动态计算尺寸
|
|
|
|
|
|
|
|
// float imageWidth = RIGHT_COL_WIDTH - 10;
|
|
|
|
|
|
|
|
// float scale = imageWidth / image.getWidth();
|
|
|
|
|
|
|
|
// float imageHeight = image.getHeight() * scale;
|
|
|
|
|
|
|
|
// float rowHeight = imageHeight + 20;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制单元格边框
|
|
|
|
|
|
|
|
// cs.setLineWidth(0.5f);
|
|
|
|
|
|
|
|
// cs.addRect(startX, startY - rowHeight, LEFT_COL_WIDTH, rowHeight);
|
|
|
|
|
|
|
|
// cs.addRect(startX + LEFT_COL_WIDTH, startY - rowHeight, RIGHT_COL_WIDTH, rowHeight);
|
|
|
|
|
|
|
|
// cs.stroke();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 垂直居中文本(改进算法)
|
|
|
|
|
|
|
|
// float textY = startY - rowHeight/2 + FONT_SIZE/2;
|
|
|
|
|
|
|
|
// //String title = "安装完成探测器工作图片\n检测时间:2025-08-14";
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// /* if(title.contains("\n")){
|
|
|
|
|
|
|
|
// String[] lines = title.split("\n");
|
|
|
|
|
|
|
|
// textY = startY - rowHeight/2 + FONT_SIZE/2 + (lines.length-1)*FONT_SIZE/2;
|
|
|
|
|
|
|
|
// }*/
|
|
|
|
|
|
|
|
// drawCenteredText(cs, font, FONT_SIZE, title,
|
|
|
|
|
|
|
|
// startX + LEFT_COL_WIDTH/2, textY);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制图片(带5pt边距)
|
|
|
|
|
|
|
|
// cs.drawImage(image,
|
|
|
|
|
|
|
|
// startX + LEFT_COL_WIDTH + 5,
|
|
|
|
|
|
|
|
// startY - rowHeight + 10,
|
|
|
|
|
|
|
|
// imageWidth, imageHeight);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// return startY - rowHeight - 10;
|
|
|
|
|
|
|
|
// /*// 计算图片尺寸(保持比例)
|
|
|
|
|
|
|
|
// float imageWidth = RIGHT_COL_WIDTH - 10;
|
|
|
|
|
|
|
|
// float scale = imageWidth / image.getWidth();
|
|
|
|
|
|
|
|
// float imageHeight = image.getHeight() * scale;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 动态计算行高(图片高度+上下边距20)
|
|
|
|
|
|
|
|
// float dynamicRowHeight = imageHeight + 20;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制左侧标题单元格
|
|
|
|
|
|
|
|
// cs.setLineWidth(0.5f);
|
|
|
|
|
|
|
|
// cs.addRect(TABLE_MARGIN, startY - dynamicRowHeight, LEFT_COL_WIDTH, dynamicRowHeight);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 绘制右侧图片单元格(高度与左侧同步)
|
|
|
|
|
|
|
|
// cs.addRect(TABLE_MARGIN + LEFT_COL_WIDTH, startY - dynamicRowHeight, RIGHT_COL_WIDTH, dynamicRowHeight);
|
|
|
|
|
|
|
|
// cs.stroke();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 居中绘制标题文本
|
|
|
|
|
|
|
|
// float titleWidth = font.getStringWidth(title) / 1000 * FONT_SIZE;
|
|
|
|
|
|
|
|
// float titleX = TABLE_MARGIN + (LEFT_COL_WIDTH - titleWidth) / 2;
|
|
|
|
|
|
|
|
// float titleY = startY - (dynamicRowHeight / 2) - (FONT_SIZE / 3);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// cs.beginText();
|
|
|
|
|
|
|
|
// cs.setFont(font, FONT_SIZE);
|
|
|
|
|
|
|
|
// cs.newLineAtOffset(titleX, titleY);
|
|
|
|
|
|
|
|
// cs.showText(title);
|
|
|
|
|
|
|
|
// cs.endText();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 居中绘制图片
|
|
|
|
|
|
|
|
// float imageX = TABLE_MARGIN + LEFT_COL_WIDTH + 5;
|
|
|
|
|
|
|
|
// float imageY = startY - dynamicRowHeight + 10;
|
|
|
|
|
|
|
|
// cs.drawImage(image, imageX, imageY, imageWidth, imageHeight);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 返回新的Y坐标(当前位置减去行高和间距)
|
|
|
|
|
|
|
|
// return startY - dynamicRowHeight - 10;*/
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* private static float addImageRow(PDPageContentStream cs, PDDocument doc,
|
|
|
|
/* private static float addImageRow(PDPageContentStream cs, PDDocument doc,
|
|
|
|
PDType0Font font, String title, String imageUrl, float x, float y) throws IOException {
|
|
|
|
PDType0Font font, String title, String imageUrl, float x, float y) throws IOException {
|
|
|
|
// 绘制标题行
|
|
|
|
// 绘制标题行
|
|
|
|
|