Word转PDF 并转成base64(亲测可用)

简介: Word转PDF 并转成base64(亲测可用)

控制台

@Override
  public String printFile(Long id) throws IOException {
    try {
      FileDypeVo fileDypeVos = fileMapper.selectFileId(id);
      String path = fileDypeVos.getFilePath();
      String name = fileDypeVos.getFileName();
      File file = new File(path + name);
      // 获取后缀
      String str = name.substring(name.lastIndexOf(".") + 1);
      // 获取前面的名字
      String str1 = name.substring(0, name.lastIndexOf(".") - 1);
      // 拼装为pdf
      String str2 = str1 + ".pdf";
      LOGGER.info("str==》" + str);
      LOGGER.info("str1===>" + str1);
      LOGGER.info("str2===>" + str2);
      LOGGER.info("file===>" + file);
      BASE64Encoder encoder = new BASE64Encoder();
      if (str.equals("pdf")) {
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        System.out.println("pdf预览成功");
        return encoder.encode(buffer);
      }
      if (str.equals("docx") || str.equals("doc")) {
        String sourcePath = path + name;
        String targetPath = path + str2;
        String files01 = str2;
        LOGGER.info("sourcePath==>" + sourcePath + "==>targetPath==>" + targetPath + "==files" + files01);
        File fileYan = new File(targetPath);
        // 查看是否生成过pdf了查看文件是否是存在 不存在则进入 存在不执行
        if (!fileYan.exists()) {
          ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);
        }
        FileInputStream inputFile = new FileInputStream(fileYan);
        byte[] buffer = new byte[(int) fileYan.length()];
        inputFile.read(buffer);
        inputFile.close();
        System.out.println("pdf预览成功");
        return encoder.encode(buffer);
      } else {
        throw new ServiceException("该文档格式无法预览");
      }
    } catch (Exception e) {
      throw new ServiceException("预览失败" + e.getMessage());
      // TODO: handle exception
    }
  }

核心代码

ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);

实现类

/**
     * 通过documents4j 实现word转pdf
     *
     * @param sourcePath 源文件地址 如 /root/example.doc
     * @param targetPath 目标文件地址 如 /root/example.pdf
     */
    public static void documents4jWordToPdf(String sourcePath, String targetPath) {
        File inputWord = new File(sourcePath);
        File outputFile = new File(targetPath);
        try  {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF).execute();
            outputStream.close();
        } catch (Exception e) {
          LOGGER.error("[documents4J] word转pdf失败:{}"+ e.toString());
           // log.error("[documents4J] word转pdf失败:{}", e.toString());
        }
    }

POM

<dependency>
      <groupId>com.documents4j</groupId>
      <artifactId>documents4j-local</artifactId>
      <version>1.0.3</version>
    </dependency>
    <dependency>
      <groupId>com.documents4j</groupId>
      <artifactId>documents4j-transformer-msoffice-word</artifactId>
      <version>1.0.3</version>
          </dependency>

Gradle

compile 我这边设置以他为空,各位你们可自行查看;manve库和我们这边不一样

compile group: 'com.documents4j', name: 'documents4j-local', version: '1.0.3'
     // https://mvnrepository.com/artifact/com.documents4j/documents4j-transformer-msoffice-word
     compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-word', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-api', version: '1.0.3'
  // https://mvnrepository.com/artifact/com.documents4j/documents4j-transformer
     compile group: 'com.documents4j', name: 'documents4j-transformer', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-transformer-api', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-base', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-util-all', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-util-conversion', version: '1.0.3'
     compile group: 'com.documents4j', name: 'documents4j-util-transformer-process', version: '1.0.3'
     compile group: 'org.zeroturnaround', name: 'zt-exec', version: '1.8'

扩展其他方式 apose

https://yanwc.blog.csdn.net/article/details/128285226


相关文章
|
2月前
|
小程序
公众号如何添加附传Word、Excel、Pdf、PPT文档
公众号里添加一些文档给公众号粉丝下载,比如课件PPT、申请表Word文档、岗位需求Excel表、大赛入围/获奖名单等。公众号本身是不支持直接上传文件的,但我们可以通过附件小程序“间接”上传文件。
523 0
|
3月前
|
监控 Linux 数据安全/隐私保护
Python实现Word转PDF全攻略:从入门到实战
在数字化办公中,Python实现Word转PDF自动化,可大幅提升处理效率,解决格式兼容问题。本文详解五种主流方案,包括跨平台的docx2pdf、Windows原生的pywin32、服务器部署首选的LibreOffice命令行、企业级的Aspose.Words,以及轻量级的python-docx+pdfkit组合。每种方案均提供核心代码与适用场景,并涵盖中文字体处理、表格优化、批量进度监控等实用技巧,助力高效办公自动化。
918 0
|
4月前
|
存储 人工智能 文字识别
三款安卓手机word编辑器下载,Microsoft Word,wps office,Word手机版,手机word编辑查看阅读器,PDF转换器apk下载
WPS Office是一款功能强大的办公软件,支持文档编辑、表格处理和演示文稿制作,兼容多种格式并提供丰富的云服务。它具备低内存占用、快速运行的特点,支持跨设备同步与多人协作,内置海量模板及AI辅助功能,如智能写作和PPT自动生成。此外,还可扫描文件、编辑PDF并转换为其他格式,极大提升办公效率,适合手机用户便捷操作。
419 1
|
6月前
|
人工智能 算法 安全
使用CodeBuddy实现批量转换PPT、Excel、Word为PDF文件工具
通过 CodeBuddy 实现本地批量转换工具,让复杂的文档处理需求转化为 “需求描述→代码生成→一键运行” 的极简流程,真正实现 “技术为效率服务” 的目标。感兴趣的快来体验下把
274 10
|
前端开发
PDF文件上传转成base64编码并支持预览
PDF文件上传转成base64编码并支持预览
682 12
DeepSeek + Xmind,1分钟自动把pdf/word文档转成思维导图
DeepSeek与Xmind结合,1分钟将PDF、Word文档转换为思维导图。只需四步:上传文档至DeepSeek,请求转换为Markdown格式,保存文件并修改后缀为.md,最后导入Xmind生成思维导图。轻松驾驭复杂文档,适用于学习笔记、工作报告、项目规划等场景,大幅提升效率!
|
11月前
|
人工智能 文字识别 数据挖掘
MarkItDown:微软开源的多格式转Markdown工具,支持将PDF、Word、图像和音频等文件转换为Markdown格式
MarkItDown 是微软开源的多功能文档转换工具,支持将 PDF、PPT、Word、Excel、图像、音频等多种格式的文件转换为 Markdown 格式,具备 OCR 文字识别、语音转文字和元数据提取等功能。
2444 9
MarkItDown:微软开源的多格式转Markdown工具,支持将PDF、Word、图像和音频等文件转换为Markdown格式
|
Java Apache Maven
将word文档转换成pdf文件方法
在Java中,将Word文档转换为PDF文件可采用多种方法:1) 使用Apache POI和iText库,适合处理基本转换需求;2) Aspose.Words for Java,提供更高级的功能和性能;3) 利用LibreOffice命令行工具,适用于需要开源解决方案的场景。每种方法都有其适用范围,可根据具体需求选择。
|
Java Apache Maven
Java将word文档转换成pdf文件的方法?
【10月更文挑战第13天】Java将word文档转换成pdf文件的方法?
4111 1
|
Python
Python——将PPT和Word转为PDF文件
Python——将PPT和Word转为PDF文件
294 1
下一篇
oss云网关配置