Skip to content

ComPDFKit/compdfkit-api-swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

ComPDF API for Swift

As part of the KDAN ecosystem, ComPDF offers powerful and reliable PDF libraries and comprehensive PDF capabilities.

If you find this library helpful, please consider giving us a ⭐ Star on GitHub! Have feedback or questions? Join the conversation in our Discussions.

Why ComPDF API?

  • Easy API Call — Complete complex PDF processing workflows with minimal code. Also supports integration with various low-code platforms, agents, MCP tools, and enterprise workflows such as Salesforce, SharePoint, Make, Zapier, ClawHub Skills, MCP.SO, and MCPMarket.

  • Comprehensive Feature Support:

    • PDF generation

    • PDF/image format conversion:Convert PDF/images to Word, Excel, PPT, HTML, RTF, images, CSV, TXT, JSON, OFD, Markdown, and searchable PDF.Convert Word, Excel, PPT, HTML, RTF, images, CSV, TXT, JSON, and Markdown to PDFs.

    • Page editing: merge, split, extract, insert, delete, and rotate PDF pages

    • Data parsing and extraction

    • Watermarking

    • PDF Compression

    • PDF comparison

  • Secure & Reliable — Enterprise-grade infrastructure with data privacy compliance.

  • Free Tier — Process up to 200+ API calls per month at no cost.

Table of Contents

Requirements

Programming Environment: macOS 10.13 and higher, and iOS 10 and higher.

Dependencies: Xcode13 and higher.

Installation

Add the following dependency to your "Podfile":

pod 'compdfkit-api-swift'

Usage

Create API Client

You can use your publicKey and secretKey to complete the authentication. You need to sign in your ComPDF API account to get your publicKey and secretKey at the dashboard. If you are new to ComPDF, click here to sign up for a free trial (200+ API calls per month for free).

  • Project public Key: You can find the public key in the API Keys section of your ComPDF API account.

  • Project secret Key: You can find the secret key in the API Keys section of your ComPDF API account.

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

Create A Task

A task ID is automatically generated for you based on the type of PDF tool you choose. You can provide the callback notification URL. After the task processing is completed, we will notify you of the task result through the callback interface. You can perform other operations according to the request result, such as checking the status of the task, uploading files, starting the task, or downloading the result file. There are two ways to create a task in Swift below.

Async Block:

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

// Create a task
client.createTask(url: CPDFConversion.PDF_TO_WORD, language: .english) { taskModel in
    guard let taskId = taskModel?.taskId else {
        Swift.debugPrint(taskModel?.errorDesc ?? "")
        return
    }
}

Async Await:

When creating a task with the method async await, the programming environment should be macOS 10.15 and higher, and iOS 13 and higher.

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

Task { @MainActor in
    // Create a task
    let taskModel = await client.createTask(url: CPDFConversion.PDF_TO_WORD, language: .english)
    let taskId = taskModel?.taskId ?? ""
}

Upload Files

Upload the original file and bind the file to the task ID. The field parameter is used to pass the JSON string to set the processing parameters for the file. Each file will generate automatically a unique file key. Please note that a maximum of five files can be uploaded for a task ID and no files can be uploaded for that task after it has started. There are two ways to upload files in Swift below.

Async Block:

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

// Create a task
client.createTask(url: CPDFConversion.PDF_TO_WORD, language: .english) { taskModel in
    guard let taskId = taskModel?.taskId else {
        Swift.debugPrint(taskModel?.errorDesc ?? "")
        return
    }

    // Upload file
    let group = DispatchGroup()
    group.enter()
    let path = Bundle.main.path(forResource: "test", ofType: "pdf")
    client.uploadFile(filepath: path!, password: "", language: .english, params: [
        CPDFFileUploadParameterKey.isContainAnnot.string() : "1",
        CPDFFileUploadParameterKey.isContainImg.string() : "1",
        CPDFFileUploadParameterKey.isFlowLayout.string() : "1"
    ], taskId: taskId) { uploadFileModel in
        if let errorInfo = uploadFileModel?.errorDesc {
            Swift.debugPrint(errorInfo)
        }
        group.leave()
    }
}

Async Await:

When uploading files with the method async await, the programming environment should be macOS 10.15 and higher, and iOS 13 and higher.

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

Task { @MainActor in
    // Create a task
    let taskModel = await client.createTask(url: CPDFConversion.PDF_TO_WORD, language: .english)
    let taskId = taskModel?.taskId ?? ""

    // Upload file
    let path = Bundle.main.path(forResource: "test", ofType: "pdf")
    let uploadFileModel = await client.uploadFile(filepath: path ?? "", password: "", language: .english, params: [
        CPDFFileUploadParameterKey.isContainAnnot.string() : "1",
        CPDFFileUploadParameterKey.isContainImg.string() : "1",
        CPDFFileUploadParameterKey.isFlowLayout.string() : "1"
    ], taskId: taskId)
}

Execute the Task

After completing the file upload, call this interface with the task ID to process the files. There are two ways to execute the task in Swift below.

Async Block:

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

