How to Redact PDF Text with cURL

Learn how to use cURL to redact text on a PDF document with the pdfRest Redact PDF API tool.
Share this page

Why Redact PDF Text with cURL?

The pdfRest Redact PDF API Tool is a powerful utility designed for securely removing sensitive information from PDF documents. By using cURL, a command-line tool for transferring data with URLs, you can easily interact with this API to automate the redaction process. This tutorial will guide you through sending an API call to the Redact PDF endpoint using cURL, allowing you to efficiently redact text from your PDF files.

In real-world scenarios, redacting PDF documents is crucial for maintaining privacy and confidentiality. For instance, a legal firm might need to share documents with clients or opposing counsel but must ensure that sensitive information, such as personal identifiers or confidential business information, is not disclosed. By using the Redact PDF API, they can automate the redaction process, ensuring that all sensitive data is consistently removed before sharing the documents.

Redact PDF Text with cURL Code Example

REDACTIONS='[{"type":"preset","value":"email"},{"type":"regex","value":"(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}"},{"type":"literal","value":"word"}]'

curl -X POST "https://api.pdfrest.com/pdf-with-redacted-text-preview" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
  -F "file=@/path/to/file" \
  -F "redactions=$REDACTIONS" \
  -F "output=example_out"

Source: GitHub Repository

Breaking Down the Code

The code begins with defining a variable REDACTIONS which specifies the types of text to be redacted from the PDF. The redactions include:

REDACTIONS='[{"type":"preset","value":"email"},{"type":"regex","value":"(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}"},{"type":"literal","value":"word"}]'

- preset: Redacts email addresses using a pre-defined pattern.

- regex: Uses a regular expression to redact phone numbers formatted in various ways.

- literal: Redacts the literal word "word".

The curl command is used to make a POST request to the pdfRest API endpoint https://api.pdfrest.com/pdf-with-redacted-text-preview. The headers specify the request's content type and the API key for authentication:

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

The -F flag is used to specify the form data being sent in the request:

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

This uploads the PDF file to be redacted. The path must be replaced with the actual file path.

-F "redactions=$REDACTIONS"

This sends the redaction instructions defined earlier.

-F "output=example_out"

This specifies the output file name for the redacted PDF.

Beyond the Tutorial

In this tutorial, you've learned how to use cURL to send a request to the pdfRest Redact PDF API, specifying redactions for email addresses, phone numbers, and specific words. This example demonstrates how to automate the redaction process using a command-line tool, which can be particularly useful for batch processing documents.

To further explore the capabilities of pdfRest, consider trying out all the API Tools available in the API Lab. For more detailed information, refer to the API Reference Guide.

Note: This example uses a multipart API call. For code samples using JSON payloads, visit the GitHub Repository.

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