documents4j 文档转换

简介: documents4j 文档转换

documents4j 是一个 Java 库,可以将文档转换为另一种文档格式。

https://github.com/documents4j/documents4j

<parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.6.0</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-local</artifactId>
        <version>1.1.5</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-transformer-msoffice-word</artifactId>
        <version>1.1.5</version>
    </dependency>
</dependencies>
@RequestMapping("/to")
@RestController
public class PdfController {
    /**
     * doc文件转pdf
     *
     * @param path 文件路径
     */
    @RequestMapping("/doc2pdf")
    public ResponseEntity<byte[]> doc2pdfFileUpload(String path) throws IOException {
        URL url = new URL(path);
        URLConnection conn = url.openConnection();
        InputStream inputStream = conn.getInputStream();
        return doc2Pdf(inputStream, ".doc", "fileName.pdf");
    }
    /**
     * docx、xlsx、转pdf
     *
     * @param fileType docx、doc、xls、xlsx
     * @param fileName pdf名称
     */
    public ResponseEntity<byte[]> doc2Pdf(InputStream docxInputStream, String fileType, String fileName) throws IOException {
        // 转换后的pdf临时路径
        File outputFile = new File("C:/Users/admin002/Desktop/folder/doc/" + fileName);
        IConverter converter = LocalConverter.builder().build();
        ResponseEntity<byte[]> fileResult = null;
        try (OutputStream outputStream = Files.newOutputStream(outputFile.toPath());
             // 导出pdf文件给前端
             FileInputStream inputStream = new FileInputStream(outputFile);
             ) {
            if (".docx".equals(fileType)) {
                converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            } else if (".doc".equals(fileType)) {
                converter.convert(docxInputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
            } else if (".xls".equals(fileType)) {
                converter.convert(docxInputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();
            } else if (".xlsx".equals(fileType)) {
                converter.convert(docxInputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
            }
            byte[] bytes = new byte[(int) outputFile.length()];
            inputStream.read(bytes);
            HttpHeaders headers = new HttpHeaders();
            // 此处pdf名称需要传入
            headers.set("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            fileResult = new ResponseEntity(bytes, headers, HttpStatus.OK);
        } finally {
            docxInputStream.close();
        }
        return fileResult;
    }
}

以上方式依赖于Windows上的MS Word、MS Excel,必须使用Windows。

只有在以下情况下才能运行 LocalConverter构建:

  • JVM在MS Windows平台上运行,该平台附带VBS的Microsoft脚本主机。
  • MS Word 版本必须在2007 或更高版本。安装了 PDF 插件时,才支持 PDF 转换。该插件包含在了Word 2010及更高版本的MS Word中。

未完待续。。。。

相关文章
SpringBoot 集成log4j2
SpringBoot 集成log4j2
667 0
SpringBoot 集成log4j2
|
10月前
|
关系型数据库 MySQL Java
SpringBoot集成Sharding-Jdbc分库分表
ShardingSphere是一套开源分布式数据库中间件解决方案,包含Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar三款产品,提供数据分片、分布式事务和数据库治理功能,适用于多样化应用场景。
1260 0
SpringBoot集成Sharding-Jdbc分库分表
|
Linux 虚拟化 数据安全/隐私保护
银河麒麟V10 VMWare安装保姆级教程
银河麒麟V10 VMWare安装保姆级教程
23488 5
银河麒麟V10 VMWare安装保姆级教程
|
Java Linux 数据安全/隐私保护
libreOffice word 转 pdf
在Windows环境下,使用documents4j进行DOCX到PDF的转换大约需要20秒,而Linux环境下通过Docker配置LibreOffice进行转换,时间仅为1.4秒。documents4j仅适用于Windows且需Microsoft Office支持,libreOfficeCommand则依赖于Linux环境。Spire.Doc.Free有前4页免费但有水印,不推荐。
1724 0
|
Java Apache Maven
Java将word文档转换成pdf文件的方法?
【10月更文挑战第13天】Java将word文档转换成pdf文件的方法?
5790 1
|
Java BI API
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
这篇文章介绍了如何在Spring Boot项目中整合iTextPDF库来导出PDF文件,包括写入大文本和HTML代码,并分析了几种常用的Java PDF导出工具。
4765 0
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
|
JavaScript Java Kotlin
深入 Spring Cloud Gateway 过滤器
Spring Cloud Gateway 是新一代微服务网关框架,支持多种过滤器实现。本文详解了 `GlobalFilter`、`GatewayFilter` 和 `AbstractGatewayFilterFactory` 三种过滤器的实现方式及其应用场景,帮助开发者高效利用这些工具进行网关开发。
2252 1
|
Java Apache Maven
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
11383 0
|
Java Spring 容器
springboot @RequiredArgsConstructor @Lazy解决循环依赖的原理
【10月更文挑战第15天】在Spring Boot应用中,循环依赖是一个常见问题,当两个或多个Bean相互依赖时,会导致Spring容器陷入死循环。本文通过比较@RequiredArgsConstructor和@Lazy注解,探讨它们解决循环依赖的原理和优缺点。@RequiredArgsConstructor通过构造函数注入依赖,使代码更简洁;@Lazy则通过延迟Bean的初始化,打破创建顺序依赖。两者各有优势,需根据具体场景选择合适的方法。
1183 4

热门文章

最新文章