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中。

未完待续。。。。

目录
打赏
0
0
0
0
16
分享
相关文章
使用Spire.Doc来转换文本
使用Spire.Doc来转换文本 前段时间,我为不熟悉这个产品的读者们写了一篇关于我对 Spire.Doc的初识印象。Spire.Doc是一个专业的Word .NET库,它是专门为开发人员设计的用来快捷高效地在任何.NET(C#,VB.NET,ASP.NET)创建,载入,编辑,转化,打印 Word文档的。
1476 0
将word文档转换成pdf文件方法
在Java中,将Word文档转换为PDF文件可采用多种方法:1) 使用Apache POI和iText库,适合处理基本转换需求;2) Aspose.Words for Java,提供更高级的功能和性能;3) 利用LibreOffice命令行工具,适用于需要开源解决方案的场景。每种方法都有其适用范围,可根据具体需求选择。
SharePoint 2013 Word 转换PDF服务介绍及示例
原文:SharePoint 2013 Word 转换PDF服务介绍及示例   前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有将Word文档转换PDF的服务了,用起来还是不错的。
1395 0
c#调用cmd命令,将doc转换为 pdf
c#调用cmd命令,将doc转换为 pdf   using System; using System.Collections.Generic; using System.
1002 0
使用POI转换word doc文件
使用POI转换word doc文件 目录 1       转换为Html文件 2       转换为Xml文件 3       转换为Text文件          在POI中还存在有针对于word doc文件进行格式转换的功能。
1031 0
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
353 2
C# 对Word文档进行处理
public partial class Form1 : Form { public Microsoft.Office.Interop.Word.Document document; public Microsoft.Office.Interop.Word.ApplicationClass wd = null; public stri
1622 0
C# 给Word文档添加内容控件
C# 给Word文档添加内容控件 在MS Word中,我们可以通过内容控件来向word文档中插入预先定义好的模块,指定模块的内容格式(如图片、日期、列表或格式化的文本等),从而创建一个结构化的word文档。
994 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等