1. 程式人生 > 其它 >java實現pdf模板生成並列印

java實現pdf模板生成並列印

(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;
}

效果: