記錄libreoffice實現office轉pdf(適用於windows、linux)


由於目前的工作跟office打交道比較多,所以才有了此篇blog,需求是實現word轉換pdf方便頁面展示。之前lz采用的是jacob(僅支持windows)進行轉換的,但是現在服務器改成linux顯然不能用了,於是網上搜羅一圈,最終決定采用LibreOffice。(前提:需要安裝jdk環境)

LibreOffice中文官網:https://zh-cn.libreoffice.org/   下載合適的版本,本文下載的是6.1.6 

已上傳百度網盤(鏈接: https://pan.baidu.com/s/1hS-GUT5yXaDgFDMWq3mjXQ 提取碼: 1e9z)

一:windows下實現office轉pdf

安裝:直接一鍵默認安裝

環境變量:在path前加入libreoffice安裝路徑(如:D:\Program Files\LibreOffice\program)

進入dos窗口輸入soffice 如果彈出libreoffice界面則表示安裝成功

java程序實現轉換操作(原理通過cmd調用libreoffice指令)

/**
     * 利用libreOffice將office文檔轉換成pdf
     * @param inputFile  目標文件地址
     * @param pdfFile    輸出文件夾
     * @return
     */
    public static boolean convertOffice2PDF(String inputFile, String pdfFile){
        long start = System.currentTimeMillis();
        String command;
        boolean flag;
        String osName = System.getProperty("os.name");
        if (osName.contains("Windows")) {
            command = "cmd /c soffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;
        }else {
            command = "libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;
        }
        flag = executeLibreOfficeCommand(command);
        long end = System.currentTimeMillis();
        logger.debug("用時:{} ms", end - start);
        return flag;
    }


    /**
     * 執行command指令
     * @param command
     * @return
     */
    public static boolean executeLibreOfficeCommand(String command) {
        logger.info("開始進行轉化.......");
        Process process;// Process可以控制該子進程的執行或獲取該子進程的信息
        try {
            logger.debug("convertOffice2PDF cmd : {}", command);
            process = Runtime.getRuntime().exec(command);// exec()方法指示Java虛擬機創建一個子進程執行指定的可執行程序,並返回與該子進程對應的Process對象實例。
            // 下面兩個可以獲取輸入輸出流
//            InputStream errorStream = process.getErrorStream();
//            InputStream inputStream = process.getInputStream();
        } catch (IOException e) {
            logger.error(" convertOffice2PDF {} error", command, e);
            return false;
        }
        int exitStatus = 0;
        try {
            exitStatus = process.waitFor();// 等待子進程完成再往下執行,返回值是子線程執行完畢的返回值,返回0表示正常結束
            // 第二種接受返回值的方法
            int i = process.exitValue(); // 接收執行完畢的返回值
            logger.debug("i----" + i);
        } catch (InterruptedException e) {
            logger.error("InterruptedException  convertOffice2PDF {}", command, e);
            return false;
        }
        if (exitStatus != 0) {
            logger.error("convertOffice2PDF cmd exitStatus {}", exitStatus);
        } else {
            logger.debug("convertOffice2PDF cmd exitStatus {}", exitStatus);
        }
        process.destroy(); // 銷毀子進程
        logger.info("轉化結束.......");
        return true;
    }  

二:Linux下實現office轉pdf

安裝:把下載下來的三個安裝包上傳到linux,采用  tar -xvf xxxxxx.tar.gz解壓即可

然后進入RPMS包下,采用yum localinstall *.rpm安裝rpm文件

測試是否安裝成功:libreoffice6.1 -help

為了使用libreoffice創建別名

[root@VM]# alias libreoffice='libreoffice6.0'
[root@VM]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias libreoffice='libreoffice6.0'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'

linux下面命令行測試word轉pdf(其參數與windows下的參數大體相同)

命令:libreoffice --convert-to pdf:writer_pdf_Export /usr/lib/files/白頭擬稿紙.doc --outdir /usr/lib/files/

關於word轉pdf中文亂碼問題處理

1:查看fonts目錄:cat /etc/fonts/fonts.conf | grep fon
得知字體存放位置:/usr/share/fonts

2: 把Windows下的字體C:\Windows\Fonts下的宋體,即simsun.ttc上傳到linux服務器
在fonts下新建Fonts文件 把字體上傳到該路徑下即可

 


免責聲明!

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



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