Skip to content

PDF Merge Tool Guide

Note:Before learning how to use different functions, we recommend that read the Request Workflow to know a basic PDF processing process. When using different functions, you can set their own special parameters when uploading files. Other basic steps are consistent.

PDF Merge:

  • In the same task, upload multiple files (up to five). If pageOptions is not provided, it will merge multiple files.
  • In the same task, upload multiple files (up to five). If pageOptions is provided, it will merge the specified pages of multiple files.
java
{
    "pageOptions": "['1,2']"
}

Required Parameters:

pageOptions: Page range of the merged documents, starting from 1. For example: 1,2,4,6,9-11 (if not provided, all pages are included by default; the provided page numbers cannot exceed the maximum page number of the document).

Request Example:

Replace apiKey with the publicKey obtained from the dashboard, file with the file you want to convert, and language with your preferred interface error prompt language type.

curl
curl --location --request POST 'https://api-server.compdf.com/server/v2/process/pdf/merge' \
--header 'x-api-key: apiKey' \
--header 'Accept: */*' \
--header 'Connection: keep-alive' \
--header 'Content-Type: multipart/form-data' \
--form 'file=@"file1"' \
--form 'file=@"file2"' \
--form 'password="" \
--form 'parameter="{ \"pageOptions\": \"['1,2']\" }"' \
--form 'language="1"'
java
import java.io.*;
import okhttp3.*;

public class Main {
    public static void main(String[] args) throws IOException {
        List<File> filesToUpload = List.of(
            new File("file1.pdf"),
            new File("file2.pdf"),
            new File("file3.pdf")
        );
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MultipartBody.Builder bodyBuilder = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("language", "{{language}}")
            .addFormDataPart("password", "")
            .addFormDataPart("parameter", "{\"pageOptions\": \"['1,2']\"}");

        for (File file : filesToUpload) {
            bodyBuilder.addFormDataPart(
                "file", 
                file.getName(),
                RequestBody.create(
                    MediaType.parse("application/octet-stream"),  
                    file
                )
            );
        }
        Request request = new Request.Builder()
            .url("https://api-server.compdf.com/server/v2/process/pdf/merge")
            .method("POST", bodyBuilder.build())
            .addHeader("x-api-key", "{{apiKey}}")
            .build();
        Response response = client.newCall(request).execute();
    }
}

Response Information:

A successful request returns an HTTP 200 OK status code and a JSON response body showing the order details.

Response type:application/json

Response ParameterData TypeDescription
codeStringHTTP request status, "200" indicates success
messageStringRequest message
dataObjectReturn result
+taskIdStringTask ID
+taskFileNumintNumber of files processed in the task
+taskSuccessNumintNumber of files successfully processed in the task
+taskFailNumintNumber of files failed in the task
+taskStatusStringTask status
+assetTypeIdintUsed asset type ID
+taskCostintTask cost
+taskTimeintTask duration
+sourceTypeStringOriginal format
+targetTypeStringTarget format
+fileInfoDTOListArrayTask file information
++fileKeyStringFile key
++taskIdStringTask ID
++fileNameStringOriginal file name
++downFileNameStringDownload file name
++fileUrlStringOriginal file URL
++downloadUrlStringProcessed result file download URL
++sourceTypeStringOriginal format
++targetTypeStringTarget format
++fileSizeintFile size
++convertSizeintProcessed result file size
++convertTimeintProcessing time
++statusStringFile processing status
++failureCodeStringFile processing failure error code
++failureReasonStringFile processing failure description
++fileParameterStringProcessing parameter

Response Example:

json
"code": "200",
"msg": "success",
"data": {
    "taskId": "f416dbcf-0c10-4f93-ab9e-a835c1f5dba1",
    "taskFileNum": 1,
    "taskSuccessNum": 1,
    "taskFailNum": 0,
    "taskStatus": "<taskStatus>",
    "assetTypeId": 0,
    "taskCost": 1,
    "taskTime": 1,
    "sourceType": "<sourceType>",
    "targetType": "<targetType>",
    "fileInfoDTOList": [
      {
        "fileKey": "<fileKey>",
        "taskId": "<taskId>",
        "fileName": "<fileName>",
        "downFileName": "<downFileName>",
        "fileUrl": "<fileUrl>",
        "downloadUrl": "<downloadUrl>",
        "sourceType": "<sourceType>",
        "targetType": "<targetType>",
        "fileSize": 24475,
        "convertSize": 6922,
        "convertTime": 8,
        "status": "<status>",
        "failureCode": "",
        "failureReason": "",
        "fileParameter": "<fileParameter>"
      }
    ]
}

Result:

File TypeDescription
.pdfMerged PDF file

Asynchronous Request

If you need to use the file asynchronous processing flow, please read the Asynchronous Request Instructions.