Getting Started
Overview Supported Languages File Types Test Mode Postman Collection Tools and APIsPricing
Pricing Calculate Credit Usage Pricing per ToolDeveloper Guides
API Overview Authentication Errors Combine Workflows Performance PDF Generation API ReferenceSupported Languages
Java C# JavaScript Python PHP Other Languages Deployment Options Security Privacy Support About PSPDFKitPHP PDF API — Getting Started
With PSPDFKit’s PHP PDF API, you can quickly process your documents by making HTTP requests to one of our 50+ API tools. You can make a single request to one tool or combine API actions to generate, edit, OCR, and convert your document (1 document can have multiple API actions).
In this guide, we’ll go through how you can use PHP to make HTTP requests with our API by:
-
1
-
2
-
3
This guide will use curl
. Keep in mind that you can use any HTTP client you want; this example uses curl
to demonstrate the principles of interacting with PSPDFKit API.
Installing the Required Dependencies
You’ll need to add document.pdf
and logo.png
files to the root of your PHP project (the same folder you’ll be creating the pspdfkit.php
file in). You can use the sample files provided by us — document.pdf and logo.png — or use your own.
Now that you have your assets set up, you’re ready to start making requests to PSPDFKit API.
Preparing the Payload
First, you have to define your instructions
object:
<?php
$instructions = '{
"parts": [
{
"file": "document",
"pages": {
"end": -2
}
},
{
"file": "document",
"pages": {
"start": -1
},
"actions": [
{
"type": "watermark",
"image": "company-logo",
"width": "50%"
}
]
}
]
}';
You define your instructions
as a JSON-formatted string. Keep in mind that if this isn’t valid JSON, PSPDFKit API will return an error.
In this example, you’ll be adding an image watermark. For more details on the available options specifically related to watermarking, refer to our watermarking guide.
Making the Request
Now, you’re ready to make a curl
call. Make sure to replace the your_api_key_here
placeholder with your actual API key if it hasn’t yet been replaced:
$FileHandle = fopen('result.pdf', 'w+');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.pspdfkit.com/build',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_POSTFIELDS => array(
'instructions' => $instructions,
'document' => new CURLFILE('document.pdf'),
'company-logo' => new CURLFILE('logo.png')
),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer your_api_key_here'
),
CURLOPT_FILE => $FileHandle,
));
$response = curl_exec($curl);
curl_close($curl);
fclose($FileHandle);
Here, you set up your curl
object and configure it to make a request to PSPDFKit API. The CURLOPT_POSTFIELDS
array contains all the parts you want to send. In this case, this means your instructions
and the two required assets (the PDF and the watermark image).
You also set up your output using CURLOPT_FILE
, which will save the resulting document as result.pdf
in the same folder as your PHP file.
And with that, you're ready to make a request. Run your script by executing php pspdfkit.php
, and the result.pdf
should appear in the same folder.
While this example made use of our watermarking API, this same approach can be used for all our available API tools.
Full Code
For your convenience, here’s the whole code. Just copy it and run it:
<?php
$FileHandle = fopen('result.pdf', 'w+');
$curl = curl_init();
$instructions = '{
"parts": [
{
"file": "document",
"pages": {
"end": -2
}
},
{
"file": "document",
"pages": {
"start": -1
},
"actions": [
{
"type": "watermark",
"image": "company-logo",
"width": "50%"
}
]
}
]
}';
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.pspdfkit.com/build',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_POSTFIELDS => array(
'instructions' => $instructions,
'document' => new CURLFILE('document.pdf'),
'company-logo' => new CURLFILE('logo.png')
),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer your_api_key_here'
),
CURLOPT_FILE => $FileHandle,
));
$response = curl_exec($curl);
curl_close($curl);
fclose($FileHandle);