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

未完待续。。。。

相关文章
|
Web App开发 监控 JavaScript
1号防红网:什么是微信防红不死短链接?微信防红不死短链接代码示例
1号防红网:什么是微信防红不死短链接?微信防红不死短链接代码示例
1139 0
解决用@value从配置文件中给静态变量赋值的问题
解决用@value从配置文件中给静态变量赋值的问题
902 0
|
算法 安全 Java
java对zip、rar、7z文件带密码解压实例
本文采用java语言实现了对zip和rar、7z文件的解压统一算法。并对比了相应的解压速度,支持传入密码进行在线解压。
2833 0
java对zip、rar、7z文件带密码解压实例
|
Java Maven Windows
java 获取视频第一帧 | Java工具类
java 获取视频第一帧 | Java工具类
java 获取视频第一帧 | Java工具类
|
9月前
|
XML 算法 数据格式
当Word文档里的图片成了“拦路虎”:用Python批量处理图片的实战指南
本文分享用Python批量处理Word文档图片的实战方案:利用python-docx提取图片、Pillow统一调整尺寸与格式,并自动插入新报告文档。附完整代码与常见问题(如浮动图、.doc格式)解决方案,助你3分钟完成原本一整天的手工活。
974 1
|
12月前
|
数据采集 缓存 人工智能
微信 item_search - 关键词取文章列表接口对接全攻略:从入门到精通
本攻略详解基于搜狗微信搜索合规接口的item_search调用方法,涵盖接口认知、密钥获取、参数配置、签名生成、批量采集、异常处理及性能优化,结合Python实操示例,助力开发者高效实现微信文章列表的数据获取与舆情监测、内容聚合等应用,兼顾合规性与生产稳定性。
|
SQL 索引
在 SQL Server 中使用 STRING_AGG 函数
【8月更文挑战第5天】
5203 2
在 SQL Server 中使用 STRING_AGG 函数
|
数据挖掘 API 开发者
电商API接口:淘宝、1688、拼多多一键对接
本内容展示了部分电商API接口,涵盖淘宝/天猫、1688和拼多多等平台。功能包括商品详情查询(如`item_get`)、店铺信息获取(如`seller_info`)、关键词搜索(如`item_search`)、图片搜索(如`item_search_img`)及搜索词统计与推荐等。每个平台提供独立的Key和Secret以确保安全接入。除展示内容外,还有更多国内外电商平台接口可供使用。适合开发者快速集成和调用电商数据,助力应用开发与数据分析。
|
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页免费但有水印,不推荐。
1999 0