Tutorials

How to Integrate ComPDFKit PDF API Library in PHP

By ComPDFKit | Wed. 24 Jul. 2024
PDF APIPHP

ComPDFKit enables developers to easily build PDF applications by calling PHP API libraries. With the various PHP API tools we provide, developers can quickly integrate various functions into their applications without a lot of research and design, such as viewing, editing, annotating, signing, encrypting, and decrypting PDF files, thereby improving user experience.

 

In this tutorial, we will step by step introduce how to integrate the ComPDFKit PDF API library in PHP, including steps such as registering an account, installing the PHP PDF API library, identity authentication, and sending PDF API library requests.

 

Before you start, you can quickly register a free ComPDFKit API library accountand enjoy the offer of unlimited free processing of 1,000 documents within 30 days.

 

 

Windows   Web   Android   iOS   Mac   Server   React Native   Flutter   Electron
30-day Free

 

The Requirements Before PHP API Library Calling

Programming Environment: PHP Version 7.0 and higher.

Dependencies: Composer.

 

Step 1: Register and Get a License

- Register as a developer for using ComPDFKit API Library, and then you can get a free an account so that you can process 1000 documents unlimitely for free every month.

- Log in your account that you just register. Then go to the console’s API Dashboard, choose Project, and choose API Keys to access the Project ID, Public Key, and Secret Key.

 

 

Step 2: Install the ComPDFKit PHP PDF API Library

Install the ComPDFKit PHP API Library via Composer, and run the following command:

composer require compdfkit/compdfkit-api-php

Alternatively, you can add "compdfkit/compdfkit-api-php": "^1.2.4" to your "composer.json" file and then run it.

composer update

If you are not using a PHP framework with autoloading capabilities, you will need to use the following code for autoloading.

require_once('vendor/autoload.php');

 

Step 3: Authentication Your License

The ComPDFKit API employs JSON Web Tokens for authentication. Simplify to authenticate your project by following:

$client = new CPDFClient('public_key', 'secret_key');

To learn more about this method and its applications, please take a look at the Authentication section in our API reference.

 

Step 4: Make API Requests

Making an API library request is very simple and generally includes four steps: creating a task, uploading a file, executing the task, and downloading the result file. Here, we will introduce you to the details of each step by example of converting PDF to Word in PHP:

 

(1) Create a task

Based on the type of PDF tool you choose, a task ID is automatically generated for you. You can provide a callback notification URL. 

After the task is processed, we will notify you of the task result through the callback interface. You can perform other operations based on the request result, such as viewing the task status, uploading files, starting tasks, downloading result files, etc.

// Create a task
// Create an example of a PDF TO WORD task
$taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD);

Note: Different PDF APIs correspond to different tasks, so please determine the PDF function of the PDF API you need when creating a task.

 

(2) Upload file: Upload the original file and bind the file to the task ID.

The Field parameter is used to pass a JSON string to set the file processing parameters. Each uploaded file will automatically generate a unique filekey.

// Upload files
$file = $client->addFile('test.pdf')->uploadFile($taskInfo['taskId']);

Note: Pay attention to the file format you upload. Although most ComPDFKit PDF APIs are designed for processing PDF files, some conversion PDF APIs can handle different formats, such as HTML to PDF API can handle HTML format.

 

(3) Execute task: After the file is uploaded, call this interface to process the file through the task ID.

// execute Task
$client->executeTask($taskInfo['taskId']);

 

(4) Download result file: Request task status and file-related metadata based on the task ID.

// Query TaskInfo
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);

 

(5) Sample of Making API Request: Convert PDF to Word in PHP

Before building your PHP API request, please provide the Public key and Secret key.

require_once('../vendor/autoload.php');

use ComPDFKit\Client\CPDFClient;
use ComPDFKit\Constant\CPDFConversion;
use ComPDFKit\Exception\CPDFException;

$client = new CPDFClient('public_key', 'secret_key');

try{
    //Create a task
    $taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD);

    // File handling parameter settings
    $file = $client->addFile('test.pdf')
        ->setIsContainAnnot('1')
        ->setIsContainImg('1')
        ->setIsFlowLayout('1');

    //Upload files
    $fileInfo = $file->uploadFile($taskInfo['taskId']);

    //execute Task
    $client->executeTask($taskInfo['taskId']);

    //query TaskInfo
    $taskInfo = $client->getTaskInfo($taskInfo['taskId']);

    while ($taskInfo['taskStatus'] != 'TaskFinish'){
        sleep(5);
        $taskInfo = $client->getTaskInfo($taskInfo['taskId']);
    }

    print_r($taskInfo);die;
}catch (CPDFException $e){
    echo $e->getMessage();
}

Without a doubt, by incorporating a PHP PDF API library such as ComPDFKit, businesses can optimize their PDF-related workflows, enhance productivity, minimize errors and debugging expenses, ensure data security, and deliver an improved user experience for both employees and customers.

 

Final Words

In this tutorial, we have explained in detail how to integrate the ComPDFKit PDF API library in PHP. With this guide, you can easily integrate the ComPDFKit API library in PHP and add various features and services to your project.

 

In addition, you can find more integration guides from the ComPDFKit PDF API library documentation page, such as Java, Swift, Python, etc. to enhance your user experience.

 

ComPDFKit API has a professional R&D team dedicated to producing comprehensive technical documents and guides to help developers.

 

Moreover, if you have any questions or need technical support, please contact us and you will get a timely response.

Windows   Web   Android   iOS   Mac   Server   React Native   Flutter   Electron
30-day Free