跪拜 Guibai
← All articles
Python · Backend

PDF Batch Processing: The Only 'Done' Is a Second Extraction's Count

By databook ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A PDF batch that looks successful can archive missing pages or unredacted personal data, and redaction mistakes can't be undone once the file is public. Verification must be a second extraction that counts, not a human scan of the output.

Summary

Batch PDF jobs produce output files without erroring while pages silently disappear. A Python pipeline for archiving course materials demonstrates why each step needs a second pass. After splitting, the script counts actual /Type /Page objects in the content stream, not just the /Count metadata value, because incremental updates can leave orphan objects that inflate the count. After extraction, pages that return empty strings are flagged separately; an empty string usually means a scanned image, not an empty page, and the extractor won't tell you.

The strictest check is redaction. Drawing a black rectangle over author names and answer keys leaves the underlying Tj line in the content stream; re-extraction still finds the sensitive terms. Only removing the entire Tj line brings the count to zero. The pipeline classifies operations as reversible or irreversible before deciding how strict to be: reversible mistakes cost a re-run, irreversible ones cost a public leak. A final checklist keeps declared page count, actual object count, and 'no text layer' as separate columns, because three of five test files failed silently—two scanned pages, a truncated file, and a metadata mismatch—without any script error.

Takeaways
— Verify split output by summing the page counts of the parts and comparing that sum to the actual number of /Type /Page objects in the content stream, not to the /Count metadata value, because incremental updates can leave orphan page objects.
— When extraction returns an empty string for a page, mark it as a possible scanned image; the extractor does not error on pages without a text layer.
— Drawing a black rectangle over sensitive text does not remove the underlying text; the Tj line remains in the content stream and can be copied or extracted.
— To actually redact text in a PDF, remove the entire line containing the sensitive Tj operator, then re-extract and count; zero occurrences is the only pass condition.
— Separate declared page count from actual page object count in the checklist; a mismatch between the two is a signal in itself.
— Run the overview/checklist first, not last, so you know which files are truncated or scanned before deciding how to split and extract.
— A script that exits without errors and writes all output files can still contain silent failures; any page that fails is only visible if you explicitly flag it.
Conclusions

Classifying operations by whether a mistake can be re-run is a more practical driver for verification strength than a blanket 'test everything' rule; it concentrates effort on the irreversible redaction step.

PDF's separation of graphics layer from content stream means visual inspection can never confirm redaction; only a re-extraction over the parsed stream can prove the sensitive text is gone.

Silent failures dominate PDF batch work: the three flagged files in the sample all passed normal success criteria—no error, output present—and were only caught because the checklist had explicit columns for 'can open' and 'no text layer'.

Running the checklist as a post-processing summary is backwards; it has to run first to shape the rest of the pipeline, which is why the article's sample treats it as the final step but notes the awkwardness.

Concepts & terms
Orphan page object
A /Type /Page object not attached to the page tree, left behind by PDF incremental updates; readers ignore it but a naive count finds it, creating a mismatch with the /Count metadata.
Tj operator
The PDF content stream command that places a text string on the page. Removing the line containing it deletes the text; drawing a rectangle over it does not.
Text layer
The embedded extractable text in a PDF, independent of visual appearance. Scanned pages have none, so extraction returns an empty string instead of an error.
From the discussion
Featured comments
卷土的土

The step of re-extracting text after redaction cannot be skipped.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