A PDF and an XML file can represent the same document while containing different information.
A value may have been modified in one representation, a reference may be missing, a number may use a different format, or some information may exist in one file but not the other.
When both files are part of the same document processing workflow, checking their consistency manually can quickly become time-consuming.
PDF / XML Comparator is a lightweight Python utility designed to perform an initial comparison between these two representations locally.
Its purpose is not to interpret the business meaning of every field, but to identify potential differences between the textual content of a PDF and the data contained in an XML file.
1. Two Formats, Two Different Representations
PDF and XML serve fundamentally different purposes.
A PDF provides a human-readable representation of a document. It defines how text, images, and other elements appear on a page.
XML provides a structured representation of data that can be processed by software.
| Format | Primary purpose | Data representation |
|---|---|---|
| Human-readable document | Text and graphical elements | |
| XML | Machine-readable information | Structured elements and data |
Even when both files originate from the same generation process, their contents are not necessarily identical.
Consider the following simplified example.
PDF content:
Invoice number: INV-2026-001
Customer: Example Company
Total: 1,250.00 EUR
XML content:
<Invoice>
<InvoiceNumber>INV-2026-001</InvoiceNumber>
<Customer>Example Company</Customer>
<Total>1350.00</Total>
<Currency>EUR</Currency>
</Invoice>
Both files describe the same invoice, but the total amount differs.
The PDF displays 1,250.00, while the XML contains 1350.00.
A comparison between the two representations can help identify this type of inconsistency.
The XML structure shown here is illustrative and does not represent a specific invoicing standard.
2. Why a Bidirectional Comparison?
Checking whether XML values appear in a PDF is useful, but it only addresses one side of the problem.
A value may exist in the XML without appearing in the PDF. Conversely, information displayed in the PDF may not be present in the XML.
PDF / XML Comparator follows a bidirectional approach.
PDF ──► Text extraction ──► Search values in XML
│
â–¼
Differences
â–²
│
XML ──► Data extraction ──► Search values in PDF
The comparison operates in both directions:
| Direction | Objective |
|---|---|
| PDF → XML | Find PDF values in the XML data |
| XML → PDF | Find XML values in the PDF content |
This distinction is important because neither file is automatically assumed to contain every piece of information present in the other.
A bidirectional comparison provides a broader view of potential discrepancies than checking only one representation against the other.
3. Extracting Comparable Information
The first stage consists of extracting information from both files.
For the PDF, the comparison relies on its textual content.
For the XML, it relies on the data contained in the structured document.
PDF document XML document
│ │
â–¼ â–¼
Text extraction Data extraction
│ │
└─────────────┬──────────────┘
│
â–¼
Comparable values
The two extraction processes serve the same objective: obtaining information that can be searched and compared across both representations.
However, the extracted information does not necessarily retain the same structure.
For example, a PDF may display:
Customer: Example Company
While the corresponding XML may contain:
<Customer>Example Company</Customer>
The visible label and the XML element name are different representations of the same information.
This is why comparing the raw contents of a PDF file and an XML file would not be sufficient. The comparison must operate on extracted information rather than on the files' binary contents.
4. Identifying Potential Differences
A comparison between PDF and XML data can help identify several types of discrepancies:
- A reference that differs between the two files
- An amount that has changed in one representation
- A value present only in the XML
- Information displayed in the PDF but absent from the XML
- Equivalent values represented using different formats
Consider the following example.
PDF content:
Reference: INV-2026-042
Customer: Example Company
Total: 1,200.00 EUR
XML content:
<Invoice>
<Reference>INV-2026-042</Reference>
<Customer>Example Company</Customer>
<Total>1350.00</Total>
<Currency>EUR</Currency>
</Invoice>
The reference and customer name are consistent between the two representations.
The amount, however, differs.
A conceptual comparison result could be represented as follows:
| Information | PDF value | XML value |
|---|---|---|
| Reference | INV-2026-042 | INV-2026-042 |
| Customer | Example Company | Example Company |
| Total | 1,200.00 | 1350.00 |
This table illustrates the type of discrepancy a comparison can help reveal. It does not represent the exact output format of the utility.
5. Formatting Differences and Equivalent Values
One of the challenges of comparing PDF and XML data is that equivalent information can be represented differently.
For example:
| Information | PDF representation | XML representation |
|---|---|---|
| Amount | 1,250.00 | 1250.00 |
| Date | 15/08/2026 | 2026-08-15 |
| Percentage | 20% | 20 |
These values may represent the same information, even though their textual representations differ.
A direct textual search may therefore identify a difference where the underlying information is actually equivalent.
This introduces an important distinction between textual matching and semantic comparison.
Textual matching checks whether a value can be found in another representation.
Semantic comparison attempts to determine whether two differently formatted values have the same meaning.
The second approach requires additional knowledge about the data type, formatting conventions, and sometimes the business context.
For example, interpreting a date requires knowing which date format is being used. Comparing monetary amounts may require understanding decimal separators and currency conventions.
PDF / XML Comparator is deliberately designed as an initial comparison tool rather than a system that interprets the business meaning of every field.
6. Value Presence Is Not the Same as Field Consistency
Finding a value in both documents does not necessarily prove that it belongs to the same field.
Consider the following simplified example.
PDF content:
Subtotal: 100.00
Tax: 20.00
Total: 120.00
XML content:
<Invoice>
<Subtotal>120.00</Subtotal>
<Tax>20.00</Tax>
<Total>100.00</Total>
</Invoice>
All three numerical values appear in both representations.
However, the subtotal and total have been exchanged.
A comparison based solely on value presence may not identify this inconsistency because each value can still be found in both documents.
Detecting this type of problem requires a more specific approach that associates each value with its corresponding field.
PDF subtotal → XML Subtotal
PDF tax → XML Tax
PDF total → XML Total
This is a different level of comparison, requiring knowledge of the document structure and the relationship between PDF labels and XML elements.
The example illustrates why an initial bidirectional comparison should not be treated as proof that two documents are fully consistent.
7. Information That Exists in Only One Representation
A PDF and an XML file do not necessarily contain exactly the same set of information.
An XML document may include technical identifiers, metadata, or structured information intended for software processing.
A PDF may include explanatory text, visual labels, or presentation elements intended for the reader.
For example:
<ProcessingReference>PROC-2026-001</ProcessingReference>
This value may exist in the XML without being displayed in the PDF.
Its absence from the PDF does not automatically indicate an error.
Similarly, a PDF may contain a descriptive heading that does not appear as a separate XML value.
A bidirectional comparison can help identify information that exists in only one representation, but determining whether that difference is expected requires additional context.
This distinction is particularly important when comparing documents generated for both human readers and automated processing systems.
8. Local Processing
PDF / XML Comparator is designed to perform its comparison locally using Python.
Local machine
│
├── PDF document
├── XML document
├── Extraction and comparison
└── Detected differences
The documents are processed within the local environment rather than being transferred to an online comparison service.
This approach is useful when working with internal documents, financial information, technical records, or other files that should not be unnecessarily shared with external services.
Local processing also allows the comparison to remain independent of a remote document analysis platform.
9. Practical Use Cases
A local PDF and XML comparison can be useful in several document processing workflows.
Examples include:
- Comparing a generated PDF with its associated XML data
- Reviewing successive versions of document generation systems
- Checking references, amounts, and identifiers across two representations
- Investigating discrepancies between human-readable documents and structured records
- Performing an initial consistency check before further document validation
For a developer working on a document generation system, the comparison can help identify unexpected changes between the data used by software and the information displayed to the user.
Structured source data
│
├── Generate PDF
│
└── Generate XML
│
â–¼
Bidirectional comparison
│
â–¼
Review discrepancies
This type of check can complement a broader testing workflow without requiring a complete document validation platform.
10. Comparison Is Not Validation
Comparing PDF and XML content is not the same as validating either document.
Several distinct operations may be involved in a complete document processing workflow.
| Operation | Objective |
|---|---|
| Text comparison | Identify textual differences |
| Value comparison | Check information across representations |
| XML schema validation | Verify the expected XML structure |
| Business rule checks | Verify domain-specific requirements |
| Visual inspection | Review document layout and rendering |
A value appearing in both files does not establish that the XML follows a required schema.
Likewise, a document can contain matching amounts while still failing a calculation rule or another business requirement.
For financial documents, matching values do not automatically establish tax compliance, calculation accuracy, or conformity with an electronic invoicing standard.
PDF / XML Comparator focuses on the comparison stage. It should therefore be understood as a complementary tool rather than a replacement for schema validation, business rule validation, or document compliance checks.
11. Limitations of the Approach
The main limitations arise from the differences between PDF and XML.
PDF text extraction may not preserve the visual reading order or the relationships between labels and values.
XML, by contrast, contains an explicit hierarchy of elements, but those elements do not necessarily correspond directly to the way information is presented in a PDF.
Scanned PDFs introduce another challenge because their content may exist only as images rather than as extractable text.
Other limitations include:
- Equivalent values represented using different formats
- Repeated values appearing in different fields
- Information intentionally present in only one representation
- Differences between textual presence and business meaning
These limitations explain why a bidirectional comparison is useful as an initial verification step, but cannot independently establish complete document equivalence.
Conclusion
PDF / XML Comparator illustrates a focused approach to comparing two different representations of the same document using Python.
By extracting information from both files and searching for values in both directions, the utility helps identify potential discrepancies between human-readable content and structured data.
Its scope remains intentionally limited. It does not attempt to interpret every field, validate business rules, or establish full document compliance.
The distinction between comparison and validation is central to this approach.
A local comparison can help identify where further investigation is needed, while more specialized validation mechanisms determine whether the documents meet their expected structural and business requirements.

Top comments (0)