word转pdf --(documents4j 方式)

 

documents4j,在本地使用很方便,但是部署到LINUX上面之后,抛出异常,

官方文档:documents4j是使用本地的MS Office应用做的文件格式转换,Linux没有对应的MS Office应用

 

依赖:

<!-- pdf -->
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>${documents4j.version}</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-word</artifactId>
            <version>${documents4j.version}</version>
        </dependency>
        <dependency>
            <groupId>e-iceblued</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>${spire.version}</version>
        </dependency>


版本:
<documents4j.version>1.1.5</documents4j.version>
<spire.version>3.9.0</spire.version>

 

 

代码:

package com.infinitepossibilities.pdf;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.ImageType;
import org.apache.commons.io.FileUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class PdfUtil {
    /**
     * word转pdf
     * @param wordFilePath word文件路径
     * @param pdfFilePath  pdf文件路径
     * @return 成功或失败
     */
    public static boolean docxToPdf(String wordFilePath, String pdfFilePath) {
        boolean result = false;

        File inputFile = new File(wordFilePath);
        File outputFile = new File(pdfFilePath);
        try {
            InputStream inputStream = new FileInputStream(inputFile);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();

            outputStream.close();
//            converter.shutDown();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) {

        docxToPdf("C:\\Users\\admin\\Desktop\\1.7.doc",
                "C:\\Users\\admin\\Desktop\\1.7.pdf");
    }

}

 

posted @ 2021-11-30 14:39  Li&Fan  阅读(2642)  评论(1编辑  收藏  举报