Combine PDF Files with PDF API

See more on “[How to Combine PDF Files Mac](https://www.compdf.com/blog/combine-pdf-files-without-adobe?utm_source=qiita_20241114&utm_medium=mergepdf)” and Combine PDF Without Adobe.
 

## 1. Register and authentication
You can [register a free ComPDFKit API account](https://api-dashboard.compdf.com/dashboard?utm_source=qiita_20241114&utm_medium=mergepdf), which allows you to process more than 1,000 documents for free and unlimited within 30 days.
ComPDFKit API uses the JSON Web Tokens method for secure authentication. Get your Public Key and Secret Key from the control panel and authenticate it as follows.

`// Create a client
let client: CPDFClient = CPDFClient(publicKey: public_key, secretKey: secret_key)`




## 2. Create PDF merge tasks
Select the PDF merge tool and replace it with the accessToken obtained in the previous step. Replace the display language of the error message with the language type you want. Then you can get the taskId in the response data.

`// Create a task
// Create an example of a PDF merge task
    let taskModel = await client.createTask(url: CPDFDocumentEditor.MERGE, language: .english)
// Get a task id
    let taskId = taskModel?.taskId ?? ""`

## 3. Upload files
Upload the PDF files you want to merge and bind them with the task ID.

`// Upload files
let path = Bundle.main.path(forResource: "test", ofType: "pdf")
    let uploadFileModel = await client.uploadFile(filepath: path ?? "", language: .english, params: [CPDFFileUploadParameterKey.pageOptions.string():["1,2"]], taskId: taskId)
// Upload files
let uploadFileModel2 = await client.uploadFile(filepath: path ?? "",language: .english ,params: [CPDFFileUploadParameterKey.pageOptions.string():["1,2"]], taskId: taskId)`

**Note:**
In the same task, upload multiple files (up to five). If pageOptions is not passed, it will be a multi-file merge.
In the same task, upload multiple files (up to five) and pageOptions, and merge the specified page numbers of multiple files.
The upload interface supports single-file uploads only.

## 4. Merge PDF files
After the file upload is completed, call this interface through the task ID to merge the files.

`// Execute task
let _ = await client.processFiles(taskId: taskId, language: .english)`

## 5. Get task information
Request task status and file-related metadata based on the task ID.

`// Create a client
let client: CPDFClient = CPDFClient(publicKey: public_key, secretKey: secret_key)
Task { @MainActor in
    // Create a task
    // Create an example of a PDF merge task
    let taskModel = await client.createTask(url: CPDFDocumentEditor.MERGE, language: .english)
    // Get a task id
    let taskId = taskModel?.taskId ?? ""
    // Upload files
    let path = Bundle.main.path(forResource: "test", ofType: "pdf")
    let uploadFileModel = await client.uploadFile(filepath: path ?? "", language: .english, params: [CPDFFileUploadParameterKey.pageOptions.string():["1,2"]], taskId: taskId)
    // Upload files
    let uploadFileModel2 = await client.uploadFile(filepath: path ?? "",language: .english ,params: [CPDFFileUploadParameterKey.pageOptions.string():["1,2"]], taskId: taskId)
    // Execute task
    let _ = await client.processFiles(taskId: taskId, language: .english)
    // Query TaskInfo
    let taskInfoModel = await client.getTaskInfo(taskId: taskId, language: .english)
}`