Springboot如何根據docx填充生成word文件并導出pdf
在項目中碰見一個需求,需要將.doc的合同,轉換為pdf實現打印與預覽功能。
將docx模板填充數據生成doc文件
1、依賴引入
填充docx模板,只需要引入一個pom依賴即可實現。
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.5.0</version>
</dependency>2、doc文件轉換docx,并標注別名
用office或者wps,創(chuàng)建一個001.doc文件,繪制表格,保存。
更改后綴為.docx,確定后,在指定的位置,表示數據接受變量名稱。
如下圖所示:

3、編寫java代碼實現數據填充
import com.deepoove.poi.XWPFTemplate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
/**
* word 填充測試
*/
public class TestWord {
public static void main(String[] args) throws IOException {
Map<String, Object> params = new HashMap<>();
params.put("username","xiangjiao1");
params.put("password","******");
params.put("age",22);
params.put("email","專注寫bug測試中文");
Resource resource = new ClassPathResource("templates_report/001.docx");
File file = resource.getFile();
// 數據填充
XWPFTemplate template = XWPFTemplate.compile(file).render(params);
String docOutPath = System.getProperty("user.dir")+File.separator+"springboot-poi"+File.separator+"pdf"+File.separator+ "1.doc";
OutputStream outputStream = new FileOutputStream(docOutPath);
template.write(outputStream);
}
}運行程序,查看結果。
測試項目結構如下:

