zoukankan      html  css  js  c++  java
  • 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");
        }
    
    }
  • 相关阅读:
    关于oracle的一些操作
    关于List的一些操作
    XSS挑战赛(2)
    阿里云图床搭建
    XSS挑战赛(1)
    Shiro remeberMe反序列化漏洞复现(Shiro-550)
    HTTP慢速拒绝服务攻击(Slow HTTP Dos)
    从Excel获取整列内容进行批量扫描
    PHP代码审计分段讲解(14)
    PHP代码审计分段讲解(13)
  • 原文地址:https://www.cnblogs.com/lifan12589/p/15624217.html
Copyright © 2011-2022 走看看