How to Translate PDF Text with cURL
Why Translate PDF Text with cURL?
The pdfRest Translate PDF API Tool is a powerful resource for converting the text within PDF files into different languages. This tutorial will guide you through the process of sending an API call to the Translate PDF endpoint using cURL, a command-line tool for transferring data with URLs. By leveraging this API, you can automate the translation of PDF documents, making it easier to handle multilingual content and streamline workflows.
Imagine you are working for a global company that needs to distribute technical manuals or marketing materials in various languages. Instead of manually translating each document, you can use the Translate PDF API to quickly convert the text within your PDFs to the desired language, saving time and reducing the potential for human error.
Translate PDF Text with cURL Code Example
#!/bin/sh # By default, we use the US-based API service. This is the primary endpoint for global use. API_URL="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 # API_URL="https://eu-api.pdfrest.com" curl -X POST "$API_URL/translated-pdf-text" \ -H "Accept: application/json" \ -H "Content-Type: multipart/form-data" \ -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \ -F "file=@/path/to/file" \ -F "output_language=en-US" # 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').
Source: GitHub Repository
Breaking Down the Code
The script begins by setting the API_URL
variable to the default US-based API service endpoint:
API_URL="https://api.pdfrest.com"
This is the primary endpoint for global use. For European users who require GDPR compliance, you can switch to the EU-based service by uncommenting the alternative URL:
# API_URL="https://eu-api.pdfrest.com"
The curl
command is used to make a POST request to the /translated-pdf-text
endpoint:
curl -X POST "$API_URL/translated-pdf-text"
Several headers are included to specify the request format and authentication:
-H "Accept: application/json" \ -H "Content-Type: multipart/form-data" \ -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
The Accept
header indicates that the response should be in JSON format. The Content-Type
header specifies that the request body will be sent as multipart/form-data
. The Api-Key
header is used for authentication; replace xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
with your actual API key.
The -F
flag is used to specify form data. The file
parameter uploads the PDF file to be translated:
-F "file=@/path/to/file"
Replace /path/to/file
with the actual path to your PDF file. The output_language
parameter specifies the target language for translation:
-F "output_language=en-US"
Here, the text is translated to American English. The output_language
should be formatted as a 2-3 character ISO 639 code, optionally with a region/script (e.g., 'en', 'es', 'zh-Hant', 'eng-US').
Beyond the Tutorial
In this tutorial, you learned how to use cURL to make an API call to the pdfRest Translate PDF endpoint, allowing you to translate the text within a PDF document into another language. This can be a valuable tool for handling multilingual content efficiently.
To explore further, try out all the pdfRest API Tools in the API Lab. For more detailed information, refer to the API Reference Guide. Note that this is an example of a multipart API call, and code samples using JSON payloads can be found here.