Why Drawing Black Boxes on PDFs Leaks Sensitive Data (and How to Permanently Scrub PII in Python)
Every year, law firms, HR teams, and government agencies accidentally leak Social Security numbers, bank accounts, and confidential clauses in "redacted" PDFs. Why? Because drawing a black rectangle on top of text in ma
Every year, law firms, HR teams, and government agencies accidentally leak Social Security numbers, bank accounts, and confidential clauses in "redacted" PDFs.
Why? Because drawing a black rectangle on top of text in macOS Preview, Word, or basic PDF editors only adds a visual overlay. The underlying text stream is still sitting untouched in the PDFβanyone can press Ctrl+A, Ctrl+C, and paste the "redacted" SSN straight into Notepad.
Here is how to build a true permanent PDF redaction pipeline in Python that physically destroys the underlying text layer so 0% of redacted PII can ever be recovered.
1. Detect Sensitive PII with Exact Word Coordinates
Using pdfplumber, we can scan every page for SSNs, EINs, Luhn-verified credit cards, IBANs, emails, and custom keywords while capturing each token's exact (x0, top, x1, bottom) bounding box:
import io, re, pdfplumber
SSN_RE = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")
def find_ssn_boxes(pdf_bytes: bytes):
hits = []
with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
for page_num, page in enumerate(pdf.pages, start=1):
for w in page.extract_words():
if SSN_RE.search(w["text"]):
hits.append((page_num, (w["x0"], w["top"], w["x1"], w["bottom"]), page.width, page.height))
return hits
2. Burn Opaque Boxes Directly into Flattened Pixels
To guarantee mathematical irreversibility (HIPAA / GDPR / court discovery safe), render each page via pypdfium2, paint solid black rectangles directly over the pixel coordinates with Pillow, and rebuild the page as a flattened image PDF:
import pypdfium2 as pdfium
from PIL import ImageDraw
doc = pdfium.PdfDocument(pdf_bytes)
page = doc[0]
img = page.render(scale=160/72.0).to_pil().convert("RGB")
draw = ImageDraw.Draw(img)
# Scale PDF points -> image pixels and burn opaque black pixels
draw.rectangle([x0 * sx - 3, top * sy - 3, x1 * sx + 3, bottom * sy + 3], fill=(0, 0, 0))
3. Verify 0 Characters Remain in the Text Stream
Always run a post-redaction assertion before shipping a redacted PDF:
with pdfplumber.open(io.BytesIO(redacted_pdf_bytes)) as check:
remaining = sum(len((p.extract_text() or "").strip()) for p in check.pages)
assert remaining == 0, "Text layer still present!"
Try the Live Legal & Privacy PDF Toolkit (Zero Login + REST API)
We packaged this deterministic engineβalongside two companion legal document utilitiesβinto ToolForge:
- Permanent PDF PII Redactor (with Audit Certificate): tools.takefox.com/pdf-pii-redactor (includes a 1-click sample disclosure PDF to test immediately)
- Legal Bates Stamping & Court Exhibit Indexer: tools.takefox.com/bates-stamper
- Dual-PDF Contract & Lease Redline Diff (Watermark-Free): tools.takefox.com/pdf-contract-diff
- REST API v1 (RapidAPI): tools.takefox.com/developers
If you work with legal discovery, HIPAA compliance, or GDPR anonymization and need support for country-specific ID patterns, let me know in the comments!
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.