// Create a task
client.createTask(url: CPDFConversion.PDF_TO_WORD, language: .english) { taskModel in
    guard let taskId = taskModel?.taskId else {
        Swift.debugPrint(taskModel?.errorDesc ?? "")
        return
    }

    // Upload file
    let group = DispatchGroup()
    group.enter()
    let path = Bundle.main.path(forResource: "test", ofType: "pdf")
    client.uploadFile(filepath: path!, password: "", language: .english, params: [
        CPDFFileUploadParameterKey.isContainAnnot.string() : "1",
        CPDFFileUploadParameterKey.isContainImg.string() : "1",
        CPDFFileUploadParameterKey.isFlowLayout.string() : "1"
    ], taskId: taskId) { uploadFileModel in
        if let errorInfo = uploadFileModel?.errorDesc {
            Swift.debugPrint(errorInfo)
        }
        group.leave()
    }

    group.notify(queue: .main) {
        // Execute task
        client.processFiles(taskId: taskId, language: .english) { processFileModel in
            if let errorInfo = processFileModel?.errorDesc {
                Swift.debugPrint(errorInfo)
            }
        }
    }
}

Async Await:

When executing the task with the method async await, the programming environment should be macOS 10.15 and higher, and iOS 13 and higher.

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

Task { @MainActor in
    // Create a task
    let taskModel = await client.createTask(url: CPDFConversion.PDF_TO_WORD, language: .english)
    let taskId = taskModel?.taskId ?? ""

    // Upload file
    let path = Bundle.main.path(forResource: "test", ofType: "pdf")
    let uploadFileModel = await client.uploadFile(filepath: path ?? "", password: "", language: .english, params: [
        CPDFFileUploadParameterKey.isContainAnnot.string() : "1",
        CPDFFileUploadParameterKey.isContainImg.string() : "1",
        CPDFFileUploadParameterKey.isFlowLayout.string() : "1"
    ], taskId: taskId)

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

Get the Task Info

Request task status and file-related metadata based on the task ID. There are two ways to get the task information in Swift below.

Async Block:

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

// Create a task
client.createTask(url: CPDFConversion.PDF_TO_WORD, language: .english) { taskModel in
    guard let taskId = taskModel?.taskId else {
        Swift.debugPrint(taskModel?.errorDesc ?? "")
        return
    }

    // Upload file
    let group = DispatchGroup()
    group.enter()
    let path = Bundle.main.path(forResource: "test", ofType: "pdf")
    client.uploadFile(filepath: path!, password: "", language: .english, params: [
        CPDFFileUploadParameterKey.isContainAnnot.string() : "1",
        CPDFFileUploadParameterKey.isContainImg.string() : "1",
        CPDFFileUploadParameterKey.isFlowLayout.string() : "1"
    ], taskId: taskId) { uploadFileModel in
        if let errorInfo = uploadFileModel?.errorDesc {
            Swift.debugPrint(errorInfo)
        }
        group.leave()
    }

    group.notify(queue: .main) {
        // Execute task
        client.processFiles(taskId: taskId, language: .english) { processFileModel in
            if let errorInfo = processFileModel?.errorDesc {
                Swift.debugPrint(errorInfo)
            }
            // Get task processing information
            client.getTaskInfo(taskId: taskId, language: .english) { taskInfoModel in
                guard let _model = taskInfoModel else {
                    Swift.debugPrint("error:....")
                    return
                }
                if (_model.isFinish()) {
                    _model.printInfo()
                } else if (_model.isRuning()) {
                    Swift.debugPrint("Task incomplete processing")
                } else {
                    Swift.debugPrint("error: \(_model.errorDesc ?? "")")
                }
            }
        }
    }
}

Async Await:

When getting the task information with the method Async Await, the programming environment should be macOS 10.15 and higher, and iOS 13 and higher.

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

Task { @MainActor in
    // Create a task
    let taskModel = await client.createTask(url: CPDFConversion.PDF_TO_WORD, language: .english)
    let taskId = taskModel?.taskId ?? ""

    // Upload file
    let path = Bundle.main.path(forResource: "test", ofType: "pdf")
    let uploadFileModel = await client.uploadFile(filepath: path ?? "", password: "", language: .english, params: [
        CPDFFileUploadParameterKey.isContainAnnot.string() : "1",
        CPDFFileUploadParameterKey.isContainImg.string() : "1",
        CPDFFileUploadParameterKey.isFlowLayout.string() : "1"
    ], taskId: taskId)

    // Execute task
    let _ = await client.processFiles(taskId: taskId, language: .english)
    // Get task processing information
    let taskInfoModel = await client.getTaskInfo(taskId: taskId, language: .english)
    guard let _model = taskInfoModel else {
        Swift.debugPrint("error:....")
        return
    }
    if (_model.isFinish()) {
        _model.printInfo()
    } else if (_model.isRuning()) {
        Swift.debugPrint("Task incomplete processing")
        client.getTaskInfoComplete(taskId: taskId) { isFinish, params in
            Swift.debugPrint(params)
        }
    } else {
        Swift.debugPrint("error: \(taskInfoModel?.errorDesc ?? "")")
    }
}

Samples

See "Samples" folder in this folder.

Changelog

Go to our Changelog to keep up with the latest updates, improvements, and bug fixes for ComPDF API.

Support

ComPDF has a professional R&D team that produces comprehensive technical documentation and guides to help developers. Also, you can get an immediate response when reporting your problems to our support team.

  • For detailed information, please visit our guides page.
  • For technical assistance, please reach out to our technical support.
  • To get more details and an accurate quote, please contact our sales team.

Free Trial and License

The code is available as open source under the terms of the Apache-2.0 License.

ComPDF API is a powerful API that can be used to create an efficient document processing workflow in a single API call.

If you do not have a ComPDF API account, you can sign up for a free trial to process 200+ API calls per month for free.

Once you have a ComPDF API account, you can obtain your publicKey and secretKey in the dashboard.

Packages

 
 
 

Contributors