springboot+itextpdf按模板⽣成PDF⽂件及在线下载PDF⽂件
最近做项⽬改造,将C项⽬改造成Java项⽬,因为没有⽂档,从⽹上找了很多资料,做成了⽣成⽂件放到本地,后来才发现跑偏了,是在线⽣成⽂件下载,索性两个代码都写到了⼯具类中,⽅⾯以后使⽤。
⽣成本地⽂件和导出⽂件,都是流的问题,⼀个FileOutputStream(路径)⼀个
OutputStream())。
这个功能是将数据库查询到的数据插⼊到固定的模板中。具体操作流程如下:
⾸先在word⽂档中,设定好模板,保存为pdf格式,然后使⽤Adobe Acrobat软件打开,我⽤的是Adobe Acrobat 9 Pro版本的,
⽤到了⽂件和表单两个模块。
在⽂件中打开你刚才保存的模板,然后点击表单,添加或编辑域,就可以⾃⼰选定区域,就可以添加域,域名称就是你要传的值的属性名,对应不上的话就传不上的;这⾥需要注意设置域的长度,如果长度⼩于你的字段长度,会⾃动隐藏掉超出的部分,不会⾃⼰增长,所以要设定的长度尽量的长。
引⼊依赖:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>捣衣诗
<version>5.5.13.1</version>
</dependency>
好了,贴代码:
import Document;
巨人三传import DocumentException;
import pdf.*;
import javax.rvlet.http.HttpServletRespon;
import java.io.*;
import java.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* 根据pdf模板⽣成pdf⽂件⽣成到某路径下或导出
*
*/
public class PdfUtil {
/**
* 利⽤模板⽣成pdf保存到某路径下
*/
public static void pdfOut(Map<String, Object> inputMap) {
// ⽣成的新⽂件路径
String path0 = "D:/ProhibitDelete";
File f = new File(path0);
if (!f.exists()) {
f.mkdirs();
}
// 模板路径
String templatePath = "D:/ProhibitDelete/template11.pdf";
// 创建⽂件夹历史正剧
String newPdfPath = "D:/ProhibitDelete/testMould.pdf";
File file = new File(templatePath);
File file1 = new File(newPdfPath);
if (!ists()) {
try {
} catch (IOException e) {
e.printStackTrace();
}
}
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
BaFont bf = ateFont("C:/Windows/f", BaFont.IDENTITY_H, BaFont.EMBEDDED); // 输出流
out = new FileOutputStream(newPdfPath);
// 读取pdf模板
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = AcroFields();
//⽂字类的内容处理
Map<String, String> datemap = (Map<String, String>) ("dateMap");
form.addSubstitutionFont(bf);
for (String key : datemap.keySet()) {
String value = (key);
form.tField(key, value);
}
stamper.tFormFlattening(fal);
stamper.clo();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();男性照片
PdfImportedPage importPage = ImportedPage(new ByteArray()), 1);
copy.addPage(importPage);
doc.clo();
} catch (IOException | DocumentException e) {
System.out.println(e);
}
}
/**
* 利⽤模板⽣成pdf导出
*/
public static void pdfExport(Map<String, Object> inputMap, HttpServletRespon respon) {
// ⽣成的新⽂件路径
String path0 = "D:/ProhibitDelete";
File f = new File(path0);
if (!f.exists()) {
f.mkdirs();
}
// 模板路径
高龄String templatePath = "D:/ProhibitDelete/template12.pdf";
File file = new File(templatePath);
PdfReader reader;
ByteArrayOutputStream bos;
PdfStamper stamper;
兔子的谜语OutputStream out = null;
try {
Map<String, String> datemap = (Map<String, String>) ("dateMap");
BaFont bf = ateFont("C:/Windows/f", BaFont.IDENTITY_H, BaFont.EMBEDDED); // 输出流
respon.tContentType("application/pdf");
respon.tHeader("Content-Disposition",
"attachment;fileName=" + ("billId") + ".pdf", "UTF-8"));
out = new OutputStream());
// 读取pdf模板
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = AcroFields();
//⽂字类的内容处理
form.addSubstitutionFont(bf);
for (String key : datemap.keySet()) {
蛋糕英语怎么读
String value = (key);
form.tField(key, value);
}
stamper.tFormFlattening(fal);
stamper.clo();
带来英语Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = ImportedPage(new ByteArray()), 1);
copy.addPage(importPage);
doc.clo();
} catch (IOException | DocumentException e) {
System.out.println(e);
} finally {
try {
asrt out != null;
out.clo();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} ```