Java代碼實現WORD轉PDF


第一步:

安裝OpenOffice   在此良心提供windows版本安裝文件

鏈接:https://pan.baidu.com/s/17pPCkcS1C46VtLhevqSgPw  密碼:vmlu

安裝就一直點下一步即可。

安裝完成后,進入OpenOffice安裝目錄

安裝目錄一般為C:ProgramFiles(x86)/OpenOffice 4/program/

執行下面命令:

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"

第二步:

JAVA代碼實現

這里在SpringBoot基礎上寫了個接口,可以直接通過項目調用

package com.test.controller;
 
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.File;
import java.net.ConnectException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * Created by jlm on 2019-05-07 11:46
 */
@RestController
@RequestMapping("/test")
public class WordToPdfController {
 
    @RequestMapping(value = "toPdf.do", method = RequestMethod.POST)
    public boolean pauseToDeal(String sourceFile, String destFile) {
        try {
            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                // 找不到源文件, 則返回false
                return false;
            }
            // 如果目標路徑不存在, 則新建該路徑
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            //如果目標文件存在,則刪除
            if (outputFile.exists()) {
                outputFile.delete();
            }
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            connection.connect();
            //用於測試openOffice連接時間
            System.out.println("連接時間:" + df.format(new Date()));
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
                    connection);
            converter.convert(inputFile, outputFile);
            //測試word轉PDF的轉換時間
            System.out.println("轉換時間:" + df.format(new Date()));
            connection.disconnect();
            return true;
        } catch (ConnectException e) {
            e.printStackTrace();
            System.err.println("openOffice連接失敗!請檢查IP,端口");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
}

Maven POM文件配置

<dependency>
      <groupId>com.artofsolving</groupId>
      <artifactId>jodconverter</artifactId>
      <version>2.2.2</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>jurt</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>ridl</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>juh</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>unoil</artifactId>
      <version>3.0.1</version>
    </dependency>

此方法適用於windos服務器下的項目,如需linux,請安裝liunx版本OpenOffice

原文:https://blog.csdn.net/Rice_kil/article/details/89921849#10006-weixin-1-52626-6b3bffd01fdde4900130bc5a2751b6d1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM