How to Add Text to PDF Files with cURL

Learn how to add text to a PDF using pdfRest Add to PDF API Tool with cURL
Share this page

Why Add Text to PDF Files with cURL?

The pdfRest Add to PDF API Tool is a powerful resource for developers who need to programmatically add text to PDF documents. This tutorial will guide you through the process of sending an API call to the Add to PDF endpoint using cURL, a command line tool for transferring data with URLs. By learning how to use cURL with pdfRest, you can automate the process of modifying PDFs, making it efficient and scalable for various applications.

Imagine you are working for a company that needs to generate personalized reports for hundreds of clients. Each report is a PDF document that requires the client's name and unique data to be added to specific locations within the document. Using the Add to PDF API with cURL, you can automate this process, ensuring accuracy and saving time by eliminating the need for manual editing.

Add Text to PDF Files with cURL Code Example

TEXT_OPTIONS='[{"font":"Times New Roman","max_width":"175","opacity":"1","page":"1","rotation":"0","text":"sample text in PDF","text_color_rgb":"0,0,0","text_size":"30","x":"72","y":"144"}]'

curl -X POST "https://api.pdfrest.com/pdf-with-added-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 "text_objects=$TEXT_OPTIONS" \
  -F "output=example_out"

Source: GitHub Repository

Breaking Down the Code

Let's break down the provided cURL command to understand how it works:

TEXT_OPTIONS='[{"font":"Times New Roman","max_width":"175","opacity":"1","page":"1","rotation":"0","text":"sample text in PDF","text_color_rgb":"0,0,0","text_size":"30","x":"72","y":"144"}]'

This line defines a shell variable TEXT_OPTIONS containing a JSON string. This JSON specifies the text properties to be added to the PDF, such as font, size, color, and position.

curl -X POST "https://api.pdfrest.com/pdf-with-added-text"

This line initiates a POST request to the pdfRest API endpoint for adding text to PDFs.

-H "Accept: application/json"

This header specifies that the client expects a JSON response from the server.

-H "Content-Type: multipart/form-data"

This header indicates that the request body will be sent as multipart/form-data, which is necessary for file uploads.

-H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

This header includes the API key, which is required for authentication. You must replace the placeholder with your actual API key.

-F "file=@/path/to/file"

This option specifies the file to upload. Replace /path/to/file with the path to your PDF file.

-F "text_objects=$TEXT_OPTIONS"

This option sends the text options defined earlier as part of the form data.

-F "output=example_out"

This option specifies the output file name for the processed PDF.

Beyond the Tutorial

In this tutorial, you learned how to make a multipart API call to add text to a PDF using the pdfRest Add to PDF API Tool with cURL. This process allows for efficient and automated PDF modifications. To explore more features and tools, try out all the pdfRest API Tools in the API Lab. For detailed information on each API, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub JSON Payload Examples.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.