How to Translate PDF Text with PHP
Why Translate PDF Text with PHP?
The pdfRest Translate PDF API Tool provides a seamless way to convert the text within a PDF document into a specified language using an API call. This tutorial will guide you through the process of sending an API request to the Translate PDF endpoint using PHP, allowing you to leverage this powerful tool in your applications.
Imagine a scenario where a multinational company receives PDF documents in various languages. By using the Translate PDF API, they can automatically translate these documents into a common language, such as English, for easier processing and understanding by their global team. This can significantly enhance workflow efficiency and communication across different regions.
Translate PDF Text with PHP Code Example
require 'vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Utils; // By default, we use the US-based API service. This is the primary endpoint for global use. $apiUrl = "https://api.pdfrest.com"; /* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below. * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work */ //$apiUrl = "https://eu-api.pdfrest.com"; $client = new Client(); $headers = [ 'Api-Key' =--> 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ]; $options = [ 'multipart' => [ [ 'name' => 'file', 'contents' => Utils::tryFopen('/path/to/file', 'r'), 'filename' => 'filename.pdf', 'headers' => [ 'Content-Type' => 'application/pdf' ] ], [ // Translates text to American English. Format the output_language as a 2-3 character ISO 639 code, optionally with a region/script (e.g., 'en', 'es', 'zh-Hant', 'eng-US'). 'name' => 'output_language', 'contents' => 'en-US' ] ] ]; $request = new Request('POST', $apiUrl.'/translated-pdf-text', $headers); $res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
Source: GitHub
Breaking Down the Code
The code begins by including the necessary libraries using Composer's autoload feature:
require 'vendor/autoload.php';
Next, it initializes the Guzzle HTTP client, which is used to make HTTP requests:
use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Utils;
By default, the API URL is set to the US-based endpoint:
$apiUrl = "https://api.pdfrest.com";
For European users, there's an option to switch to the EU-based service for GDPR compliance by uncommenting the alternative URL.
To authenticate the API request, an API key is included in the headers:
$headers = [ 'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The API request is configured to send a multipart form-data payload. The 'file' part includes the PDF file to be translated:
'contents' => Utils::tryFopen('/path/to/file', 'r'), 'filename' => 'filename.pdf', 'headers' => [ 'Content-Type' => 'application/pdf' ]
The 'output_language' specifies the desired language for translation. It uses ISO 639 codes:
'name' => 'output_language', 'contents' => 'en-US'
The request is then created and sent asynchronously, waiting for the response:
$request = new Request('POST', $apiUrl.'/translated-pdf-text', $headers); $res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
Beyond the Tutorial
In this tutorial, you learned how to make an API call to the pdfRest Translate PDF endpoint using PHP. This allows you to translate PDF text into different languages programmatically.
To explore more functionalities, you can demo all of the pdfRest API Tools in the API Lab. For detailed information, consult the API Reference Guide.
Note: This example demonstrates a multipart API call. You can find code samples using JSON payloads here.