How to Add Images to PDF with cURL

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

Why Add Images to PDF with cURL?

The pdfRest Add to PDF API Tool is a powerful resource for developers looking to automate the process of adding images to PDF documents. This tool is part of the pdfRest suite of APIs, which provide a range of functionalities for PDF manipulation and conversion. In this tutorial, we will demonstrate how to use cURL, a command-line tool for making HTTP requests, to call the Add to PDF API endpoint. cURL is widely supported and can be used on almost any platform, making it a versatile choice for interacting with web APIs like pdfRest.

Imagine you're developing an application that generates reports and you need to add your company logo or relevant images to the PDFs before they are sent to clients. The Add to PDF API allows you to do just that programmatically, ensuring that each document is branded consistently without manual intervention. This can save time and reduce errors, especially when dealing with a large volume of documents.

Add Images to PDF with cURL Code Example

curl -X POST "https://api.pdfrest.com/pdf-with-added-image" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
  -F "file=@/path/to/file" \
  -F "image_file=@/path/to/file" \
  -F "output=example_out" \
  -F "x=10" \
  -F "y=10" \
  -F "page=1"

Reference: pdfRest API cURL Samples

Breaking Down the Code

The code provided is a cURL command that makes a POST request to the pdfRest Add to PDF API endpoint. Let's break down the key components of this request:

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

This line specifies the HTTP method (POST) and the API endpoint URL.

-H "Accept: application/json"
-H "Content-Type: multipart/form-data"
-H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

The -H flag adds HTTP headers to the request. The "Accept" header tells the server the client expects JSON in response. The "Content-Type" header indicates that the request body will be sent as multipart/form-data, which is necessary for uploading files. The "Api-Key" header is where you would include your unique API key provided by pdfRest.

-F "file=@/path/to/file"
-F "image_file=@/path/to/file"
-F "output=example_out"
-F "x=10"
-F "y=10"
-F "page=1"

The -F flag is used to specify each part of the multipart request. The "file" is the path to the PDF file you want to modify, and "image_file" is the path to the image you want to add. The "output" field specifies the base name for the output file. The "x" and "y" fields set the coordinates where the image will be placed on the PDF page (measured in PDF Units, where 1 PDF Unit = 1/72"). The "page" field specifies the page number where the image should be added.

Beyond the Tutorial

By following the steps in this tutorial, you've learned how to make an API call to pdfRest's Add to PDF endpoint using cURL. This capability enables you to automate the process of adding images to PDF documents, which can be integrated into various workflows and applications.

For further exploration and to demo all of the pdfRest API Tools, visit the API Lab. You can also find more detailed information in the API Reference Guide.

Note: This is an example of a multipart API call. For code samples using JSON payloads, you can visit pdfRest API cURL Samples with JSON Payload.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us