• java实现word转pdf


    前两天写了一篇博客,讲的是word、ppt、pptx等文件转pdf,我研究文件转化也是因为工作需要,后来发现写的功能在我本地(window 10)没有问题,但是发到现场(Linux)word转pdf转化出来时0k,没办法只能再换一种方式,今天就给大家介绍Apache OpenOffice。OpenOffice是开源的办公软件套件,包可以在多个操作系统上运行,包括Windows、Mac OS X和Linux等。
    一、下载OpenOffice
    OpenOffice下载连接

    二、解压依赖包

    tar -zxvf 压缩包名称
    
    • 1

    三、安装依赖

    # 移动到指定目录下
    cd zh-CN/RPMS
    # 安装依赖
    yum localinstall *.rpm
    
    • 1
    • 2
    • 3
    • 4

    四、启动OpenOffice程序

    # 这里127.0.0.1只能是本机访问,占用端口设置为8100
    nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
    
    • 1
    • 2

    五、测试

    用openoffice需要集成相关依赖
    测试的时候我们需要用到jodconverter-2.2.2.jar
    需要注意的是如果服务器的字体不全,转出来的文件可能乱码或者空白或者都是框

    # 解压压缩包
    unzip jodconverter-2.2.2.zip
    # 进入解压后的文件夹
    cd /jodconverter-2.2.2/lib
    # 运行测试
    java -jar jodconverter-cli-2.2.2.jar /root/1.doc /usr/1.pdf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    六、项目集成

      <dependency>
                <groupId>com.documents4j</groupId>
                <artifactId>documents4j-local</artifactId>
                <version>1.0.3</version>
            </dependency>
            <dependency>
                <groupId>com.documents4j</groupId>
                <artifactId>documents4j-transformer-msoffice-word</artifactId>
                <version>1.0.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>pdfbox</artifactId>
                <version>2.0.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>fontbox</artifactId>
                <version>2.0.17</version>
            </dependency>
    		<dependency>
    		    <groupId>com.artofsolving</groupId>
    		    <artifactId>jodconverter</artifactId>
    		    <version>2.2.2</version>
    		</dependency>
    		<dependency>
    		    <groupId>org.openoffice</groupId>
    		    <artifactId>juh</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>unoil</artifactId>
    		    <version>3.0.1</version>
    		</dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    import java.io.*;
    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.OpenOfficeDocumentConverter;
    
    public static void docToPdf(String sourcePath, String targetPath){
            File sourceFile = new File(sourcePath);
                File pdfFile = new File(targetPath);
                OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
                try {
                    connection.connect();
                    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                    converter.convert(sourceFile, pdfFile);
                    connection.disconnect();
                } catch (Exception e) {
                    log.error(e.getMessage(),e);
                }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    Kotlin的泛型约束
    前端经典布局
    Android UI 冻结处理方法
    Java-Day15 常用类解析 (包装类、Junit测试单元、Object类、String类及StringBuffer和StringBuilder)
    【查找算法】二分查找(C# + 递归、非递归和变种形式)
    基于深度学习的疫情期间网民情绪识别项目详解
    C++ 字面量
    cms之wordpress主题安装
    Docker
    文件上传笔记
  • 原文地址:https://blog.csdn.net/weixin_45494557/article/details/133905297