- 对象转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;
/**
* Description: PdfUtils <br>
* 依赖的包:itextpdf itext-asian
* commons-io,commons-codec
*/
@Slf4j
public class PdfUtils {
public static ByteArrayOutputStream genPdf(Map map) throws IOException {
File templateFile = inputStreamToFile();
return fillParam(map, FileUtils.readFileToByteArray(templateFile));
}
/**
* Description: 使用map中的参数填充pdf,map中的key和pdf表单中的field对应 <br>
*/
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;
}
/***
* linux下获取文件方式
* @param
* @return {@link File}
* @date 2022/10/26
**/
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());
}
}
}
- 引入的包
<!-- pdf文件依赖包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.2.0</version>
</dependency>
<!-- pdf字体依赖包 -->
<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>
标签:java,new,表单,itextpdf,io,import,pdf,null
From: https://www.cnblogs.com/fuqian/p/16828048.html