SpringBoot集成阿里云文档格式转换实现pdf转换word,excel

一、前置条件

1.1 创建accessKey

如何申请:https://help.aliyun.com/zh/ram/user-guide/create-an-accesskey-pair

1.2 开通服务

官方地址:https://docmind.console.aliyun.com/doc-overview

未开通服务时需要点击开通按钮,然后才能调用相关api。

二、代码实现

2.1 引入依赖

xml 复制代码
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>tea-openapi</artifactId>
    <version>0.2.5</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>docmind_api20220711</artifactId>
    <version>2.0.3</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.50</version>
</dependency>

2.2 pdf转换word

官方文档:https://help.aliyun.com/zh/document-mind/developer-reference/convertpdftoword

java 复制代码
package net.lab1024.sa.admin.util;

import com.aliyun.docmind_api20220711.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.docmind_api20220711.Client;
import com.aliyun.teautil.models.RuntimeOptions;

import java.io.FileInputStream;
import java.util.List;

public class PdfConvertUtil {

    private static final String OK = "200";

    private static final String ACCESS_KEY_ID = "xxx";

    private static final String ACCESS_KEY_SECRET = "xxx";

    public static void main(String[] args) throws Exception {
        String id = submitPdfToWord("C:\\Users\\admin\\Desktop\\example.pdf");

        // 10秒后再查询结果,等阿里云处理一会儿
        Thread.sleep(10000);

        List<GetDocumentConvertResultResponseBody.GetDocumentConvertResultResponseBodyData> data = queryPdfToWord(id);
    }

    /**
     * 客户端
     *
     * @return
     * @throws Exception
     */
    private static Client getClient() throws Exception {
        Config config = new Config();
        config.setAccessKeyId(ACCESS_KEY_ID);
        config.setAccessKeySecret(ACCESS_KEY_SECRET);
        // 访问的域名,支持ipv4和ipv6两种方式,ipv6请使用docmind-api-dualstack.cn-hangzhou.aliyuncs.com
        config.setEndpoint("docmind-api.cn-hangzhou.aliyuncs.com");
        return new Client(config);
    }

    /**
     * 提交pdf转换word转换任务
     *
     * @return
     * @throws Exception
     */
    public static String submitPdfToWord(String filePath) throws Exception {
        Client client = getClient();

        // 请求参数
        SubmitConvertPdfToWordJobAdvanceRequest advanceRequest = new SubmitConvertPdfToWordJobAdvanceRequest();
        advanceRequest.setFileUrlObject(new FileInputStream(filePath));
        advanceRequest.setFileName("example.pdf");

        // 运行参数
        RuntimeOptions runtime = new RuntimeOptions();


        // 发送请求
        SubmitConvertPdfToWordJobResponse response = client.submitConvertPdfToWordJobAdvance(advanceRequest, runtime);

        // 处理结果
        SubmitConvertPdfToWordJobResponseBody body = response.getBody();
        if (!OK.equals(body.getCode())) {
            throw new RuntimeException("pdf转换word任务提交失败");
        }
        return body.getData().getId();
    }

    /**
     * 查询pdf转换word转换任务
     *
     * @param id
     * @return
     * @throws Exception
     */
    public static List<GetDocumentConvertResultResponseBody.GetDocumentConvertResultResponseBodyData> queryPdfToWord(String id) throws Exception {
        Client client = getClient();

        // 请求参数
        GetDocumentConvertResultRequest resultRequest = new GetDocumentConvertResultRequest();
        resultRequest.setId(id);

        // todo 这里是简单处理 需要轮询120分钟,10秒一次

        GetDocumentConvertResultResponse response = client.getDocumentConvertResult(resultRequest);
        GetDocumentConvertResultResponseBody body = response.getBody();
        if (!OK.equals(body.getCode())) {
            throw new RuntimeException("pdf转换word任务查询失败");
        }

        Boolean completed = body.getCompleted();
        if (!completed) {
            throw new RuntimeException("pdf转换word任务未完成");
        }

        String status = body.getStatus();
        if (!"Success".equals(status)) {
            throw new RuntimeException("pdf转换word任务转换失败");
        }
        return body.getData();
    }

}

2.3 pdf转换excel

官方文档:https://help.aliyun.com/zh/document-mind/developer-reference/convertpdftoexcel

java 复制代码
package net.lab1024.sa.admin.util;

import com.aliyun.docmind_api20220711.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.docmind_api20220711.Client;
import com.aliyun.teautil.models.RuntimeOptions;

import java.io.FileInputStream;
import java.util.List;

public class PdfConvertUtil {

    private static final String OK = "200";

    private static final String ACCESS_KEY_ID = "xxx";

