Skip to main content

Crate einvoice_pdf

Crate einvoice_pdf 

Source
Expand description

Extract the embedded e-invoice XML from a Factur-X, ZUGFeRD or Order-X PDF (a PDF/A-3 file with the invoice as an associated file), with its XMP metadata, in pure Rust.

  • extract returns the embedded file named as the specifications require (INVOICE_FILE_NAMES: factur-x.xml, zugferd-invoice.xml, ZUGFeRD-invoice.xml, xrechnung.xml, order-x.xml), its /AFRelationship, and the XMP metadata of the document catalog with the PDF/A identification and the Factur-X / ZUGFeRD properties (DocumentType, DocumentFileName, Version, ConformanceLevel).
  • attachments returns every embedded file.

Only the part of PDF (ISO 32000-2) needed to reach embedded files is read: the header, cross-reference tables and streams with their trailers and incremental updates (/Prev, /XRefStm), indirect objects, object streams, the object syntax, FlateDecode with the PNG predictors, the catalog’s /AF and /Names /EmbeddedFiles name tree, file specification dictionaries and embedded file streams (/Params /Size and /CheckSum are verified). Anything else the reader meets on that path, such as another filter, is refused with Error::Unsupported; a file that breaks the syntax is refused with Error::Malformed, never repaired. Encrypted files are refused (Error::Encrypted): PDF/A forbids encryption. Page content is never read.

The input is untrusted: nesting, reference chains, name tree depth, the number of cross-reference sections and of embedded files, and decoded sizes are bounded by Limits, each object and object stream is parsed at most once, and cycles are errors. The crate has no unsafe code and builds for wasm32-unknown-unknown.

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let pdf = std::fs::read("invoice.pdf")?;
    let invoice = einvoice_pdf::extract(&pdf)?;
    println!("{}: {} bytes", invoice.xml.file_name, invoice.xml.data.len());
    println!("relationship: {:?}", invoice.xml.relationship);
    if let Some(fx) = invoice.metadata.as_ref().and_then(|m| m.invoice.as_ref()) {
        println!("{:?} {:?} {:?}", fx.document_type, fx.version, fx.conformance_level);
    }
    for attachment in einvoice_pdf::attachments(&pdf)? {
        println!("embedded: {} ({:?})", attachment.file_name, attachment.mime_type);
    }
    Ok(())
}

Structs§

Attachment
An embedded file: its file specification and its decoded content.
Embedded
What extract returns.
InvoiceMetadata
The Factur-X / ZUGFeRD / Order-X extension schema properties of the XMP metadata.
Limits
Bounds on the work done for one file, against hostile input. Every bound that is reached is an Error::LimitExceeded naming the field.
Metadata
The XMP metadata of the document catalog (/Metadata).

Enums§

AfRelationship
The relationship of an associated file to the document (/AFRelationship, ISO 32000-2, 14.13.2, Table 43).
Error
Why a file could not be read, or why no invoice could be extracted from it.
Location
Where in the input an error was found.

Constants§

INVOICE_FILE_NAMES
The names under which the specifications embed the invoice XML: Factur-X (and ZUGFeRD 2.1 and later), ZUGFeRD 2.0, ZUGFeRD 1.0, XRechnung in ZUGFeRD, Order-X.

Functions§

attachments
Every embedded file of a PDF, with the default Limits: those of the /EmbeddedFiles name tree in key order, then those of the catalog’s /AF not already listed.
attachments_with
attachments with explicit Limits.
extract
The embedded invoice XML of a Factur-X, ZUGFeRD or Order-X PDF, with the default Limits.
extract_with
extract with explicit Limits.