模板PDF推荐使用万兴PDF工具破解版调整表单域和表单域名称,入参后的PDF需要设置入参字体和扁平化来保证PDF可以直接显示入参参数。可以防止出现打开PDF显示
文本域
// OSS上的PDF模板文件URL
private static final String OSS_PDF_TEMPLATE_URL = "";
@PostMapping("/oss/uploadSignedPdf")
public Result<String> uploadSignedPdfToOSS(@RequestBody JSONObject requestBody) {
try {
// 参数校验
String idCard = CommonUtils.processNullableValue(requestBody.getString("idCard"), "");
String email = CommonUtils.processNullableValue(requestBody.getString("email"), "");
String phone = CommonUtils.processNullableValue(requestBody.getString("phone"), "");
String signatureBase64 = CommonUtils.processNullableValue(requestBody.getString("signature"), "");
if (signatureBase64.isEmpty()) {
return Result.error("签名图片不能为空");
}
// 移除Base64字符串中的前缀(如果存在)
if (signatureBase64.startsWith("data:image/png;base64,")) {
signatureBase64 = signatureBase64.substring("data:image/png;base64,".length());
}
// Base64解码签名图片
byte[] signatureBytes = Base64.getDecoder().decode(signatureBase64);
File signatureFile = File.createTempFile("signature_", ".png");
Files.write(signatureFile.toPath(), signatureBytes);
// 创建带签名的PDF(使用PdfReader)
File tempPdf = generateSignedPdf(idCard, email, phone, signatureFile);
// 上传OSS
String relativePath = "contracts/" + phone+tempPdf.getName();
String ossUrl = OssBootUtil.upload(
new FileInputStream(tempPdf),
relativePath
);
// 清理临时文件
signatureFile.delete();
tempPdf.delete();
return Result.OK(ossUrl);
} catch (IOException | IllegalArgumentException | DocumentException e) {
return Result.error("文件处理失败: " + e.getMessage());
}
}
// 签名图片缩放比例常量
private static final float SIGNATURE_IMAGE_SCALE = 0.1f; // 将图片缩小到原来的10%
private static final String SIGNATURE_FORM_FIELD = "signature"; // 签名表单域名称
private File generateSignedPdf(String idCard, String email, String phone, File signature) throws IOException, DocumentException {
// 使用PdfReader读取模板
PdfReader reader = new PdfReader(OSS_PDF_TEMPLATE_URL);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("temp_contract.pdf"));
// 设置中文字体,使用更通用的字体配置以确保在各种设备上都能正常显示
BaseFont baseFont = null;
try {
// 尝试使用iText内置的中文字体
baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (Exception e1) {
try {
// 备选字体方案
baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (Exception e2) {
log.warn("无法加载中文字体,使用默认字体");
}
}
Font signatureFont = baseFont != null ? new Font(baseFont, 12, Font.NORMAL) : new Font();
Image signatureImage = Image.getInstance(signature.getAbsolutePath());
// 填充表单字段
AcroFields form = stamper.getAcroFields();
// 添加字体替代,确保中文正常显示
if (baseFont != null) {
form.addSubstitutionFont(baseFont);
}
// 填充表单字段,使用setValueAsString替代setField,可能在某些情况下更可靠
if (idCard != null && !idCard.isEmpty()) {
form.setField("idCard", idCard);
}
if (email != null && !email.isEmpty()) {
form.setField("email", email);
}
if (phone != null && !phone.isEmpty()) {
form.setField("phone", phone);
}
// 添加签名图片到PDF
List<AcroFields.FieldPosition> fieldPositions = form.getFieldPositions(SIGNATURE_FORM_FIELD);
if (fieldPositions != null && !fieldPositions.isEmpty()) {
for (AcroFields.FieldPosition fieldPosition : fieldPositions) {
int page = fieldPosition.page;
PdfContentByte canvas = stamper.getOverContent(page);
Rectangle signRect = fieldPosition.position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 调整签名图片大小 - 将图片缩小到原来的10%
float width = signatureImage.getWidth() * SIGNATURE_IMAGE_SCALE;
float height = signatureImage.getHeight() * SIGNATURE_IMAGE_SCALE;
signatureImage.scaleToFit(width, height);
// 设置图片位置
signatureImage.setAbsolutePosition(x, y);
canvas.addImage(signatureImage);
}
log.info("成功在{}个页面位置添加签名图片", fieldPositions.size());
} else {
log.warn("未找到签名表单域:{}", SIGNATURE_FORM_FIELD);
}
// 添加当前日期
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = dateFormat.format(new Date());
if (currentDate != null && !currentDate.isEmpty()) {
form.setField("signDate", currentDate);
}
// 设置表单扁平化,但不启用完全压缩,可能有助于移动设备兼容性
stamper.setFormFlattening(true);
stamper.close();
reader.close();
return new File("temp_contract.pdf");
}