DWS Processor API with PHP
With Nutrient DWS Processor API, you can process your documents by making HTTP requests to one of our 50+ API tools(opens in a new tab). 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).
This guide explains how to use PHP to make HTTP requests with Nutrient DWS Processor API. Keep in mind that you can use any HTTP client you want; this example uses curl to demonstrate the principles of interacting with Nutrient DWS Processor API.
It involves three main steps:
- Installing the required dependencies
- Preparing the payload
- Making the request
Installing the required dependencies
You’ll need to add the 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 Nutrient DWS Processor API.
Preparing the payload
First, 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, Nutrient DWS Processor API will return an error.
In this example, you’ll be adding an image watermark. For examples of how the watermark API can be used (for example, adding multiple watermarks to the document, watermarking the last page of the document, and watermark alignments), refer to our PDF watermark API page.
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.nutrient.io/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 Nutrient DWS Processor 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(opens in a new tab).
Complete code
For your convenience, the complete code is below:
<?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.nutrient.io/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);