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


相关文章
|
1月前
|
Linux Python Windows
Python PDF文件转Word格式,只需要3秒(附打包)
Python PDF文件转Word格式,只需要3秒(附打包)
58 3
Python PDF文件转Word格式,只需要3秒(附打包)
|
1月前
|
Python
Python——将PPT和Word转为PDF文件
Python——将PPT和Word转为PDF文件
41 1
|
29天前
|
Python
还不会免费将PDF转为Word?你可以试试这3种工具!
还不会免费将PDF转为Word?你可以试试这3种工具!
|
2月前
|
JavaScript Java
Java 将Markdown文件转换为Word和PDF文档
【7月更文挑战第5天】Java中使用`Spire.Doc for Java`库可方便地将Markdown转换为Word或PDF。基本步骤包括导入模块,创建`Document`对象,加载Markdown文件,然后保存为目标格式(`.docx`或`.pdf`)。若遇到`Invalid UTF-8 stream`错误,需确保Markdown文件是UTF-8无BOM编码。页面设置可通过`PageSetup`类调整。注意,实际应用会依据具体需求和环境有所调整。
152 6
|
3月前
使用LabVIEW打开默认应用程序中的文档(PDF,Word,Excel,Html)
使用LabVIEW的&quot;Open a Document on Disk.vi&quot;,存于&lt;LabVIEW&gt;\vi.lib\Platform\browser.llb,可让默认应用打开硬盘文档。此VI仅基础打开功能,高级控制推荐LabVIEW Report Generation Toolkit或ActiveX。注意:避免版本升级问题,最好将VI复制到vi.lib外的目录。
120 3
|
4月前
|
Java Apache
Java将word、excel文件转成pdf文件
【5月更文挑战第26天】Java将word、excel文件转成pdf文件
1636 1
|
3月前
|
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页免费但有水印,不推荐。
185 0
|
4月前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
180 0
|
4月前
|
Java Linux 数据安全/隐私保护
Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
【2月更文挑战第3天】Java 将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
277 0
|
4月前
|
Java 程序员 数据安全/隐私保护
分享一个word转pdf的工具类Aspose[java]
分享一个word转pdf的工具类Aspose[java]
135 0

热门文章

最新文章