Pdf Contract Redactor
by @chayjan
PDF contract redaction tool. Use when the user needs to redact sensitive information from scanned PDF contracts. The tool performs OCR to extract text, ident...
clawhub install pdf-contract-redactor📖 About This Skill
name: pdf-contract-redactor description: PDF contract redaction tool. Use when the user needs to redact sensitive information from scanned PDF contracts. The tool performs OCR to extract text, identifies field names and their corresponding values, and redacts only the values while keeping field names visible. Supports Alibaba Cloud OCR API for accurate Chinese text recognition.
PDF Contract Redactor
Redact sensitive values from scanned PDF contracts while preserving field names.
What It Does
1. OCR Recognition: Uses Alibaba Cloud OCR to extract text and positions from scanned PDFs 2. Field-Value Matching: Finds field names (e.g., "合同金额") and their corresponding values (e.g., "45640元") 3. Selective Redaction: Covers only the values with black boxes, keeping field names readable
Workflow
Step 1: PDF to Images
Convert PDF pages to high-resolution PNG images (200 DPI) for OCR.Step 2: OCR with Alibaba Cloud
Call Alibaba Cloud OCR API to get:Step 3: Match Fields to Values
For each field in the field list: 1. Find the field name text block 2. Look for the corresponding value in: - Right side: Same row, to the right of field name - Below: Next row, aligned with field name 3. Record field-value pair with both bounding boxesStep 4: Generate Redacted PDF
For each matched value: 1. Convert image coordinates to PDF coordinates 2. Draw black rectangle over the value area 3. Keep field name area unchangedField List
The following fields are searched and their values are redacted:
Usage
Prerequisites
1. Alibaba Cloud account with OCR service enabled 2. AccessKey ID and AccessKey Secret
Running the Tool
python scripts/redact_contract.py [output.pdf]
Example:
python scripts/redact_contract.py contract.pdf LTAIxxx xxx contract_redacted.pdf
Output
_redacted.pdf : Redacted PDF with values covered_fields.json : JSON file listing all matched field-value pairsImplementation Notes
OCR API
Uses Alibaba Cloud "通用文字识别-高精度版" (RecognizeAdvanced API):
https://ocr.aliyuncs.comField-Value Matching Logic
# For a field at (fx0, fy0, fx1, fy1)
Look for values that are:
1. To the right: vx0 > fx1 and |vy0 - fy0| < field_height * 2
2. Below: vy0 > fy1 and vx0 >= fx0 - field_width * 0.3
Choose the closest match
Coordinate Transformation
OCR returns coordinates in image space (200 DPI).
Convert to PDF space (72 DPI) using scale factor: scale = 72 / 200 = 0.36
Dependencies
pip install pymupdf pillow requests
Error Handling
💡 Examples
Prerequisites
1. Alibaba Cloud account with OCR service enabled 2. AccessKey ID and AccessKey Secret
Running the Tool
python scripts/redact_contract.py [output.pdf]
Example:
python scripts/redact_contract.py contract.pdf LTAIxxx xxx contract_redacted.pdf
Output
_redacted.pdf : Redacted PDF with values covered_fields.json : JSON file listing all matched field-value pairs⚙️ Configuration
1. Alibaba Cloud account with OCR service enabled 2. AccessKey ID and AccessKey Secret
Running the Tool
python scripts/redact_contract.py [output.pdf]
Example:
python scripts/redact_contract.py contract.pdf LTAIxxx xxx contract_redacted.pdf
Output
_redacted.pdf : Redacted PDF with values covered_fields.json : JSON file listing all matched field-value pairs