Comparing two versions of a PDF does not necessarily require uploading documents to an online service.
For internal documentation, reports, invoices, technical specifications, or administrative documents, local processing can provide a straightforward way to identify changes without transferring sensitive files to a third party.
PDF Comparator is a lightweight Python utility designed to compare the textual content of two PDF files directly on a local machine.
The tool focuses on a specific task: extracting text, normalizing it, and identifying differences between two document versions.
It requires Python 3 and pypdf, with no external API, remote processing, or AI dependency.
1. How PDF Text Comparison Works
A PDF is not structured like a conventional text document. Its primary purpose is to describe how content should appear on a page.
Before comparing two PDFs, their textual content must therefore be extracted and converted into a representation suitable for comparison.
PDF Comparator follows a simple processing pipeline:
PDF A PDF B
│ │
â–¼ â–¼
Text extraction Text extraction
│ │
â–¼ â–¼
Normalization Normalization
│ │
└────────────┬────────────┘
│
â–¼
Text comparison
│
â–¼
Detected differences
The first PDF is treated as the reference version, while the second represents the new version.
Text is extracted from all pages of both documents before being normalized and compared.
2. Text Extraction with pypdf
The utility relies on pypdf to read PDF documents and extract their textual content.
This extraction step makes it possible to compare the text contained in two documents without analyzing their graphical rendering.
A typical extraction workflow can be represented as follows:
PDF document
│
â–¼
Read pages
│
â–¼
Extract available text
│
â–¼
Combine extracted content
│
â–¼
Text ready for comparison
The quality of the extracted content depends on how the original PDF was generated.
A PDF containing a properly defined text layer can generally be processed directly. A scanned document containing only images, however, does not provide the same textual information.
Such documents require optical character recognition (OCR) before a reliable textual comparison can be performed.
3. Lightweight Text Normalization
Two PDF documents can contain visually identical text while producing slightly different extracted content.
Differences in whitespace, line breaks, or text positioning may influence the extraction result.
PDF Comparator applies lightweight normalization before comparing the documents.
The purpose of this step is to reduce unnecessary differences in the extracted text while preserving meaningful content changes.
For example:
Document A:
Invoice number: 2026-001
Document B:
Invoice number: 2026-001
These two lines contain the same information, despite a difference in spacing.
Normalization can help reduce this type of variation.
However, normalization must remain limited. Removing too much information could conceal changes that are relevant to the document being compared.
The objective is not to interpret or rewrite the document, but to prepare its extracted text for comparison.
4. Detecting Added and Removed Lines
Once both documents have been processed, their textual content is compared to identify differences.
The result follows a line-based diff representation:
--- old.pdf
+++ new.pdf
-Address: 12 Example Street
+Address: 24 Example Street
-Total: €1,200
+Total: €1,350
Each prefix has a specific meaning:
| Prefix | Meaning |
|---|---|
--- |
Reference document |
+++ |
New document |
- |
Line removed from the reference |
+ |
Line added in the new document |
A modified line is represented as a removed line followed by an added line.
In this example, two changes are immediately visible: the address and the total amount.
The utility does not attempt to determine whether these changes are correct. It simply identifies differences in the extracted textual content.
5. Running the Comparison
The utility requires Python 3 and the pypdf library.
The dependency can be installed with:
python -m pip install pypdf
The comparison is then executed by passing the two PDF files to the Python script:
python pdf-compare.py old.pdf new.pdf
The arguments define the comparison order:
| Argument | Role |
|---|---|
old.pdf |
Reference document |
new.pdf |
New document |
If textual differences are detected, they are displayed directly in the terminal.
If no textual difference is found, the utility reports that no difference was detected.
Importantly, this means that no difference was found in the extracted and normalized text. It does not necessarily mean that the two PDF files are identical at the binary or visual level.
6. Textual Comparison vs. Visual Comparison
PDF comparison can refer to several different operations.
A textual comparison identifies changes in extracted text, while a visual comparison examines how documents are rendered.
PDF Comparator is deliberately limited to textual comparison.
| Comparison type | Supported |
|---|---|
| Added text | Yes |
| Removed text | Yes |
| Modified text | Yes |
| Images | No |
| Fonts | No |
| Colors | No |
| Graphical layout | No |
| Visual rendering | No |
For example, changing an invoice total from 1200 to 1350 produces a textual difference.
Changing the font or moving the same text to another position on the page may not produce a meaningful textual difference.
Similarly, two PDFs may appear identical when displayed but contain different internal representations of their text.
These distinctions are important when choosing the appropriate comparison method.
7. Practical Use Cases
A local PDF comparison utility can be useful when reviewing successive versions of documents such as:
- Technical documentation
- Reports and specifications
- Invoices
- Administrative documents
- Generated PDFs
- Archived documents
It can also support development workflows involving automated PDF generation.
For example, after modifying a document generation system, a developer may want to compare two generated outputs to determine whether their textual content has changed.
Previous PDF output
│
â–¼
Comparison
â–²
│
Updated PDF output
This can provide a quick way to identify unexpected changes in generated content.
However, textual comparison alone does not replace visual inspection or functional testing when document layout and formatting are also important.
8. Local Processing and Document Privacy
The utility processes both PDF files directly on the local machine.
Local machine
│
├── Reference PDF
├── New PDF
├── Python script
└── Comparison output
No document upload or remote processing is required.
The comparison does not depend on:
- An external API
- A remote server
- A user account
- An AI model
Once Python and the required dependency are installed, the comparison can be performed offline.
For documents containing confidential or internal information, this architecture avoids an unnecessary transfer to an external service.
Local processing does not eliminate every security concern, but it keeps the comparison workflow within the user's own environment.
9. Limitations of Text Extraction
The main limitation of this approach comes from the PDF format itself.
Text extraction does not always reproduce the reading order that a person sees on the page.
Documents containing multiple columns, complex tables, or unusual text positioning may produce extracted text in an unexpected order.
Scanned documents introduce another limitation: without an existing text layer, there may be little or no text available for comparison.
| Document characteristic | Potential limitation |
|---|---|
| Standard text layer | Suitable for text extraction |
| Multiple columns | Reading order may differ |
| Complex tables | Structure may be lost |
| Scanned pages | OCR may be required |
| Graphical content | Not included in text analysis |
These limitations do not necessarily indicate a problem with the comparison itself. They reflect the difference between extracting textual content and reconstructing the visual or logical structure of a PDF.
A textual diff should therefore be interpreted in the context of the documents being compared.
Conclusion
PDF Comparator illustrates a focused approach to document comparison using Python and pypdf.
By separating text extraction, normalization, and comparison, the utility provides a straightforward way to identify changes between two PDF versions without relying on remote processing.
Its scope remains intentionally limited: it detects textual differences rather than visual changes or the meaning of those changes.
For development workflows, document reviews, and internal files, this distinction makes the tool useful without introducing unnecessary complexity.
Two PDF files, local text extraction, and a readable representation of their differences.

Top comments (0)