如何将word文件转为pdf

您所在的位置:网站首页 md文件生成pdf 如何将word文件转为pdf

如何将word文件转为pdf

2023-03-23 13:37| 来源: 网络整理| 查看: 265

项目中有生成word文档,并支持在线预览的需求,我首先使用freemarker模板生成word文件,然后使用word转pdf的方式实现文件预览,最终找到了以下两种方法:

1、POI

引入依赖

org.apache.poi poi-ooxml 4.1.2 fr.opensagres.xdocreport fr.opensagres.poi.xwpf.converter.pdf-gae 2.0.2

文件转换

public static void main(String[] args) throws IOException { String wordSourcePath = "C:\\Users\\Administrator\\Desktop\\test\\test.docx"; String pdfSourcePath = wordSourcePath.replace(".docx", ".pdf"); boolean wordToPdf = wordToPdf(wordSourcePath, pdfSourcePath); } public static boolean wordToPdf(String wordPath, String pdfPath) throws IOException { FileInputStream fileInputStream = new FileInputStream(wordPath); XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream); PdfOptions pdfOptions = PdfOptions.create(); FileOutputStream fileOutputStream = new FileOutputStream(pdfPath); PdfConverter.getInstance().convert(xwpfDocument, fileOutputStream, pdfOptions); fileInputStream.close(); fileOutputStream.close(); return true; } 2、Aspose

引入依赖 word:aspose-words-15.8.0-jdk16 excel:aspose-cells-8.5.2

com.aspose aspose-words 15.8.0

需要在项目里加入一个license.xml,不然生成的pdf会有水印 license.xml示例如下:

Shanghai Hudun Information Technology Co., Ltd [email protected] Developer OEM Limited to 1 developer. 140714065432 266166 This is a redistributable license Aspose.Total for .NET Enterprise ea712420-fbb2-41e7-9ede-39f7b3a43e4a 20150714 3.0 http://www.aspose.com/corporate/purchase/license-instructions.aspx Jv8ksvWRYLPzqyQX6J5pAUk4VNc1sogN5xq3ep7Piiy0iGQXg3p8yiGILjPpyCn7G2fkpcnCWPPffoWKqELUc40CiQWsuSmvAuLpvdJCVArt41CpaHNPAUKhrpOzrruzMPWgOB/ByzBVyvUkXl5g2OYHS29gVFlCJ2Ps+p0LnIQ=

文件转换工具类

public class WordToPdfUtil { public static boolean wordToPdf(String wordPath, String pdfPath) { // 验证License 若不验证则转化出的pdf文档会有水印产生 if (!getLicense()) { return false; } FileOutputStream os = null; try { File file = new File(pdfPath); os = new FileOutputStream(file); Document doc = new Document(wordPath); doc.save(os, SaveFormat.PDF); } catch (Exception e) { e.printStackTrace(); return false; }finally { if (os != null) { try { os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } private static boolean getLicense() { boolean result = false; try { // license.xml放在resources根目录下 InputStream is = Test.class.getClassLoader().getResourceAsStream("\license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } }

中文乱码 假如你使用的是Linux服务器,这个转化过程不加以配置的话,在Linux中转化后的PDF,如果带有中文是会有乱码现象的。因为Linux里面没有中文字符的样式,所以我们要做的就是将window中的中文样式导入到Linux服务器里面。 1、找到windows系统的中文样式,并打成一个包。这个位于C:\Windows\Fonts,这个文件一般来说是只读的,但我们可以直接复制一个,然后再打包,打包成一个zip包 2、传到Linux服务器 3、解压到指定文件夹 4、安装字体

经过对比,POI可以实现常规的文本转换,数据表格没有转换成功,于是选择Aspose,完美转换。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3