使用 poi-tl 根據(jù)模板生成 word 文件。
使用 xdocreport 將 docx 文件轉(zhuǎn)換為 pdf 文件。
xdocreport 也支持根據(jù)模板導(dǎo)出 word ,但是 poi-tl 的功能更齊全,操作更簡單,文檔清晰。
poi-tl 、xdocreport 內(nèi)部均依賴了 poi ,要注意兩者中 poi 和 自身項(xiàng)目引用的 poi 的版本是否存在沖突。
Pom 依賴
使用 poi 5.2.2 ,poi-tl 1.12.1 ,xdocreport 2.0.3
<!-- poi依賴-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<!-- poi-tl依賴-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.1</version>
</dependency>
<!-- xdocreport依賴-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-full</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
生成 DOCX 文件
創(chuàng)建 DOCX 模板
根據(jù)官方文檔按要求創(chuàng)建模板,并放在resources文件夾下。官方文檔 http://deepoove.com/poi-tl/。
生成 DOCX 文檔
創(chuàng)建模板填充類
其實(shí)也可以在生成文件時(shí)使用 Map 類型的方式填充文件內(nèi)容,如下
XWPFTemplate.compile(inputStream).render(
new HashMap<String, Object>(){{
put("title", "Hi, poi-tl Word模板引擎");
}});
但是使用實(shí)體類更規(guī)范一些。
@Data
public class ChildRoundsProvalDocxEntity {
private String childName;//孩子姓名
private String identify;//身份證號(hào)
private String gender;//性別
private String childRounds;//孩次
private PictureRenderData avatar;//頭像地址
private String entryDate;//入院時(shí)間
private String gardenMonths;//孩次
private String charge;//每月收費(fèi)
private String fatherName;//父親姓名
private String fatherIdentify;//父親身份證號(hào)
private String motherName;//母親姓名
private String motherIdentify;//母親身份證號(hào)
private String address;//住址及聯(lián)系方式
private String firstChildName;//第一個(gè)子女姓名
private String firstChildIdentify;//第一個(gè)子女身份證號(hào)
private String secondChildName;//第二個(gè)子女姓名
private String secondChildIdentify;//第二個(gè)子女身份證號(hào)
private String signDate;//簽署日期
}
生成文件(多種方式)
1、生成一個(gè)文件輸入流,方便用于再次編輯
不要在方法內(nèi)部將輸入流關(guān)閉
/**
* 生成文件到輸入流中
* @param templatePath
* @param data
* @return
*/
public InputStream createWordFile1(Object data){
Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");
XWPFTemplate template = null;
InputStream resultStream = null;
try {
// word模板填充
InputStream inputStream = templateFile.getInputStream();
template = XWPFTemplate.compile(inputStream).render(data);
resultStream = PoitlIOUtils.templateToInputStream(template);
PoitlIOUtils.closeQuietlyMulti(template);
} catch (Exception e) {
log.error("導(dǎo)出失敗,異常原因:" + e.getMessage());
} finally {
try {
if (template != null) {
template.close();
}
} catch (Exception e) {
log.error("流關(guān)閉失敗,異常原因:" + e.getMessage());
}
}
return resultStream;
}
2、生成docx到輸出流中,一般是在網(wǎng)絡(luò)響應(yīng)中直接輸出,瀏覽器去下載
public void createWordFile2(Object data, HttpServletResponse httpServletResponse){
//獲取模板信息
Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");
XWPFTemplate template = null;
docName = URLEncoder.encode(docName, StandardCharsets.UTF_8);
try {
httpServletResponse.setContentType("application/octet-stream");
httpServletResponse.addHeader("Content-Disposition", "attachment;filename=" + docName + ".docx");
httpServletResponse.addHeader("filename", docName);
// word模板內(nèi)容填充
InputStream inputStream = templateFile.getInputStream();
template = XWPFTemplate.compile(inputStream).render(data);
OutputStream out = httpServletResponse.getOutputStream();//要記得關(guān)閉
BufferedOutputStream bos = new BufferedOutputStream(out);//要記得關(guān)閉
template.write(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(template, bos, out);
} catch (Exception e) {
log.error("導(dǎo)出失敗,異常原因:" + e.getMessage());
throw new BaseException("Word文檔生成失敗");
} finally {
try {
if (template != null) {
template.close();
}
} catch (Exception e) {
log.error("流關(guān)閉失敗,異常原因:" + e.getMessage());
}
}
}
3、直接生成文件到指定路徑
public void createWordFile3(Object data, String path){
//獲取模板信息
Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");
XWPFTemplate template = null;
try {
// word模板內(nèi)容填充
InputStream inputStream = templateFile.getInputStream();
template = XWPFTemplate.compile(inputStream).render(data);
template.writeToFile(path);//文件夾路徑必須存在 可以提前創(chuàng)建
PoitlIOUtils.closeQuietlyMulti(template);
} catch (Exception e) {
log.error("導(dǎo)出失敗,異常原因:" + e.getMessage());
throw new BaseException("Word文檔生成失敗");
} finally {
try {
if (template != null) {
template.close();
}
} catch (Exception e) {
log.error("流關(guān)閉失敗,異常原因:" + e.getMessage());
}
}
}
生成效果
DOCX 轉(zhuǎn) PDF
初始化 XWPFDocument 需要一個(gè)輸入流,以下是直接使用文件輸入流去初始化。
FileInputStream inputStream1 = new FileInputStream("docx文件位置.docx");
XWPFDocument xwpfDocument = new XWPFDocument(inputStream1);
PdfOptions options = PdfOptions.create();
try (OutputStream outPDF = Files.newOutputStream(Paths.get("要生成的pdf位置.pdf"))) {
PdfConverter.getInstance().convert(xwpfDocument.getXWPFDocument(), outPDF, options);
} catch (IOException e) {
log.error("PDF轉(zhuǎn)換失敗",e);
}
PDF 生成效果
文章來源:http://www.zghlxwxcb.cn/news/detail-615317.html
注意
DOCX 模板中如果表格元素中放了圖片(如上圖中的頭像),要確保生成的文件中的圖片大小不超過模板中單元格大小。
即圖片所在單元格不能被圖片撐大,否則圖片在轉(zhuǎn)換成PDF時(shí)無法展示。
。。。。。。。。。。。。。。。。。。。。。。
此方法轉(zhuǎn)換出來的PDF格式可能有點(diǎn)問題,所以換了個(gè)新的文檔轉(zhuǎn)換工具類,點(diǎn)擊這里查看文章來源地址http://www.zghlxwxcb.cn/news/detail-615317.html
到了這里,關(guān)于Springboot -- 按照模板生成docx、pdf文件,docx轉(zhuǎn)pdf格式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!