admin管理员组文章数量:1605340
需求:将文件转为pdf
方案1:使用poi工具
参考:使用poi实现Word转pdf - 极墨笔记
方案2:使用apose工具
参考:Aspose word 转换为pdf 排版错位和数据丢失_blues199的博客-CSDN博客
注意:apose工具使用,在Linux系统部署时,需要添加文件涉及的字体文件,因为linux不支持window的字体文件,会出现pdf乱码问题如图。
如何解决
步骤1:断点找到文件用到了哪些字体(也可以自己设置好文档字体)
步骤2:添加字体文件依赖
情况1:项目是war包,可将字体文件直接添加到项目目录下,打包部署即可
情况2:项目是jar包,则需要将字体添加到linux的路径下
项目配置文件
字体文件目录
至此转换文件成功展示
工具类测试
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.SaveFormat;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Component;
import java.io.*;
@Slf4j
@Component
public class PdfUtil {
/**
* 要展示的文件流
*
* @param wordInputStream
* @param pdfPath
* @return
*/
public boolean doc2pdf(InputStream wordInputStream, String pdfPath) {
long start = System.currentTimeMillis();
FileOutputStream os = null;
try {
File file = new File(pdfPath); // 新建一个空白pdf文档
// linux字体设置
OsInfo osInfo = SystemUtil.getOsInfo();
if(osInfo.isLinux()){
FontSettings.getDefaultInstance().setFontsFolder(this.getClass().getClassLoader().getResource("").getPath()+"font", true);
}
os = new FileOutputStream(file);
Document doc = new Document(wordInputStream); // Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
long end = System.currentTimeMillis();
log.info("pdf转换成功,共耗时:" + ((end - start) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
log.error("Word转pdf失败,原因:", e);
return false;
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
log.error("文件流关闭失败,原因:", e);
}
}
}
return true;
}
/**
* 删除PDF
*
* @param filePath 文件路径
*/
public static void deletePDF(String filePath) {
File file = new File(filePath);
if (file.exists()) {
log.info("PDF临时文件删除:" + file.delete());
}
}
public static void main(String[] args) throws Exception {
/**
* aspose转pdf
*/
long start = System.currentTimeMillis();
// 原Word文件
FileInputStream fileInputStream = new FileInputStream("F:\\承诺书已签字.docx");
// 要生成的pdf文件
FileOutputStream os = new FileOutputStream("F:\\承诺书已签字-apos.pdf");
Document doc = new Document(fileInputStream);
// 执行转换
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
System.out.println("apose转pdf耗时:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
/**
* poi转pdf
*/
// 原Word文件
fileInputStream = new FileInputStream("F:\\承诺书已签字.docx");
XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
// 要生成的pdf文件
FileOutputStream fileOutputStream = new FileOutputStream("F:\\承诺书已签字-pdf.pdf");
PdfOptions pdfOptions = PdfOptions.create();
// 执行转换
PdfConverter.getInstance().convert(xwpfDocument, fileOutputStream, pdfOptions);
System.out.println("poi转pdf耗时:" + (System.currentTimeMillis() - start));
}
}
从耗时时间上看,转换一个28K的docx文件耗时,apose耗时更短。
版权声明:本文标题:java工具word转pdf 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1728481242a1160123.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论