docx文件填充數據導出pdf(web)
1、依賴引入
向docx模板中填充數據,并導出pdf類型的文件,除了上面的pom依賴之外,還需要引入其他的依賴信息,完整依賴如下所示:
<!-- docx 數據填充生成 doc文件 這個是主要 -->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.5.0</version>
</dependency>
<!-- doc 轉 pdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- docx4j docx2pdf -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>6.1.2</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>6.0.0</version>
</dependency>2、字體文件
在src\main\resources下創(chuàng)建一個font文件夾,其中放入simsun.ttc字體文件。
3、編寫工具類
思想很簡單
- 1、先使用上面的docx模板填充數據生成
臨時doc文件, - 2、再將doc文件轉換為pdf文件
- 3、刪除臨時文件
【注意:】
為了避免出現多人同時操作,導致文件誤刪的問題,需要盡可能地保證臨時文件名稱的唯一性。
import com.deepoove.poi.XWPFTemplate;
import com.itextpdf.text.*;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.docx4j.Docx4J;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.Map;
import java.util.UUID;
import java.util.zip.ZipOutputStream;
/**
* pdf 導出工具類
*/
@Component
@Slf4j
public final class FreeMarkUtils {
/**
* 根據docx模板填充數據 并生成pdf文件
*
* @param dataMap 數據源
* @param docxFile docx模板的文件名
* @return 生成的文件路徑
*/
public static byte[] createDocx2Pdf(Map<String, Object> dataMap, String docxFile) {
//輸出word文件路徑和名稱 (臨時文件名,本次為測試,最好使用雪花算法生成,或者用uuid)
String fileName = UUID.randomUUID().toString() + ".docx";
// word 數據填充
// 生成docx臨時文件
final File tempPath = new File(fileName);
final File docxTempFile = getTempFile(docxFile);
XWPFTemplate template = XWPFTemplate.compile(docxTempFile).render(dataMap);
try {
template.write(new FileOutputStream(tempPath));
} catch (IOException e) {
e.printStackTrace();
}
// word轉pdf
final String pdfFile = convertDocx2Pdf(fileName);
return getFileOutputStream(new File(pdfFile)).toByteArray();
}
/**
* word(doc)轉pdf
*
* @param wordPath doc 生成的臨時文件路徑
* @return 生成的帶水印的pdf路徑
*/
public static String convertDocx2Pdf(String wordPath) {
OutputStream os = null;
InputStream is = null;
//輸出pdf文件路徑和名稱 (臨時文件 盡可能保證文件名稱的唯一性)
final String fileName = UUID.randomUUID().toString() + ".pdf";
try {
is = new FileInputStream(wordPath);
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(is);
Mapper fontMapper = new IdentityPlusMapper();
fontMapper.put("隸書", PhysicalFonts.get("LiSu"));
fontMapper.put("宋體", PhysicalFonts.get("SimSun"));
fontMapper.put("微軟雅黑", PhysicalFonts.get("Microsoft Yahei"));
fontMapper.put("黑體", PhysicalFonts.get("SimHei"));
fontMapper.put("楷體", PhysicalFonts.get("KaiTi"));
fontMapper.put("新宋體", PhysicalFonts.get("NSimSun"));
fontMapper.put("華文行楷", PhysicalFonts.get("STXingkai"));
fontMapper.put("華文仿宋", PhysicalFonts.get("STFangsong"));
fontMapper.put("宋體擴展", PhysicalFonts.get("simsun-extB"));
fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
fontMapper.put("幼圓", PhysicalFonts.get("YouYuan"));
fontMapper.put("華文宋體", PhysicalFonts.get("STSong"));
fontMapper.put("華文中宋", PhysicalFonts.get("STZhongsong"));
//解決宋體(正文)和宋體(標題)的亂碼問題
PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));
PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));
// 字體文件
PhysicalFonts.addPhysicalFonts("SimSun", WordUtils.class.getResource("/font/simsun.ttc"));
mlPackage.setFontMapper(fontMapper);
os = new FileOutputStream(fileName);
//docx4j docx轉pdf
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(mlPackage);
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
is.close();//關閉輸入流
os.close();//關閉輸出流
} catch (Exception e) {
e.printStackTrace();
} finally {
// 刪除docx 臨時文件
File file = new File(wordPath);
if (file != null && file.isFile() && file.exists()) {
file.delete();
}
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return fileName;
}
/**
* 文件轉字節(jié)輸出流
*
* @param outFile 文件
* @return
*/
public static ByteArrayOutputStream getFileOutputStream(File outFile) {
// 獲取生成臨時文件的輸出流
InputStream input = null;
ByteArrayOutputStream bytestream = null;
try {
input = new FileInputStream(outFile);
bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = input.read()) != -1) {
bytestream.write(ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bytestream.close();
input.close();
log.info("刪除臨時文件");
if (outFile.exists()) {
outFile.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bytestream;
}
/**
* 獲取資源文件的臨時文件
* 資源文件打jar包后,不能直接獲取,需要通過流獲取生成臨時文件
*
* @param fileName 文件路徑 templates/xxx.docx
* @return
*/
public static File getTempFile(String fileName) {
final File tempFile = new File(fileName);
InputStream fontTempStream = null;
try {
fontTempStream = FreeMarkUtils.class.getClassLoader().getResourceAsStream(fileName);
FileUtils.copyInputStreamToFile(fontTempStream, tempFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fontTempStream != null) {
fontTempStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return tempFile;
}
/**
* 插入圖片水印
* @param srcByte 已生成PDF的字節(jié)數組(流轉字節(jié))
* @param destFile 生成有水印的臨時文件 temp.pdf
* @return
*/
public static FileOutputStream addWaterMark(byte[] srcByte, String destFile) {
// 待加水印的文件
PdfReader reader = null;
// 加完水印的文件
PdfStamper stamper = null;
FileOutputStream fileOutputStream = null;
try {
reader = new PdfReader(srcByte);
fileOutputStream = new FileOutputStream(destFile);
stamper = new PdfStamper(reader, fileOutputStream);
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
// 設置字體
//BaseFont font = BaseFont.createFont();
// 循環(huán)對每頁插入水印
for (int i = 1; i < total; i++) {
final PdfGState gs = new PdfGState();
// 水印的起始
content = stamper.getUnderContent(i);
// 開始
content.beginText();
// 設置顏色 默認為藍色
//content.setColorFill(BaseColor.BLUE);
// content.setColorFill(Color.GRAY);
// 設置字體及字號
//content.setFontAndSize(font, 38);
// 設置起始位置
// content.setTextMatrix(400, 880);
//content.setTextMatrix(textWidth, textHeight);
// 開始寫入水印
//content.showTextAligned(Element.ALIGN_LEFT, text, textWidth, textHeight, 45);
// 設置水印透明度
// 設置筆觸字體不透明度為0.4f
gs.setStrokeOpacity(0f);
Image image = null;
image = Image.getInstance("url");
// 設置坐標 絕對位置 X Y 這個位置大約在 A4紙 右上角展示LOGO
image.setAbsolutePosition(472, 785);
// 設置旋轉弧度
image.setRotation(0);// 旋轉 弧度
// 設置旋轉角度
image.setRotationDegrees(0);// 旋轉 角度
// 設置等比縮放 圖片大小
image.scalePercent(4);// 依照比例縮放
// image.scaleAbsolute(200,100);//自定義大小
// 設置透明度
content.setGState(gs);
// 添加水印圖片
content.addImage(image);
// 設置透明度
content.setGState(gs);
//結束設置
content.endText();
content.stroke();
}
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
try {
stamper.close();
fileOutputStream.close();
reader.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return fileOutputStream;
}
}4、編寫測試接口
import cn.xj.util.FreeMarkUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RestController
@RequestMapping("/report")
public class ReportController {
@GetMapping("/doc2pdf")
public void doc2pdf(HttpServletResponse response) {
Map<String, Object> params = new HashMap<>();
params.put("username","xiangjiao1");
params.put("password","******");
params.put("age",22);
params.put("email","專注寫bug測試中文");
final byte[] data = FreeMarkUtils.createDocx2Pdf(params, "templates_report/001.docx");
String fileName = UUID.randomUUID().toString() + "_001_test.pdf";
generateFile(response, data, fileName);
}
/**
* 下載文件
* @param response 相應
* @param data 數據
* @param fileName 文件名
*/
private void generateFile(HttpServletResponse response, byte[] data, String fileName) {
response.setHeader("content-Type", "application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
try {
response.getOutputStream().write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}請求測試
http://localhost/report/doc2pdf

docx4j 復雜docx文件轉pdf碰見的坑總結
轉pdf出現空格壓縮、中文縮減等問題,可以考慮將半角替換成全角,將模板中的空格使用全角空格替換。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
FeignClientFactoryBean創(chuàng)建動態(tài)代理詳細解讀
這篇文章主要介紹了FeignClientFactoryBean創(chuàng)建動態(tài)代理詳細解讀,當直接進去注冊的方法中,一步步放下走,都是直接放bean的定義信息中放入值,然后轉成BeanDefinitionHolder,最后在注冊到IOC容器中,需要的朋友可以參考下2023-11-11
Spring Boot + thymeleaf 實現文件上傳下載功能
最近同事問我有沒有有關于技術的電子書,我打開電腦上的小書庫,但是郵件發(fā)給他太大了,公司又禁止用文件夾共享,于是花半天時間寫了個小的文件上傳程序,部署在自己的Linux機器上,需要的朋友可以參考下2018-01-01

