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.
extractreturns 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 /ZUGFeRDproperties (DocumentType,DocumentFileName,Version,ConformanceLevel).attachmentsreturns 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
extractreturns. - Invoice
Metadata - 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::LimitExceedednaming 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
ZUGFeRD2.1 and later),ZUGFeRD2.0,ZUGFeRD1.0,XRechnunginZUGFeRD, Order-X.
Functions§
- attachments
- Every embedded file of a PDF, with the default
Limits: those of the/EmbeddedFilesname tree in key order, then those of the catalog’s/AFnot already listed. - attachments_
with attachmentswith explicitLimits.- extract
- The embedded invoice XML of a Factur-X,
ZUGFeRDor Order-X PDF, with the defaultLimits. - extract_
with extractwith explicitLimits.