根据自定义内容生成pdf文件并上传
需求:根据前端提供数据,生成pdf文件并上传至文件服务器
开干,代码如下:
try {
//设置字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
Document doc = new Document();
String name = "任务修复单" + System.currentTimeMillis() + ".pdf";
PdfWriter.getInstance(doc, new FileOutputStream(name));
doc.open();
//-------------------标题----------------------
Font font1 = new Font(bfChinese, 16, Font.BOLD);
Paragraph paragraphBlue = new Paragraph("修复任务单", font1);
paragraphBlue.setAlignment(Element.ALIGN_CENTER);
paragraphBlue.setSpacingBefore(20);
doc.add(paragraphBlue);
Chunk chunk = new Chunk("日期: " + data.get("date") + " ");
chunk.setFont(fontChinese);
Paragraph paragraphBlue1 = new Paragraph("", fontChinese);
paragraphBlue1.setLeading(20f);// 行间距
paragraphBlue1.setAlignment(Element.ALIGN_RIGHT);
paragraphBlue1.add(chunk);
doc.add(paragraphBlue1);
//创建表格
PdfPTable table = new PdfPTable(2);
//水平居中
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
// 边框宽度
table.getDefaultCell().setBorderWidth(1);
// 单元格内容上下填充
table.getDefaultCell().setPaddingBottom(10);
table.getDefaultCell().setPaddingTop(10);
// 设置两列单元格的宽度
table.setWidths(new int[]{50, 100});
// 单元格赋值
table.addCell(new Paragraph("项目名称", fontChinese));
table.addCell(new Paragraph(data.get("projectName") + "", fontChinese));
table.addCell(new Paragraph("地点", fontChinese));
table.addCell(new Paragraph(data.get("projectAddr") + "", fontChinese));
table.addCell(new Paragraph("单位", fontChinese));
table.addCell(new Paragraph(data.get("constructorUnit") + "", fontChinese));
table.addCell(new Paragraph("内容", fontChinese));
table.addCell(new Paragraph(data.get("fixContent") + "", fontChinese));
table.addCell(new Paragraph("备注", fontChinese));
table.addCell(new Paragraph(data.get("remark") + "", fontChinese));
// 设置表格上面空白宽度
table.setSpacingBefore(10f);
doc.add(table);
doc.close();
// 读取文件
File file = new File(name);
// 下面是我调用自己的公共方法上传文件,可自行修改
MultipartFile cMultiFile=new MockMultipartFile("file",file.getName(),"application/pdf",new FileInputStream(file));
String type=file.getName().substring(file.getName().lastIndexOf(".")+1);
upload= minioUtils.upload(new MultipartFile[]{cMultiFile}, type);
System.out.println(upload);
if (file.exists()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
效果展示:
实现过程中也采用过pdf模板,通过替换内容的方式来生成,但是如果内容过多,由于模板中宽度高度固定,导致显示不完整,不过在这里做一个记录,代码如下:
// 该方法是采用pdf模板,替换模板内容来生成pdf文件,如果内容过多,会显示不全,弃用
PdfReader reader = null;
AcroFields s = null;
PdfStamper ps = null;
ByteArrayOutputStream bos = null;
//设置文件名
String fileName = "fixTaskTemplate_" + DateFormatUtils.format(new Date(), "yyyyMMddhhmmss") + ".pdf";
try {
// fixTaskTemplate.pdf是我自己的模板文件
String file = this.getClass().getClassLoader().getResource("fixTaskTemplate.pdf").getPath();
//设置字体
reader = new PdfReader(file);
bos = new ByteArrayOutputStream();
ps = new PdfStamper(reader, bos);
s = ps.getAcroFields();
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//设置编码格式
s.addSubstitutionFont(bfChinese);
// 遍历data 给pdf表单表格赋值
for (String key : data.keySet()) {
if (data.get(key) != null) {
s.setField(key, data.get(key).toString());
}
}
// 如果为false那么生成的PDF文件还能编辑,一定要设为true
ps.setFormFlattening(true);
ps.close();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bos.toByteArray());
byteArrayInputStream.close();
bos.close();
// 这里是我自己的上传方法,可自行修改
Map upload = minioUtils.upload(fileName, "docx", byteArrayInputStream, byteArrayInputStream.available())
return upload ;
} catch (Exception e) {
System.out.println("读取文件异常");
e.printStackTrace();
return "";
} finally {
try {
bos.close();
reader.close();
} catch (IOException e) {
System.out.println("关闭流异常");
e.printStackTrace();
}
}
这里的模板可使用 万兴pdf 进行编辑:
第一步: 选中表单
第二部:选中文本字段,然后添加属性即可
总结:
第一种方法相对灵活,内容不受限制,但是如果表格相对复杂的情况下,需要考虑如何构造出表格样式;
第二种方法模板固定,操作比较简单,弊端在于内容过多的情况下,可能存在单元格过小,显示不完整的问题。
两种方案,任君选择,可自由发挥想象,如有好的想法或优化点,还请私信我,感谢