How to Add Tables to PDF Files with cURL
Why Add Tables to PDF with cURL?
The pdfRest Add to PDF API Tool is a powerful resource for developers looking to programmatically enhance PDF documents by adding tables, images, and other elements. 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 following this tutorial, you will learn how to integrate this functionality into your applications, enabling automated PDF modifications.
A project manager might use the Add to PDF tool to insert a project status table into a PDF report before distributing it to stakeholders. This table could include milestones, responsible team members, and current status updates, all formatted for clarity and ease of understanding. Automating this process with the Add to PDF API can save time and reduce manual errors, ensuring that documents are consistently updated and formatted.
Add Tables to PDF 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 an accessible project-status table with a header, colored status cells, and a footer.
TABLE_OBJECTS='[
{
"page": 1,
"x": 54,
"y": 540,
"width": 504,
"columns": [{"width": 210}, {"width": 144}, {"width": 150}],
"tag_structure_type": "Table",
"style": {"padding": {"top": 6, "right": 8, "bottom": 6, "left": 8}, "text_size": 10},
"header_rows": [{"cells": [
{"text": "Milestone", "tag_structure_type": "TH", "style": {"background_color_rgb": [26,72,112], "text_color_rgb": [255,255,255]}},
{"text": "Owner", "tag_structure_type": "TH", "style": {"background_color_rgb": [26,72,112], "text_color_rgb": [255,255,255]}},
{"text": "Status", "tag_structure_type": "TH", "style": {"background_color_rgb": [26,72,112], "text_color_rgb": [255,255,255]}}
]}],
"rows": [
{"cells": [{"text": "Requirements review"}, {"text": "Maya Chen"}, {"text": "Complete", "tag_structure_type": "TD", "style": {"background_color_rgb": [220,252,231]}}]},
{"cells": [{"text": "Prototype delivery"}, {"text": "Jordan Lee"}, {"text": "In progress", "tag_structure_type": "TD", "style": {"background_color_rgb": [254,249,195]}}]},
{"cells": [{"text": "Stakeholder approval"}, {"text": "Avery Patel"}, {"text": "Planned", "tag_structure_type": "TD", "style": {"background_color_rgb": [239,246,255]}}]}
],
"footer_rows": [{"cells": [
{"text": "Next review: Friday, 10:00 AM", "col_span": 3, "tag_structure_type": "TD", "style": {"background_color_rgb": [245,247,250], "text_color_rgb": [55,65,81]}}
]}]
}
]'
curl -X POST "$API_URL/pdf-with-added-tables" \
-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 "table_objects=$TABLE_OBJECTS" \
-F "tag_enabled=true" \
-F "tag_language=en-US" \
-F "output=project-status"
Source: GitHub
Breaking Down the Code
The script begins by setting the API_URL variable to the default US-based service endpoint. This is the primary endpoint for global use. If you need to comply with GDPR or require better performance in Europe, you can switch to the EU-based service by uncommenting the alternative URL.
API_URL="https://api.pdfrest.com" # API_URL="https://eu-api.pdfrest.com"
The TABLE_OBJECTS variable defines the structure of the table to be added to the PDF. It includes details such as page number, position, width, column definitions, and cell styles.
TABLE_OBJECTS='[
{
"page": 1,
"x": 54,
"y": 540,
"width": 504,
...
}
]'
The curl command is used to send a POST request to the pdf-with-added-tables endpoint. The request includes several headers and form fields:
curl -X POST "$API_URL/pdf-with-added-tables" \ -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 "table_objects=$TABLE_OBJECTS" \ -F "tag_enabled=true" \ -F "tag_language=en-US" \ -F "output=project-status"
The -H flags set the request headers, including the Api-Key, which must be replaced with your actual API key. The -F flags specify the form data, including the PDF file to be modified, the table objects, and additional parameters such as tag_enabled and tag_language.
Beyond the Tutorial
In this tutorial, you learned how to use cURL to send a request to the pdfRest Add to PDF API, adding a table to a PDF document. This example demonstrated the use of multipart form data to include both the PDF file and the table configuration in the request.
To explore further, you can demo all of the pdfRest API Tools in the API Lab. For detailed information on each API tool, refer to the API Reference Guide.
Note: This example uses a multipart API call. Code samples using JSON payloads can be found at GitHub.