(1)Adobe Acrobat pro软件:用来制作导出模板
(2)itext的jar包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/itext/itext -->
<dependency>
<groupId>itext</groupId>
<artifactId>itext</artifactId>
<version>1.3</version>
</dependency>
(3)先用word做出模板界面
(4)将world输出为pdf
(5)用Adobe Acrobat pro软件打开刚刚生成的pdf
(6)工具类引入
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.*;
public class PdfUtil {
/**
*
* @param o 写入的数据
* @param out 自定义保存pdf的文件流
* @param templatePath pdf模板路径
*/
// 利用模板生成pdf
public void fillTemplate(Map<String,Object> o,ServletOutputStream out,String templatePath) {
PdfReader reader;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font FontChinese = new Font(bf, 2, Font.NORMAL);
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
java.util.Iterator<String> it = form.getFields().keySet().iterator();
form.addSubstitutionFont(bf);
while (it.hasNext()) {
String name = it.next().toString();
String value = o.get(name)!=null?o.get(name).toString():null;
form.setField(name,value);
}
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
} catch (IOException e) {
System.out.println(e);
} catch (DocumentException e) {
System.out.println(e);
}
}
}
(7)controller调用
public String materialRequisition(HttpServletResponse response){
//制造假数据,结合前端我打算用map接收
String createDate = "2021/12/7";
String name = "夏帅";
String productName = "70 A125";
String type = "双非";
double num = 50.0;
double price = 10.00;
String orderId = "202112001";
double money = num*price;
String maker = "加急订单";
String library = "苏州";
Map<String,Object> map = new HashMap<>();
map.put("name",name);
map.put("createDate",createDate);
map.put("productName",productName);
map.put("price",price);
map.put("num",num);
map.put("type",type);
map.put("money",money);
map.put("maker",maker);
map.put("library",library);
map.put("orderId",orderId);
/*// 设置response参数,可以打开下载页面
response.reset();
response.setCharacterEncoding("UTF-8");
// 定义输出类型
response.setContentType("application/PDF;charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + "assessment.pdf");*/
try {
ServletOutputStream out = response.getOutputStream();
PdfUtil pdf = new PdfUtil();
//src/main/resources/static/swagger/images/msgh.pdf 模板路径记得更换自己的,我放在项目里面了
pdf.fillTemplate(map ,out,"src/main/resources/static/swagger/images/msgh.pdf");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
效果: