項(xiàng)目中有需求需要導(dǎo)出PDF育勺,
市面上現(xiàn)在有很多word導(dǎo)出PDF,比如常用的兩款 docx4j 和 aspose-words
docx4j 是開源的 aspose-words是商用的 需要購買License 但是網(wǎng)上有破解的可以搜索到
docx4j
maven依賴
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.3.8</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.3.8</version>
</dependency>
特別說明: 需要的是 *.ttf 格式的字體庫 如果缺失會(huì)出現(xiàn)亂碼 小方格娜谊。
1,word 導(dǎo)出pdf
public void outPDF2(ByteArrayOutputStream outputStream, HttpServletResponse response, String fileName) throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputStream);
ExcelUtils.exportPdf(response, fileName);
setFontMapper(wordMLPackage);
Docx4J.toPDF(wordMLPackage, response.getOutputStream());
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(response.getOutputStream());
}
2, 引入需要的字體
private static void setFontMapper(WordprocessingMLPackage mlPackage) throws Exception {
Mapper fontMapper = new IdentityPlusMapper();
//引入系統(tǒng)的
fontMapper.put("隸書", PhysicalFonts.get("LiSu"));
fontMapper.put("宋體", PhysicalFonts.get("SimSun"));
fontMapper.put("等線", PhysicalFonts.get("SimSun"));
fontMapper.put("等線 Light", PhysicalFonts.get("SimSun"));
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
fontMapper.put("新細(xì)明體", PhysicalFonts.get("SimSun"));
//解決宋體(正文)和宋體(標(biāo)題)的亂碼問題
PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));
// 如果系統(tǒng) 自己下載的字體 引入
PhysicalFonts.addPhysicalFonts("Microsoft Yahei", new ClassPathResource("font/Microsoft YaHei.ttf").getURI());
fontMapper.put("Microsoft Yahei", PhysicalFonts.get("Microsoft YaHei.ttf"));
mlPackage.setFontMapper(fontMapper);
}
3渔肩,問題
如果word當(dāng)中有表格 表格中的單元格 如果是英文和數(shù)字不會(huì)換行 中文會(huì)自動(dòng)換行這是一個(gè)BUG因俐。
aspose-words
maven依賴
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
</dependency>
特別說明: 需要的是 *.ttc 格式的字體庫 如果缺失會(huì)出現(xiàn)亂碼 小方格。
1周偎,word 導(dǎo)出pdf
public void outPDF(ByteArrayOutputStream outputStream, HttpServletResponse response, String fileName) throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
ExcelUtils.exportPdf(response, fileName);
//引入字體所在的目錄
FontSettings.setFontsFolder("/tmp/fonts", true);
//使用字體設(shè)置配置加載選項(xiàng)
LoadOptions loadOptions = new LoadOptions();
Document doc = new Document(inputStream, loadOptions);
PdfSaveOptions options = new PdfSaveOptions();
options.setCompliance(PdfCompliance.PDF_15);
// 將Word轉(zhuǎn)換為PDF
doc.save(response.getOutputStream(), options);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(response.getOutputStream());
}
2抹剩,引入需要的字體
static {
// //執(zhí)行字體
Process process = null;
try {
//正常寫 linux命令即可 、比如:mkdir xx/touch b.txt/sh start.sh/sh stop.sh
String commend = "mkdir -p /tmp/fonts";
process = Runtime.getRuntime().exec(commend);
log.info("創(chuàng)建目錄" + process.waitFor());
log.info("拷貝字體");
FileCopyUtils.copy(new ClassPathResource("font/Microsoft YaHei.ttc").getInputStream(), new FileOutputStream("/tmp/fonts/Microsoft YaHei.ttc"));
commend = "chmod -R 777 /tmp/fonts";
process = Runtime.getRuntime().exec(commend);
log.info("授予權(quán)限" + process.waitFor());
// setFontsSources();
} catch (Exception e) {
log.error("設(shè)置字體失敗", e);
}
}
3蓉坎,問題
aspose-words 是商用的 需要自己在網(wǎng)上找下 license.xml 澳眷。