How to Digitally Sign PDF Files with PHP
Why Digitally Sign PDFs with PHP?
The pdfRest Sign PDF API Tool is a powerful solution for digitally signing PDF documents programmatically. This tutorial will guide you on how to use PHP to send an API call to the Sign PDF endpoint, enabling you to automate the process of adding digital signatures to your PDFs. By leveraging the pdfRest API, you can integrate PDF signing capabilities into your PHP applications, ensuring that your documents are authenticated and secure.
A business might need to send out contracts or agreements that require digital signatures. Using the Sign PDF API, a company can automate the process of signing these documents, saving time and reducing the risk of human error. For example, a legal firm could use this API to automatically sign and send legal documents to clients, ensuring that all signatures are verified and legally binding.
Sign PDF with PHP Code Example
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client. use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace. use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class. use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams. $client = new Client(); // Create a new instance of the Guzzle HTTP client. $headers = [ 'Api-Key' =--> 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication. ]; $options = [ 'multipart' => [ [ 'name' => 'file', // Specify the field name for the file. 'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading. 'filename' => 'filename.pdf', // Set the filename for the file to be processed, in this case, '/path/to/file'. 'headers' => [ 'Content-Type' => 'application/pdf' // Set the Content-Type header for the file. ] ], [ 'name' => 'pfx_credential_file', // Specify the field name for the file. 'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading. 'filename' => 'filename.pfx', // Set the filename for the file to be processed, in this case, '/path/to/file'. 'headers' => [ 'Content-Type' => 'application/x-pkcs12' // Set the Content-Type header for the file. ] ], [ 'name' => 'pfx_passphrase_file', // Specify the field name for the file. 'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading. 'filename' => 'filename.txt', // Set the filename for the file to be processed, in this case, '/path/to/file'. 'headers' => [ 'Content-Type' => 'text/plain' // Set the Content-Type header for the file. ] ], [ 'name' => 'logo_file', // Specify the field name for the file. 'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading. 'filename' => 'filename.png', // Set the filename for the file to be processed, in this case, '/path/to/file'. 'headers' => [ 'Content-Type' => 'image/png' // Set the Content-Type header for the file. ] ], [ 'name' => 'signature_configuration', // Specify the field name for the digital signature config. 'contents' => '{"type":"new","name":"esignature","logo_opacity":"0.25","location":{"bottom_left":{ "x":"0", "y":"0" },"top_right":{ "x":"216", "y":"72" },"page":1},"display":{"include_distinguished_name":"true","include_datetime":"true","contact":"My contact info","location":"My location","name":"John Doe","reason":"My reason for signing"}}' // Set the value for the signature configuration. This is a JSON-formatted string. ], [ 'name' => 'output', // Specify the field name for the output option. 'contents' => 'pdfrest_signed_pdf' // Set the value for the output option (in this case, 'pdfrest_signed_pdf'). ] ] ]; $request = new Request('POST', 'https://api.pdfrest.com/signed-pdf', $headers); // Create a new HTTP POST request with the API endpoint and headers. $res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response. echo $res->getBody(); // Output the response body, which contains the signed PDF.
Source: GitHub
Breaking Down the Code
The code begins by requiring the autoload file to load the Guzzle HTTP client, which is necessary for making HTTP requests in PHP. The Client
, Request
, and Utils
classes from the Guzzle library are imported to facilitate the HTTP request process.
$client = new Client();
This line creates a new instance of the Guzzle HTTP client, which will be used to send the API request.
$headers = [ 'Api-Key' => 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The $headers
array sets the API key required for authentication with the pdfRest API. Replace the placeholder with your actual API key.
$options = [ 'multipart' => [ // File fields and configurations ] ];
The $options
array defines the multipart form data that will be sent with the request. Each element in the multipart
array represents a different part of the form data, including the PDF file, PFX credential file, passphrase file, logo file, and signature configuration.
$request = new Request('POST', 'https://api.pdfrest.com/signed-pdf', $headers);
This line creates a new HTTP POST request to the Sign PDF endpoint, including the headers for authentication.
$res = $client->sendAsync($request, $options)->wait();
The asynchronous request is sent using the sendAsync
method, and the script waits for the response. The response contains the signed PDF document.
echo $res->getBody();
Finally, the response body, which contains the signed PDF, is output to the console or browser.
Beyond the Tutorial
In this tutorial, you have learned how to use PHP to send an API call to the pdfRest Sign PDF endpoint, automating the process of digitally signing PDF documents. This powerful capability can be integrated into your PHP applications to streamline document workflows and ensure document authenticity.
To explore more features and capabilities of the pdfRest API, visit the API Lab to demo all available tools. For detailed information on each endpoint and parameter, refer to the API Reference Guide.
Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit the GitHub repository.