- 对象转map
Map<String, String> map = JSON.parseObject(JSONUtil.toJsonStr(platform), new TypeReference<Map<String, String>>() {
});
- 生成pdf
ByteArrayOutputStream byteArrayOutputStream = null;
try {
log.info("电签流程生成pdf成功");
byteArrayOutputStream = PdfUtils.genPdf(map);
}catch (Exception e) {
log.error("电签流程生成pdf失败 {}", e.getMessage());
throw new BusinessException(EnumBizCode.ERROR_CODE_FAILED_CREATE_PDF);
}
- 工具类
package com.bison.teamwork.util;
import com.bison.common.constant.BusinessException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.*;
import java.util.Map;
@Slf4j
public class PdfUtils {
public static ByteArrayOutputStream genPdf(Map map) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = inputStreamToFile();
return fillParam(map, byteArrayOutputStream.toByteArray());
}
public static ByteArrayOutputStream fillParam(Map<String, String> fieldValueMap, byte[] file) {
ByteArrayOutputStream byteArrayOutputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
PdfReader reader = null;
PdfStamper stamper = null;
BaseFont base = null;
try {
reader = new PdfReader(file);
stamper = new PdfStamper(reader, byteArrayOutputStream);
stamper.setFormFlattening(true);
base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
AcroFields acroFields = stamper.getAcroFields();
for (String key : acroFields.getFields().keySet()) {
acroFields.setFieldProperty(key, "textfont", base, null);
acroFields.setFieldProperty(key, "textsize", new Float(12), null);
}
if (fieldValueMap != null) {
for (String fieldName : fieldValueMap.keySet()) {
acroFields.setField(fieldName, fieldValueMap.get(fieldName));
}
}
} catch (Exception e) {
throw new BusinessException(e.getMessage());
} finally {
if (stamper != null) {
try {
stamper.close();
} catch (Exception e) {
throw new BusinessException(e.getMessage());
}
}
if (reader != null) {
reader.close();
}
}
} catch (Exception e) {
log.error("填充参数异常");
throw new BusinessException(e.getMessage());
} finally {
IOUtils.closeQuietly(byteArrayOutputStream);
}
return byteArrayOutputStream;
}
public static File inputStreamToFile(){
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
org.springframework.core.io.Resource[] resources = resolver.getResources("verify" + File.separator + "verify.pdf");
org.springframework.core.io.Resource resource = resources[0];
InputStream ins = resource.getInputStream();
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + "verify.pdf");
if (file.exists()) {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
} catch (IOException e) {
log.error("获取文件异常");
throw new BusinessException(e.getMessage());
}
}
public static ByteArrayOutputStream inputStreamToFile() {
InputStream ins = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
org.springframework.core.io.Resource[] resources = resolver.getResources("verify" + File.separator + "verify.pdf");
org.springframework.core.io.Resource resource = resources[0];
ins = resource.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
return byteArrayOutputStream;
} catch (IOException e) {
log.error("获取文件异常");
throw new BusinessException(e.getMessage());
} finally {
IoUtil.close(ins);
IoUtil.close(byteArrayOutputStream);
}
}
}
- 引入的包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.11</version>
</dependency>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?