Skip to content
Format XML

XML Formats

PDF XML Format

PDF and XML are fundamentally different file formats, but they intersect in several important ways — from XFA forms and XMP metadata to full PDF-to-XML conversion. Here is how they relate and how to work with XML inside PDFs.

PDF vs XML — two different jobs

PDF (Portable Document Format) is a fixed-layout format designed for printing and viewing. It preserves fonts, images, and positioning so a document looks the same everywhere. XML (Extensible Markup Language) is a structured data format — human-readable tags that describe what data means, not how it looks.

AspectPDFXML
PurposeVisual presentationData exchange
ReadabilityBinary (not human-readable)Plain text (human-readable)
StructureFixed layout, pagesHierarchical, schema-driven
EditingRequires PDF editorAny text editor
Machine parsingDifficult — layout-orientedEasy — data-oriented

Despite these differences, XML shows up inside PDFs more often than you might expect. The next sections cover the three main places where PDF and XML meet.

XFA forms — XML-powered PDF forms

XFA (XML Forms Architecture) is an Adobe specification that embeds XML directly inside a PDF to define interactive forms. Unlike the simpler AcroForms, XFA stores the entire form structure — fields, validation rules, calculations, and layout — as XML streams within the PDF container.

A typical XFA PDF contains these XML packets:

  • template — defines the form fields, their types, and layout rules
  • datasets — holds the actual form data submitted by users
  • config — rendering settings like pagination and locale
  • connectionSet — SOAP/WSDL bindings for server-side submission

XFA was widely used in government and enterprise forms (tax filings, insurance claims, regulatory submissions) throughout the 2000s and 2010s. Adobe deprecated XFA in 2017 when the ISO 32000-2 (PDF 2.0) standard dropped it, but millions of legacy XFA PDFs still circulate.

Modern browsers and most open-source PDF viewers cannot render XFA forms. Only Adobe Acrobat (Reader or Pro) fully supports them — which is why extracting the XML data from these files is often the most practical way to work with them.

XMP metadata — XML inside every PDF

XMP (Extensible Metadata Platform) is an ISO standard (ISO 16684) for embedding metadata in files using RDF/XML. Every modern PDF has an XMP metadata stream containing properties like title, author, creation date, and keywords — all stored as XML.

Here is what XMP metadata looks like inside a PDF:

<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/">
  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <rdf:Description rdf:about=""
      xmlns:dc="http://purl.org/dc/elements/1.1/"
      xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
      <dc:title>
        <rdf:Alt>
          <rdf:li xml:lang="x-default">Annual Report 2025</rdf:li>
        </rdf:Alt>
      </dc:title>
      <dc:creator>
        <rdf:Seq>
          <rdf:li>Jane Smith</rdf:li>
        </rdf:Seq>
      </dc:creator>
      <pdf:Producer>LibreOffice 7.6</pdf:Producer>
    </rdf:Description>
  </rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>

XMP is also used in JPEG, PNG, TIFF, and other formats — so the same XML structure appears across file types, not just PDFs.

PDF/A — archival PDFs with mandatory XML metadata

PDF/A is the ISO standard (ISO 19005) for long-term document archiving. It requires an XMP metadata stream and restricts features that could break future readability — no JavaScript, no external font references, no encryption. Courts, government archives, and regulated industries use PDF/A for records that must remain readable for decades.

PDF/A-3 (ISO 19005-3) goes further: it allows embedding arbitrary files as attachments, including XML data files. This enables a common pattern in European e-invoicing — the PDF shows a human-readable invoice while an attached XML file (typically in the ZUGFeRD or Factur-X format) carries the machine-readable data.

Extracting XML from a PDF

The method depends on what kind of XML you need:

Extract XMP metadata

Most PDF libraries expose XMP as a string. With Python and pikepdf:

import pikepdf

pdf = pikepdf.open("document.pdf")
xmp = str(pdf.open_metadata())
print(xmp)  # Raw XMP XML string

With the exiftool command line:

exiftool -xmp -b document.pdf > metadata.xml

Extract XFA form data

In Python with pikepdf, XFA data lives in the AcroForm dictionary:

import pikepdf

pdf = pikepdf.open("xfa-form.pdf")
root = pdf.Root
acroform = root.get("/AcroForm")
xfa = acroform.get("/XFA")

# XFA is usually an array of [name, stream, name, stream, ...]
for i in range(0, len(xfa), 2):
    name = str(xfa[i])
    xml_bytes = xfa[i + 1].read_bytes()
    print(f"--- {name} ---")
    print(xml_bytes.decode("utf-8"))

Convert PDF text content to XML

If you want the visible text and structure of a PDF as XML, use pdftohtml (part of Poppler):

pdftohtml -xml document.pdf output.xml

This produces an XML file with <page>, <text>, and <fontspec> elements representing each page's content. The output is useful for data extraction but does not preserve the original document layout perfectly.

Apache Tika is another option that handles a wider range of PDF structures:

java -jar tika-app.jar -x document.pdf > output.xml

Formatting XML extracted from PDFs

XML pulled from PDFs is almost always a single unindented blob. Before you can read or debug it, you need to pretty-print it. You can do that on the command line with xmllint:

xmllint --format metadata.xml

Or paste it directly into an online XML formatter for instant syntax highlighting, validation, and a choice of indent styles — with no file upload required.

Common PDF-XML workflows

Here are the scenarios where PDF and XML cross paths in practice:

  • E-invoicing (ZUGFeRD / Factur-X): Generate a PDF/A-3 invoice with an embedded XML attachment. Accounting software reads the XML; humans read the PDF.
  • Government forms: Many tax authorities still distribute XFA-based PDFs. Fill them in Acrobat, then extract the datasets XML to import into your records system.
  • Data extraction: Convert PDF reports to XML with pdftohtml or Tika, then parse the XML to pull tables, figures, or text into a database.
  • Metadata auditing: Extract XMP metadata from a batch of PDFs to check authorship, creation dates, or software versions across an archive.
  • Accessibility compliance: PDF/UA (Universal Accessibility) requires tagged structure — essentially an XML tree of semantic elements inside the PDF. Validation tools export this tag tree as XML for inspection.

Frequently asked questions

Can I convert any PDF to XML?

You can always extract XMP metadata (every modern PDF has it). Converting the visible content to structured XML is possible with tools like pdftohtml or Tika, but the quality depends on the PDF — scanned documents require OCR first, and complex layouts may not map cleanly to XML elements.

Is XFA still supported?

Adobe Acrobat Reader still renders XFA forms, but the standard was removed from PDF 2.0 (ISO 32000-2:2020). No browser or open-source PDF viewer supports XFA. For new projects, use AcroForms or HTML forms instead.

How do I validate XML extracted from a PDF?

Paste it into an XML validator to check well-formedness. If you have the XSD schema (common with e-invoicing formats), you can validate against it with xmllint --schema on the command line.

What is the difference between PDF/XML and PDF/A?

There is no format called “PDF/XML” — the term usually refers to the general relationship between PDF and XML (XFA, XMP, or conversion output). PDF/A is a specific ISO standard for archival PDFs that mandates XMP metadata and, in the PDF/A-3 variant, allows embedded XML file attachments.

Format your extracted XML instantly

Pulled XML from a PDF and got an unreadable wall of tags? Paste it into our formatter for instant indentation, syntax highlighting, and validation — right in your browser, no upload needed.

Open Format XML