How to Add Vector Lines and Rectangles to PDF Files with cURL

Learn how to work with the pdfRest Add to PDF API Tool to add vector shapes to PDF documents using cURL.
Share this page

Why Add Vector Lines and Rectangles to PDF Files with cURL?

The pdfRest Add to PDF API Tool is a powerful resource for developers looking to enhance PDFs by programmatically adding shapes such as lines, rectangles, and more. This tutorial will guide you through the process of making an API call to the Add to PDF endpoint using cURL, a command-line tool for transferring data with URLs. With this tool, you can automate the customization of PDF documents, making it easier to integrate PDF editing capabilities into your applications.

In real-world scenarios, a user might use the Add to PDF tool to insert graphical elements into a PDF for various purposes. For example, a company might want to automatically add branded headers or footers to documents before distribution, or a teacher could add annotations or highlight sections of a PDF for educational purposes. By using cURL, these tasks can be scripted and automated, saving time and ensuring consistency.

Add Vector Lines and Rectangles to PDF Files 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"

# This example adds a lightly shaded review panel and a divider line to page one.
# Coordinates are measured from the lower-left corner in PDF units (72 units = 1 inch).
SHAPE_OBJECTS='[
  {
    "type": "rectangle",
    "page": 1,
    "x": 54,
    "y": 540,
    "width": 504,
    "height": 108,
    "fill_color_rgb": "245,247,250",
    "stroke_color_rgb": "26,72,112",
    "stroke_width": 1,
    "tag_is_artifact": true
  },
  {
    "type": "line",
    "page": 1,
    "x1": 72,
    "y1": 576,
    "x2": 540,
    "y2": 576,
    "stroke_color_rgb": "26,72,112",
    "stroke_width": 1.5,
    "tag_actual_text": "Review section divider",
    "tag_structure_type": "Figure"
  }
]'

curl -X POST "$API_URL/pdf-with-added-shapes" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
  -F "file=@/path/to/input.pdf" \
  -F "shape_objects=$SHAPE_OBJECTS" \
  -F "tag_enabled=true" \
  -F "output=review-panel"

Source: GitHub Repository

Breaking Down the Code

The script begins by setting the API_URL variable to the US-based endpoint "https://api.pdfrest.com". This is the default for global users, but there's an option to switch to an EU-based endpoint for GDPR compliance.

API_URL="https://api.pdfrest.com"

The SHAPE_OBJECTS variable contains a JSON array defining the shapes to be added to the PDF. It specifies a rectangle and a line, detailing their positions, dimensions, and colors. The rectangle is filled with a light shade and outlined, while the line serves as a divider.

SHAPE_OBJECTS='[
  {
    "type": "rectangle",
    "page": 1,
    ...
  },
  {
    "type": "line",
    "page": 1,
    ...
  }
]'

The curl command sends a POST request to the pdf-with-added-shapes endpoint. It includes headers for accepting JSON responses and specifying multipart form data. An API key is required for authentication.

curl -X POST "$API_URL/pdf-with-added-shapes" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
  ...

The -F flags specify form data: the PDF file to modify, the shape objects to add, enabling tagging, and the output file name.

-F "file=@/path/to/input.pdf" \
-F "shape_objects=$SHAPE_OBJECTS" \
-F "tag_enabled=true" \
-F "output=review-panel"

Beyond the Tutorial

In this tutorial, you learned how to use cURL to call the pdfRest Add to PDF API, adding shapes to a PDF document. This example demonstrates the flexibility and power of automating PDF modifications through API calls.

To further explore the capabilities of pdfRest, try out all the API Tools in the API Lab. For detailed information on each endpoint, refer to the API Reference Guide.

Note: This is an example of a multipart API call. For code samples using JSON payloads, visit this GitHub repository.

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