    private static final String ACCESS_KEY_SECRET = "xxx";

    public static void main(String[] args) throws Exception {
        String id = submitPdfToExcel("C:\\Users\\admin\\Desktop\\example.pdf");

        // 10秒后再查询结果,等阿里云处理一会儿
        Thread.sleep(10000);

        List<GetDocumentConvertResultResponseBody.GetDocumentConvertResultResponseBodyData> data = queryPdfToExcel(id);
    }

    /**
     * 客户端
     *
     * @return
     * @throws Exception
     */
    private static Client getClient() throws Exception {
        Config config = new Config();
        config.setAccessKeyId(ACCESS_KEY_ID);
        config.setAccessKeySecret(ACCESS_KEY_SECRET);
        // 访问的域名,支持ipv4和ipv6两种方式,ipv6请使用docmind-api-dualstack.cn-hangzhou.aliyuncs.com
        config.setEndpoint("docmind-api.cn-hangzhou.aliyuncs.com");
        return new Client(config);
    }

    /**
     * 提交pdf转换excel转换任务
     * @return
     * @throws Exception
     */
    public static String submitPdfToExcel(String filePath) throws Exception {
        Client client = getClient();

        // 请求参数
        SubmitConvertPdfToExcelJobAdvanceRequest advanceRequest = new SubmitConvertPdfToExcelJobAdvanceRequest();
        advanceRequest.setFileUrlObject(new FileInputStream(filePath));
        advanceRequest.setFileName("example.pdf");
        // 合并为1个sheet
        advanceRequest.setForceMergeExcel(true);

        // 运行参数
        RuntimeOptions runtime = new RuntimeOptions();

        // 发送请求
        SubmitConvertPdfToExcelJobResponse response = client.submitConvertPdfToExcelJobAdvance(advanceRequest, runtime);

        // 处理结果
        SubmitConvertPdfToExcelJobResponseBody body = response.getBody();
        if (!OK.equals(body.getCode())) {
            throw new RuntimeException("pdf转换excel任务提交失败");
        }
        return body.getData().getId();
    }

    /**
     * 查询pdf转换excel转换任务
     * @param id
     * @return
     * @throws Exception
     */
    public static List<GetDocumentConvertResultResponseBody.GetDocumentConvertResultResponseBodyData> queryPdfToExcel(String id) throws Exception {
        Client client = getClient();

        // 请求参数
        GetDocumentConvertResultRequest resultRequest = new GetDocumentConvertResultRequest();
        resultRequest.setId(id);

        // todo 这里是简单处理 需要轮询120分钟,10秒一次

        GetDocumentConvertResultResponse response = client.getDocumentConvertResult(resultRequest);
        GetDocumentConvertResultResponseBody body = response.getBody();
        if (!OK.equals(body.getCode())) {
            throw new RuntimeException("pdf转换excel任务查询失败");
        }

        Boolean completed = body.getCompleted();
        if (!completed) {
            throw new RuntimeException("pdf转换excel任务未完成");
        }

        String status = body.getStatus();
        if (!"Success".equals(status)) {
            throw new RuntimeException("pdf转换excel任务转换失败");
        }
        return body.getData();
    }

}    
相关推荐
allenXer4 小时前
Spring Boot测试全景指南:JUnit 5 + Testcontainers实现单元与集成测试
spring boot·微服务·log4j
熟悉的新风景6 小时前
springboot项目或其他项目使用@Test测试项目接口配置-spring-boot-starter-test
java·spring boot·后端
军军君0112 小时前
基于Springboot+UniApp+Ai实现模拟面试小工具三:后端项目基础框架搭建上
前端·vue.js·spring boot·面试·elementui·微信小程序·uni-app
簪花走马过长安12 小时前
【PDF识别改名】使用京东云OCR完成PDF图片识别改名,根据PDF图片内容批量改名详细步骤和解决方案
ui·pdf·ocr·excel·京东云·wps·图片区域识别重命名
简放13 小时前
Spring-AI系列-AI模型API
spring boot·aigc·openai
泉城老铁13 小时前
Gitee上开源主流的springboot框架一探究竟
spring boot·后端·架构
ComPDFKit14 小时前
为什么有些PDF无法复制文字?原理分析与解决方案
人工智能·pdf·ocr
Yang's14 小时前
四种高效搭建SpringBoot项目的方式详解
java·spring boot·后端
bing_15814 小时前
在Spring Boot 开发中 Bean 的声明和依赖注入最佳的组合方式是什么?
java·spring boot·后端·bean
开开心心就好14 小时前
电脑桌面整理工具,一键自动分类
运维·服务器·前端·智能手机·pdf·bash·symfony