Skip to content

Commit b8cf353

Browse files
author
pjy
committed
Initial commit
1 parent 6bece02 commit b8cf353

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2091
-2
lines changed

README.md

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,135 @@
1-
# compdfkit-api-php
2-
ComPDFKit API PHP SDK
1+
## ComPDFKit API in PHP
2+
3+
[ComPDFKit](https://api.compdf.com/api/docs/introduction) API provides a variety of PHP API tools that allow you to create an efficient document processing workflow in a single API call. Try our various APIs for free — no credit card required.
4+
5+
In this guide, we’ll go through how you can use PHP to make HTTP requests with ComPDFKit API.
6+
7+
8+
9+
## Requirements
10+
11+
Programming Environment: PHP Version 7.0 and higher.
12+
13+
Dependencies: Composer.
14+
15+
16+
17+
## Installation
18+
19+
You can install the library via Composer. Run the following command.
20+
``` shell script
21+
composer require compdfkit/compdfkit-api-php
22+
```
23+
Alternatively, you can add "compdfkit/compdfkit-api-php": "^1.2.4" to your ***"composer.json"*** file and then run it.
24+
``` shell script
25+
composer update
26+
```
27+
28+
If you are not using a PHP framework with autoload feature, you need to use the code below to autoload.
29+
```php
30+
require_once('vendor/autoload.php');
31+
```
32+
33+
34+
35+
## Create API Client
36+
37+
You can use your **publicKey** and **secretKey** to complete the authentication. You need to [sign in](https://api.compdf.com/login) your ComPDFKit API account to get your **publicKey** and **secretKey** at the [dashboard](https://api-dashboard.compdf.com/api/keys). If you are new to ComPDFKit, click here to [sign up](https://api.compdf.com/signup) for a free trial.
38+
39+
- Project public Key: You can find the public key in [Management Panel](https://api-dashboard.compdf.com/api/keys).
40+
41+
- Project secret Key: You can find the secret Key in [Management Panel](https://api-dashboard.compdf.com/api/keys).
42+
43+
```php
44+
$client = new CPDFClient('public_key', 'secret_key');
45+
```
46+
47+
48+
49+
## Create Task
50+
51+
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.
52+
53+
```php
54+
// Create a client
55+
$client = new CPDFClient('public_key', 'secret_key');
56+
57+
// Create a task
58+
// Create an example of a PDF TO WORD task
59+
$taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD);
60+
```
61+
62+
63+
64+
## Upload Files
65+
66+
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 filekey. 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.
67+
68+
```php
69+
// Create a client
70+
$client = new CPDFClient('public_key', 'secret_key');
71+
72+
// Create a task
73+
// Create an example of a PDF TO WORD task
74+
$taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD);
75+
76+
// Upload files
77+
$file = $client->addFile('test.pdf')->uploadFile($taskInfo['taskId']);
78+
```
79+
80+
81+
82+
## Execute the task
83+
84+
After the file upload is completed, call this interface with the task ID to process the files.
85+
86+
```php
87+
// Create a client
88+
$client = new CPDFClient('public_key', 'secret_key');
89+
90+
// Create a task
91+
// Create an example of a PDF TO WORD task
92+
$taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD);
93+
94+
// Upload files
95+
$file = $client->addFile('test.pdf')->uploadFile($taskInfo['taskId']);
96+
97+
// execute Task
98+
$client->executeTask($taskInfo['taskId']);
99+
```
100+
101+
102+
103+
## Get Task Info
104+
105+
Request task status and file-related meta data based on the task ID.
106+
107+
```php
108+
// Create a client
109+
$client = new CPDFClient('public_key', 'secret_key');
110+
111+
// Create a task
112+
// Create an example of a PDF TO WORD task
113+
$taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD);
114+
115+
// Upload files
116+
$file = $client->addFile('test.pdf')->uploadFile($taskInfo['taskId']);
117+
118+
// Execute Task
119+
$client->executeTask($taskInfo['taskId']);
120+
121+
// Query TaskInfo
122+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
123+
```
124+
125+
126+
127+
## Samples
128+
129+
See ***"Samples"*** folder in this folder.
130+
131+
132+
133+
## Related Resources
134+
135+
* [ComPDFKit API Documentation](https://api.compdf.com/api/docs/introduction)

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "compdfkit/compdfkit-api-php",
3+
"description": "ComPDFKit API PHP SDK",
4+
"homepage": "https://api.compdf.com/api-reference/overview",
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "compdfkit",
10+
"homepage": "https://api.compdf.com"
11+
}
12+
],
13+
"require": {
14+
"php":">=7.0",
15+
"guzzlehttp/guzzle": ">=6.5",
16+
"ext-json": "*",
17+
"ext-curl": "*"
18+
},
19+
"require-dev" : {
20+
"guzzlehttp/guzzle": ">=6.5"
21+
},
22+
"minimum-stability": "stable",
23+
"autoload": {
24+
"psr-4": {"ComPDFKit\\": "src"}
25+
}
26+
}

samples/AddWatermark.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
require_once('../vendor/autoload.php');
4+
5+
use ComPDFKit\Client\CPDFClient;
6+
use ComPDFKit\Constant\CPDFDocumentEditor;
7+
use ComPDFKit\Exception\CPDFException;
8+
9+
addWatermarkText();
10+
11+
addWatermarkTextImage();
12+
13+
function addWatermarkText(){
14+
$client = new CPDFClient('public_key', 'secret_key');
15+
16+
try{
17+
//Create a task
18+
$taskInfo = $client->createTask(CPDFDocumentEditor::ADD_WATERMARK);
19+
20+
//File handling parameter settings
21+
$file = $client->addFile('test.pdf')
22+
->setTextColor('#59c5bb')
23+
->setType('text')
24+
->setContent('text')
25+
->setScale('1')
26+
->setOpacity('0.5')
27+
->setRotation('0.785')
28+
->setTargetPages('1-2')
29+
->setVertalign('center')
30+
->setHorizalign('left')
31+
->setXOffset('100')
32+
->setYOffset('100')
33+
->setFullScreen('1')
34+
->setHorizontalSpace('10')
35+
->setVerticalSpace('10');
36+
37+
//Upload files
38+
$fileInfo = $file->uploadFile($taskInfo['taskId']);
39+
40+
//execute Task
41+
$client->executeTask($taskInfo['taskId']);
42+
43+
//query TaskInfo
44+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
45+
46+
while ($taskInfo['taskStatus'] != 'TaskFinish') {
47+
sleep(5);
48+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
49+
}
50+
51+
print_r($taskInfo);
52+
}catch (CPDFException $e) {
53+
echo $e->getMessage();
54+
}
55+
}
56+
57+
function addWatermarkTextImage(){
58+
$client = new CPDFClient('public_key', 'secret_key');
59+
60+
try{
61+
//Create a task
62+
$taskInfo = $client->createTask(CPDFDocumentEditor::ADD_WATERMARK);
63+
64+
//File handling parameter settings
65+
$file = $client->addFile('test.pdf')
66+
->setType('image')
67+
//Set Watermark Image
68+
->setImagePath('3.jpg')
69+
->setScale('0.5')
70+
->setOpacity('0.5')
71+
->setRotation('45')
72+
->setTargetPages('1-2')
73+
->setVertalign('center')
74+
->setHorizalign('left')
75+
->setXOffset('50')
76+
->setYOffset('50')
77+
->setFullScreen('1')
78+
->setHorizontalSpace('100')
79+
->setVerticalSpace('100')
80+
->setFront('1');
81+
82+
//Upload files
83+
$fileInfo = $file->uploadFile($taskInfo['taskId']);
84+
85+
//execute Task
86+
$client->executeTask($taskInfo['taskId']);
87+
88+
//query TaskInfo
89+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
90+
91+
while ($taskInfo['taskStatus'] != 'TaskFinish') {
92+
sleep(5);
93+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
94+
}
95+
96+
print_r($taskInfo);
97+
}catch (CPDFException $e) {
98+
echo $e->getMessage();
99+
}
100+
}

samples/CSVToPDF.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
require_once('../vendor/autoload.php');
4+
5+
use ComPDFKit\Client\CPDFClient;
6+
use ComPDFKit\Constant\CPDFConversion;
7+
use ComPDFKit\Exception\CPDFException;
8+
9+
$client = new CPDFClient('public_key', 'secret_key');
10+
try {
11+
//Create a task
12+
$taskInfo = $client->createTask(CPDFConversion::CSV_TO_PDF);
13+
14+
//Upload files
15+
$fileInfo = $client->addFile('test.csv')->uploadFile($taskInfo['taskId']);
16+
17+
//execute Task
18+
$client->executeTask($taskInfo['taskId']);
19+
20+
//query TaskInfo
21+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
22+
23+
while ($taskInfo['taskStatus'] != 'TaskFinish') {
24+
sleep(5);
25+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
26+
}
27+
28+
print_r($taskInfo);
29+
} catch (CPDFException $e) {
30+
echo $e->getMessage();
31+
}

samples/DeleteWatermark.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
4+
require_once('../vendor/autoload.php');
5+
6+
use ComPDFKit\Client\CPDFClient;
7+
use ComPDFKit\Constant\CPDFDocumentEditor;
8+
use ComPDFKit\Exception\CPDFException;
9+
10+
$client = new CPDFClient('public_key', 'secret_key');
11+
try {
12+
//Create a task
13+
$taskInfo = $client->createTask(CPDFDocumentEditor::DEL_WATERMARK);
14+
15+
//Upload files
16+
$fileInfo = $client->addFile('test.pdf')->uploadFile($taskInfo['taskId']);
17+
18+
//execute Task
19+
$client->executeTask($taskInfo['taskId']);
20+
21+
//query TaskInfo
22+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
23+
24+
while ($taskInfo['taskStatus'] != 'TaskFinish') {
25+
sleep(5);
26+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
27+
}
28+
29+
print_r($taskInfo);
30+
} catch (CPDFException $e) {
31+
echo $e->getMessage();
32+
}

samples/ExcelToPDF.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
require_once('../vendor/autoload.php');
4+
5+
use ComPDFKit\Client\CPDFClient;
6+
use ComPDFKit\Constant\CPDFConversion;
7+
use ComPDFKit\Exception\CPDFException;
8+
9+
$client = new CPDFClient('public_key', 'secret_key');
10+
try {
11+
//Create a task
12+
$taskInfo = $client->createTask(CPDFConversion::XLSX_TO_PDF);
13+
14+
//Upload files
15+
$fileInfo = $client->addFile('test.xlsx')->uploadFile($taskInfo['taskId']);
16+
17+
//execute Task
18+
$client->executeTask($taskInfo['taskId']);
19+
20+
//query TaskInfo
21+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
22+
23+
24+
while ($taskInfo['taskStatus'] != 'TaskFinish') {
25+
sleep(5);
26+
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
27+
}
28+
29+
print_r($taskInfo);
30+
} catch (CPDFException $e) {
31+
echo $e->getMessage();
32+
}

0 commit comments

Comments
 (0)