Skip to content

合并PDF

注意:在学习不同函数的使用前,建议先阅读请求描述,了解基本的PDF处理流程。使用不同函数时,可以自行设定上传文件时的特殊参数,其他基本步骤一致。

PDF合并:

-在同一个任务中,上传多个文件(最多5个),若不传入pageOptions,则为多文件合并。

-在同一个任务中,上传多个文件(最多5个),pageOptions,合并指定页码的多个文件。

java
{
    "pageOptions": "['1,2']"
}

必要参数

pageOptions:合并文档的页面范围,页码从1开始,例如:1,2,4,6,9-11(如果默认不输入所有页面,则输入的页码不能超过文档的最大页码)

示例

  1. 授权

    您需要将认证响应中的 替换为从控制台获取的 publicKeysecretKey,并使用 accessToken

    curl
    curl --location --request POST 'https://api-server.compdf.com/server/v1/oauth/token' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "publicKey": "publicKey",
        "secretKey": "secretKey"
    }'
    java
    import java.io.*;
    import okhttp3.*;
    public class main {
      public static void main(String []args) throws IOException{
        OkHttpClient client = new OkHttpClient().newBuilder()
          .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "{\n    \"publicKey\": \"{{public_key}}\",\n    \"secretKey\": \"{{secret_key}}\"\n}");
        Request request = new Request.Builder()
          .url("https://api-server.compdf.com/server/v1/oauth/token")
          .method("POST", body)
          .build();
        Response response = client.newCall(request).execute();
      }
    }
  2. 创建任务

    您需要将 替换为上一步获取的 accessToken,将 ***替换为所需的界面和任务错误消息语言类型。请求成功后,您将从响应中收到 taskId

    curl
    curl --location --request GET 'https://api-server.compdf.com/server/v1/task/pdf/merge' \
    --header 'Authorization: Bearer accessToken'
    java
    import java.io.*;
    import okhttp3.*;
    public class main {
      public static void main(String []args) throws IOException{
        OkHttpClient client = new OkHttpClient().newBuilder()
          .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
          .url("https://api-server.compdf.com/server/v1/task/pdf/merge?language={{language}}")
          .method("GET", body)
          .addHeader("Authorization", "Bearer {{accessToken}}")
          .build();
        Response response = client.newCall(request).execute();
      }
    }
  3. 上传文件

    替换为您要转换的文件,将 替换为您上一步获取到的 taskId,将 替换为您需要的界面错误信息语言类型,将 替换为您第一步获取到的 accessToken

    PDF合并:

    “pageOptions”:“['1,2']”

    -同一个任务中,上传多个文件(最多5个),如果不传pageOptions,则为多文件合并。

    -同一个任务中,上传多个文件(最多5个),pageOptions,合并多个文件指定的页码。

    -上传接口只支持单文件上传。

    curl
    curl --location --request POST 'https://api-server.compdf.com/server/v1/file/upload' \
    --header 'Authorization: Bearer accessToken' \
    --form 'file=@"test.pdf"' \
    --form 'taskId="taskId"' \
    --form 'password=""' \
    --form 'parameter="{    \"pageOptions\": ['\''1,2'\'']}"'
    java
    import java.io.*;
    import okhttp3.*;
    public class main {
      public static void main(String []args) throws IOException{
        OkHttpClient client = new OkHttpClient().newBuilder()
          .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
          .addFormDataPart("file","{{file}}",
                           RequestBody.create(MediaType.parse("application/octet-stream"),
                                              new File("<file>")))
          .addFormDataPart("taskId","{{taskId}}")
          .addFormDataPart("language","{{language}}")
          .addFormDataPart("password","")
          .addFormDataPart("parameter","{\"pageOptions\": \"['1,2']\"}")
          .build();
        Request request = new Request.Builder()
          .url("https://api-server.compdf.com/server/v1/file/upload")
          .method("POST", body)
          .addHeader("Authorization", "Bearer {{accessToken}}")
          .build();
        Response response = client.newCall(request).execute();
      }
    }
  4. 执行任务

    替换为 创建任务 步骤中获取的 taskId,将 替换为第一步获取的 access_token,将 替换为所需的界面错误信息语言类型。

    curl
    curl --location -g --request GET 'https://api-server.compdf.com/server/v1/execute/start?taskId=taskId' \
    --header 'Authorization: Bearer accessToken'
    java
    import java.io.*;
    import okhttp3.*;
    public class main {
     public static void main(String []args) throws IOException{
       OkHttpClient client = new OkHttpClient().newBuilder()
         .build();
       MediaType mediaType = MediaType.parse("text/plain");
       RequestBody body = RequestBody.create(mediaType, "");
       Request request = new Request.Builder()
         .url("https://api-server.compdf.com/server/v1/execute/start?taskId={{taskId}}&language={{language}}")
         .method("GET", body)
         .addHeader("Authorization", "Bearer {{accessToken}}")
         .build();
       Response response = client.newCall(request).execute();
     }
    }
  5. 获取任务信息

    替换为 创建任务 步骤中获取的 taskId,将 替换为第一步中获取的 access_token

    curl
    curl --location -g --request GET 'https://api-server.compdf.com/server/v1/task/taskInfo?taskId=taskId' \
    --header 'Authorization: Bearer accessToken'
    java
    import java.io.*;
    import okhttp3.*;
    public class main {
      public static void main(String []args) throws IOException{
        OkHttpClient client = new OkHttpClient().newBuilder()
          .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
          .url("https://api-server.compdf.com/server/v1/task/taskInfo?taskId={{taskId}}")
          .method("GET", body)
          .addHeader("Authorization", "Bearer {{accessToken}}")
          .build();
        Response response = client.newCall(request).execute();
      }
    }

结果

文件类型说明
.pdf合并后的 PDF 文件