前言:由于项目需要,项目中的pdf之前都是通过前端布局绘画完成,前端绘画非常复杂,得一点一点调布局,间隙,大小等等,于是借助Adobe acrobat dc(正版收费)pdf工具,制作好pdf模板,将需要填充的内容设置为表单,在后端只需处理表单内容即可方便了好多,如果后期布局有调整也只需要用工具调整pdf模板即可
工具类:
public class PdfGenerationUtils {
/**
* 根据模板创建生成pdf
* @param map 模板中的表单数据 key-表单属性值;value-值
* @param templatePath 模板路径
* @return 返回生成的pdf文件路径
*/
public static String createPdfByTemplate(Map<String,Object> map, String templatePath) {
PdfReader reader;
ByteArrayOutputStream bos;
PdfStamper stamper;
//生成的pdf文件存放地址
// String newPdfPath = "src/main/resources/"+(System.currentTimeMillis())+".pdf";
String newPdfPath = "D:/pdf/"+"ceshi"+".pdf";
// String newPdfPath = "src/main/resources/"+"测试"+".pdf";
try {
// 设置字体 宋体
//BaseFont st = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 加载字体
BaseFont hyzh = BaseFont.createFont(
"/font/hyzhj.ttf", // 汉仪中黑简
BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED
);
BaseFont st = BaseFont.createFont("/font/simsun.ttc,1", // 宋体
BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED
);
BaseFont hyssej = BaseFont.createFont("/font/hyss2j.ttf", //汉仪书宋二简
BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED
);
BaseFont fzssjt = BaseFont.createFont("/font/FangZhengShuSongJianTi-1.ttf", //方正书宋简体
BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED
);
//BaseFont hyzh = BaseFont.createFont("/src/main/resources/fonts/hyzhj.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //汉仪中黑 简
//BaseFont fzssjt = BaseFont.createFont("src/main/resources/font/FangZhengShuSongJianTi-1.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //方正书宋简体
// 读取pdf模板
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
//拿到pdf模板中的表单属性
AcroFields form = stamper.getAcroFields();
java.util.Iterator<String> it = form.getFields().keySet().iterator();
//遍历表单属性,对每个属性赋值
while (it.hasNext()) {
String name = it.next();
String value = map.get(name)!=null?map.get(name).toString():null;
// 设置字体-宋体
if(name.equals("year") || name.equals("month") || name.equals("day")) {
form.setFieldProperty(name, "textfont", st, null);
}
// 设置字体-汉仪书宋二简
if(name.equals("createTime") || name.equals("name") || name.equals("orForm") || name.equals("BusinessScope") || name.equals("address") || name.equals("fdPersion") || name.equals("type") || name.equals("tyshxydm") || name.equals("responsiblePerson")) {
form.setFieldProperty(name, "textfont", hyssej, null);
}
// 设置字体-方正书宋简体
if (name.equals("ewmText")){
form.setFieldProperty(name, "textfont", fzssjt, null);
}
form.setField(name, value);
System.out.println(name+"------"+value);
}
// 插入图片
PdfContentByte canvas = stamper.getOverContent(1); // 获取第一页的内容
String erweima_af_image ="*********************************************************";
// 下载图片并转换为Base64
String erweimaBase64 = imageToBase64(erweima_af_image);
// String imageBase64 = (String) map.get("erweima");
// 解码Base64并插入图片
byte[] imageBytes = Base64.getDecoder().decode(erweimaBase64);
Image img = Image.getInstance(imageBytes);
// 设置图片位置和大小
img.setAbsolutePosition(597, 385); // 设置图片的左下角坐标
img.scaleToFit(82.7f, 82.7f); // 设置图片的宽高
canvas.addImage(img);
// 设置为true可编辑,false为不可编辑
stamper.setFormFlattening(true);
stamper.close();
Document doc = new Document();
File file = new File(newPdfPath);
PdfCopy copy = new PdfCopy(doc, new FileOutputStream(file));
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
System.out.println(newPdfPath);
} catch (IOException | DocumentException e) {
throw new BaseException("生成PDF失败,请联系管理员");
}
return newPdfPath;
}
总结:其中可以设置pdf每一个表单的内容字体样式,以及可以填充图片等。
此图为:项目所需要的绘制的pdf表单模板,可以对照工具类使用