Getting Started with PHP

PSPDFKit API is offered as a simple HTTP API, so using it from PHP is as easy as making an HTTP request. The following will guide you through how to install the required dependencies, and it’ll show you how to make a request.

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.

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
<?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:

PHP
$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 tools.

Full Code

For your convenience, here’s the whole code. Just copy it and run it:

PHP
<?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);