word转pdf的几种方法
word轉pdf的幾種方法
需求是實現將word轉換成pdf在線預覽, 試了幾種方法發現樣式效果最好的是aspose-words,但是需要破解, 附帶了激活方式
-
openOffice 與 libreOffice
兩款都是運行在服務器上面的office處理軟件, 通過命令行的方式去執行轉換命令
- openOffice下載地址
- libreOffice下載地址
下載安裝好之后需要在代碼中引入jodconverter相關jar包依賴(jodconverter.zip解壓后lib下的都需要引入)
相關軟件及jar下載鏈接:https://pan.baidu.com/s/1u0U5jYYAjWRYO7mL5ilD8Q 提取碼:8888
// openOffice 和 libreOffice 差不多除了命令不一樣 // 1.執行系統命令啟動openoffice線程 // 2.連接openoffice服務連接 // 3.執行轉換// window 使用 調用openoffice服務線程 String command = "C:/Program Files (x86)/OpenOffice 4/program/soffice-headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\""; // Linux使用 // String command = "/opt/openoffice4/program/soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard\""; Process p = Runtime.getRuntime().exec(command);// 連接openoffice服務 OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100); connection.connect();// 轉換word到pdf DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(new File("/temp/1.doc"), new File("/temp/1.pdf"));connection.disconnect(); p.destroy();- 如果出現中文亂碼問題, 確定一下服務器上缺不缺中文字體
ps: 試了大部分的word都能轉換pdf, 有一些樣式復雜的會渲染不出來, 直接把word源碼生成pdf
-
spire.doc.free
免費版的有頁數限制, 我的場景不會有多頁的就直接用的免費版本
效果可以參照https://smallpdf.com/cn/word-to-pdf, https://www.freepdfconvert.com/zh-cn/word-to-pdf這些在線轉換網站,我試了生成的效果和這些網站的一樣.
<!-- spire.doc.free 相關依賴 --> <dependency><groupId>e-iceblue</groupId><artifactId>spire.doc.free</artifactId><version>3.9.0</version> </dependency><repositories><repository><id>com.e-iceblue</id><url>http://repo.e-iceblue.cn/repository/maven-public/</url></repository> </repositories>代碼相當簡單…
import com.spire.doc.*;public class WordtoPDF {public static void main(String[] args) {// 加載word示例文檔Document document = new Document();document.loadFromFile("D:\\2.doc");// 保存結果文件document.saveToFile("D:\\3.pdf");} }ps: 我生成的最后換行不一致, 所以沒選用這個
-
aspose-words(推薦)
效果可以參照迅捷pdf在線轉換器https://app.xunjiepdf.com/pdf2word/
需要引入aspose-words-15.8.0-jdk16.jar到項目中
鏈接:https://pan.baidu.com/s/1q06E0wK13dC2ejDbDtdpZQ
<!-- 下載后放項目lib下通過system引用 --> <dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>15.8.0</version><scope>system</scope><systemPath>${project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath> </dependency>
提取碼:8888核心代碼
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream;import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat;public class WordPdfUtil {private static boolean license = false;static {try {// license.xml放在src/main/resources文件夾下InputStream is = WordPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");License aposeLic = new License();aposeLic.setLicense(is);license = true;} catch (Exception e) {license = false;e.printStackTrace();}}public static void doc2pdf(String wordPath, String pdfPath) {// 驗證License 若不驗證則轉化出的pdf文檔會有水印產生if (!license) {System.out.println("License驗證不通過...");return;}try {File file = new File(pdfPath);FileOutputStream os = new FileOutputStream(file);Document doc = new Document(wordPath);// 支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉換doc.save(os, SaveFormat.PDF);os.close();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {doc2pdf("D:\\3.doc","D:\\5.pdf");} }在src/main/resources文件夾下創建license.xml填充下面內容激活
<?xml version="1.0" encoding="UTF-8" ?> <License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data> <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature> </License> -
file-online-preview
github上面找到的一個開源項目, 活躍度比較高也記錄一下. 目前試了效果沒有aspose-words好用, 底層是集成的LibreOffice.
項目地址: https://github.com/kekingcn/kkFileView
支持的類型比較多,后續如果有需求,方便二次開發
總結
以上是生活随笔為你收集整理的word转pdf的几种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python爬虫的数据提取,一篇博客就搞
- 下一篇: 练习2-3 输出倒三角图案