🤞SpringBoot项目中基于PDF模板生成PDF文档🤞 常见的使用方法有POI,ITEXT等模块的使用,本次基于ITEXT模块来实现基于PDF模板生成PDF新文件。
<!--pdf依赖-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13</version>
</dependency>
<!--pdf支持中文-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
PDF工具类
package org.dromara.docMake.util;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.itextpdf.text.DocumentException;
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 io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.ResourceUtils;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
/**
* @Author: 知识浅谈
* @Package: org.dromara.docMake.util
* @Date: 2024-5-10 15:16
* @Description: pdf生成工具类
* @Version: 1.0
*/
@Slf4j
public class PdfUtil {
private static boolean isPrimitiveOrWrapper(Class<?> clazz) { //检查传入的类是否为基本类型或基本包装类型
return clazz.isPrimitive() || clazz.getName().startsWith("java.lang");
}
private static Map<String,String> turnMap(Object object) {
Map<String,Object> stringObjectMap = BeanUtil.beanToMap(object); //把对象转为Map
Map<String,String> map = new HashMap<>(stringObjectMap.size()*2);
//打印输出属性名称和属性值
stringObjectMap.forEach((k,v)->{
if(ObjectUtil.isNotEmpty(v)) {
//基本类型和封装类型
if (isPrimitiveOrWrapper(v.getClass())) {
map.put(k, String.valueOf(v));
} else {
//其他类型
if (v instanceof List) {
List<Object> list = (List) v;
for (int i = 0; i < list.size(); i++) {
Object o = list.get(i);
Map<String, Object> stringObjectMap1 = BeanUtil.beanToMap(o);
int finalI = i;
stringObjectMap1.forEach((k1, v1) -> {
map.put(StrUtil.format("{}.{}{}", k, k1, finalI), String.valueOf(v1));
});
}
} else {
Map<String, Object> stringObjectMap1 = BeanUtil.beanToMap(v);
stringObjectMap1.forEach((k1, v1) -> {
map.put(StrUtil.format("{}.{}", k, k1), String.valueOf(v1));
});
}
}
}
});
return map;
}
//生成pdf
public static void generatePdf(HttpServletRequest request,
HttpServletResponse response,
String templatePath,//当前为文件名,之后改为文件路径
Object object,
String newFileName) {
try(OutputStream responseOutputStream = response.getOutputStream(); ByteArrayOutputStream fileOut= new ByteArrayOutputStream()){
if(!newFileName.endsWith(".pdf")) newFileName=newFileName+".pdf"; //
//模版在项目中的位置
Resource resource = new PathMatchingResourcePatternResolver().getResource(ResourceUtils.CLASSPATH_URL_PREFIX + "pdf/" + templatePath);
PdfReader pdfReader = new PdfReader(resource.getInputStream());
PdfStamper pdfStamper = new PdfStamper(pdfReader, fileOut);
BaseFont font = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
ArrayList<BaseFont> fontList = new ArrayList<>();
fontList.add(font);
//取出报表模版中的所有字段
AcroFields fields = pdfStamper.getAcroFields();
fields.setSubstitutionFonts(fontList);
PdfUtil.fillData(fields,PdfUtil.turnMap(object));
//必须要调用这个,否则文档不会生成的,如果为false,生成的pdf还能编辑,所以设置为true
pdfStamper.setFormFlattening(true);
pdfStamper.close();
writeFile(request,response,fileOut,newFileName);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 填充数据
*/
private static void fillData(AcroFields fields,Map<String,String> data) throws IOException, DocumentException {
Map<String, AcroFields.Item> formFields = fields.getFields();
String v;
for (String k : data.keySet()) {
if(formFields.containsKey(k)){
v = data.get(k);
fields.setField(k,v);
}
}
}
private static void writeFile(HttpServletRequest request,
HttpServletResponse response,
ByteArrayOutputStream fileOut,
String newFileName) throws IOException{
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType("application/pdf");
try{
//这里主要防止下载的pdf文件名乱码
response.setHeader("Content-Disposition","attachment; filename="+ URLEncoder.encode(newFileName,"UTF-8"));
fileOut.writeTo(response.getOutputStream());
response.flushBuffer();
}catch (Exception e){
log.error("pdf生成异常");
throw new RuntimeException("pdf生成异常");
}
}
}
在resources下边创建pdf文件夹,然后使用 Adobe Acrobat DI创建一个PDF模板 在PDF模板中添加表单域,并设置表单域的变量名,便于后续替换
测试
对象实例:
@Data
class A{
private String name;
}
测试方法
@GetMapping("/pdfTest")
public void test(HttpServletRequest request, HttpServletResponse response) {
A a = new A();
a.setName("测试");
PdfUtil.generatePdf(request,response,"test.pdf",a,"测试");
}
然后就生成了一个替换之后的文档。
大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。