The pdfRest Convert to PDF/A API Tool allows users to convert documents to the PDF/A format, which is an ISO-standardized version of the PDF specialized for the digital preservation of electronic documents. This API tool is particularly useful when you need to ensure that documents remain readable and unchanged over long periods, which is essential for archiving purposes.
For example, government agencies, libraries, or businesses that need to store documents for several years would benefit from converting their files to PDF/A to ensure compatibility and reliability in the future.
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' => 'xxxxxxxx-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' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'. 'headers' => [ 'Content-Type' => '' // Set the Content-Type header for the file. ] ], [ 'name' => 'output_type', // Specify the field name for the output type. 'contents' => 'PDF/A-2b' // Set the value for the output type (in this case, 'PDF/A-2b'). ], [ 'name' => 'output', // Specify the field name for the output option. 'contents' => 'pdfrest_pdfa' // Set the value for the output option (in this case, 'pdfrest_pdfa'). ] ] ]; $request = new Request('POST', 'https://api.pdfrest.com/pdfa', $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 processed PDF/A content.
Source of the provided code: GitHub
The code provided is a PHP script that uses the Guzzle HTTP client to send an API request to convert a document to PDF/A format using the pdfRest API.
require 'vendor/autoload.php';
- This line includes the Composer autoload file to load the necessary classes for the Guzzle HTTP client.use GuzzleHttp\Client;
- This imports the Client class from the GuzzleHttp namespace for making HTTP requests.use GuzzleHttp\Psr7\Request;
- This imports the Request class which represents an HTTP request.use GuzzleHttp\Psr7\Utils;
- This imports utility methods for working with streams.$client = new Client();
- This creates a new instance of the Guzzle HTTP client.$headers
array includes the 'Api-Key' needed for authentication with the pdfRest API.$options
array is configured for a multipart request, which includes the file to be converted, the desired output type ('PDF/A-2b'), and the output option.new Request('POST', 'https://api.pdfrest.com/pdfa', $headers);
- This creates a new HTTP POST request with the specified URL and headers.$res = $client->sendAsync($request, $options)->wait();
- This sends the request asynchronously and waits for the response.echo $res->getBody();
- This outputs the response body, which should contain the processed PDF/A file content.In this tutorial, we've learned how to use PHP to call the pdfRest Convert to PDF/A API to convert documents to the PDF/A format. By following the steps outlined, you can integrate this functionality into your PHP applications to ensure long-term preservation of your documents.
For further exploration and to demo all of the pdfRest API Tools, you can visit the API Lab. Also find more information in the API Reference documentation.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.