feat(extract): support plain XML invoices and UBL syntax

/extract now accepts all EN 16931 e-invoices, not only ZUGFeRD PDFs:

- plain XML files via the new xml_base64 field; XML sent as pdf_base64
  is detected from the content
- UBL 2.1 Invoice and CreditNote (XRechnung UBL, Peppol BIS Billing) via
  the new src/ubl_parser.py, also when embedded in a PDF
- new response field xml_syntax ("CII" or "UBL")
- CII invoice dates are normalized to YYYY-MM-DD, matching UBL
- XRechnung profiles are detected from the specification identifier,
  independent of the factur-x version (6.x reports them as EN16931)
- untrusted XML is parsed without entity or DTD resolution (XXE)

Fixtures: KoSIT XRechnung test-suite case 01.01a in CII and UBL.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 18:32:46 +01:00
co-authored by Claude Fable 5.2
parent 6126a19bb7
commit 7aa4506dd7
8 changed files with 1229 additions and 42 deletions
+7 -5
View File
@@ -12,7 +12,7 @@ from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from src.extractor import ExtractionError, extract_zugferd
from src.extractor import ExtractionError, extract_from_xml, extract_invoice
from src.models import (
ExtractRequest,
ExtractResponse,
@@ -113,23 +113,25 @@ async def health_check() -> HealthResponse:
@app.post("/extract", response_model=ExtractResponse)
async def extract_pdf(request: ExtractRequest) -> ExtractResponse:
"""Extract ZUGFeRD data from PDF.
"""Extract e-invoice data from a PDF or a plain invoice XML.
Args:
request: ExtractRequest with pdf_base64 field
request: ExtractRequest with pdf_base64 or xml_base64 field
Returns:
ExtractResponse with extraction results
"""
try:
pdf_bytes = base64.b64decode(request.pdf_base64)
file_bytes = base64.b64decode(request.xml_base64 or request.pdf_base64 or "")
except Exception:
raise HTTPException(
status_code=400,
detail={"error": "invalid_base64", "message": "Invalid base64 encoding"},
)
return extract_zugferd(pdf_bytes)
if request.xml_base64 is not None:
return extract_from_xml(file_bytes)
return extract_invoice(file_bytes)
@app.post("/validate", response_model=ValidateResponse)