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
536 0
SpringBoot 集成log4j2
|
监控 Java 应用服务中间件
谈谈你对spring boot 3.0的理解
1. Java 版本要求:Spring Boot 3.0 要求使用 Java 17 或更高版本,这可能会对一些仍在使用旧版 Java 的项目造成兼容性问题。需要确保项目使用的 Java 版本符合要求,并考虑是否需要升级 JDK 版本。 2. 底层依赖项迁移:Spring Boot 3.0 将所有底层依赖项从 Java EE 迁移到了 Jakarta EE API,基于 Jakarta EE 9 并尽可能地兼容 Jakarta EE 10。这可能会对一些使用了 Java EE 的应用造成影响,需要进行相应的修改和调整。 3. 插件和库的支持:尽管 Spring Boot 3.0 支持更多的插件和
1736 0
|
存储 Kubernetes Cloud Native
一文搞懂云原生架构
目前,每个 IT 资源或产品都作为服务提供。而且伴随云计算的滚滚浪潮,云原生(CloudNative)的概念应运而生,云原生很火,火得一塌糊涂,都0202年了,如果还不懂云原生,那真的out了。因此,云原生软件开发成为每个企业的关键要求,无论其规模和性质如何。在加入云计算潮流之前,了解什么是云原生架构以及如何为云原生应用程序需求设计正确的架构非常重要。
一文搞懂云原生架构
|
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页免费但有水印,不推荐。
1341 0
|
搜索推荐 Java 开发者
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 问题处理
【5月更文挑战第14天】org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 问题处理
5723 1
|
Java Maven
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案
在执行Maven项目中的`install`命令时,遇到编译插件版本不匹配的错误。具体报错为:`maven-compiler-plugin:3.13.0`要求Maven版本至少为3.6.3。解决方案是将Maven版本升级到3.6.3或降低插件版本。本文详细介绍了如何下载、解压并配置Maven 3.6.3,包括环境变量设置和IDEA中的Maven配置,确保项目顺利编译。
15027 5
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案
|
Java Apache Maven
Java将word文档转换成pdf文件的方法?
【10月更文挑战第13天】Java将word文档转换成pdf文件的方法?
4975 1
|
Java Linux
POI 生成word 转 pdf
根据业务需要 需要出一份 PDF 文件 作为 公告的附件使用 PDF文件中 需要有 各种数据作为展示 是动态生成的
3094 0
POI  生成word 转 pdf
|
文字识别 Java 计算机视觉
【神技解锁】Spring Boot + Tess4J:一图胜千言,瞬间变文字,颠覆你的视觉体验!
【8月更文挑战第29天】本文详细介绍了如何在 Spring Boot 项目中集成 Tess4J,实现高效本地与远程图片的光学字符识别(OCR)处理。通过具体步骤展示了如何添加依赖、配置 OCR 引擎、创建图片处理服务及控制器,并提供了测试方法。这不仅适用于文本识别场景,还可扩展至其他图像处理任务,为项目增添实用功能。
1977 1
|
SQL Java 关系型数据库
SpringBoot 系列之 MyBatis输出SQL日志
这篇文章介绍了如何在SpringBoot项目中通过MyBatis配置输出SQL日志,具体方法是在`application.yml`或`application.properties`中设置MyBatis的日志实现为`org.apache.ibatis.logging.stdout.StdOutImpl`来直接在控制台打印SQL日志。
SpringBoot 系列之 MyBatis输出SQL日志