mod catalog;
mod document;
mod error;
mod filter;
mod md5;
mod object;
mod parser;
mod text;
mod xmp;
mod xref;
use std::fmt;
pub use error::{Error, Location};
use document::Document;
pub const INVOICE_FILE_NAMES: [&str; 5] = [
"factur-x.xml",
"zugferd-invoice.xml",
"ZUGFeRD-invoice.xml",
"xrechnung.xml",
"order-x.xml",
];
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Limits {
pub max_depth: usize,
pub max_resolution_depth: usize,
pub max_xref_sections: usize,
pub max_name_tree_depth: usize,
pub max_attachments: usize,
pub max_stream_size: usize,
pub max_total_decoded: usize,
pub max_xmp_nodes: u32,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_depth: 64,
max_resolution_depth: 32,
max_xref_sections: 1024,
max_name_tree_depth: 32,
max_attachments: 10_000,
max_stream_size: 64 << 20,
max_total_decoded: 256 << 20,
max_xmp_nodes: 1_000_000,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum AfRelationship {
Source,
Data,
Alternative,
Supplement,
EncryptedPayload,
FormData,
Schema,
Unspecified,
Other(String),
}
impl AfRelationship {
fn from_name(name: &[u8]) -> Self {
match name {
b"Source" => Self::Source,
b"Data" => Self::Data,
b"Alternative" => Self::Alternative,
b"Supplement" => Self::Supplement,
b"EncryptedPayload" => Self::EncryptedPayload,
b"FormData" => Self::FormData,
b"Schema" => Self::Schema,
b"Unspecified" => Self::Unspecified,
other => Self::Other(String::from_utf8_lossy(other).into_owned()),
}
}
#[must_use]
pub fn as_str(&self) -> &str {
match self {
Self::Source => "Source",
Self::Data => "Data",
Self::Alternative => "Alternative",
Self::Supplement => "Supplement",
Self::EncryptedPayload => "EncryptedPayload",
Self::FormData => "FormData",
Self::Schema => "Schema",
Self::Unspecified => "Unspecified",
Self::Other(name) => name,
}
}
}
impl fmt::Display for AfRelationship {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Attachment {
pub file_name: String,
pub key: Option<String>,
pub in_associated_files: bool,
pub relationship: Option<AfRelationship>,
pub description: Option<String>,
pub mime_type: Option<String>,
pub size: Option<u64>,
pub checksum: Option<[u8; 16]>,
pub creation_date: Option<String>,
pub modification_date: Option<String>,
pub data: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct InvoiceMetadata {
pub namespace: String,
pub document_type: Option<String>,
pub document_file_name: Option<String>,
pub version: Option<String>,
pub conformance_level: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Metadata {
pub xmp: String,
pub pdfa_part: Option<String>,
pub pdfa_conformance: Option<String>,
pub invoice: Option<InvoiceMetadata>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Embedded {
pub xml: Attachment,
pub metadata: Option<Metadata>,
pub pdf_version: String,
}
pub fn extract(pdf: &[u8]) -> Result<Embedded, Error> {
extract_with(pdf, &Limits::default())
}
pub fn extract_with(pdf: &[u8], limits: &Limits) -> Result<Embedded, Error> {
let mut doc = Document::load(pdf, limits)?;
let catalog = catalog::catalog(&mut doc)?;
let object::Object::Dict(catalog) = &*catalog else {
return Err(Error::malformed(
None,
"the document catalog is not a dictionary",
));
};
let specs = catalog::file_specs(&mut doc, catalog)?;
let candidates: Vec<&catalog::FileSpec> = specs
.iter()
.filter(|spec| INVOICE_FILE_NAMES.contains(&spec.file_name.as_str()))
.collect();
let spec = match candidates.as_slice() {
[] => {
return Err(Error::NoInvoice {
attachments: specs.iter().map(|s| s.file_name.clone()).collect(),
});
}
[spec] => *spec,
several => {
return Err(Error::Ambiguous {
names: several.iter().map(|s| s.file_name.clone()).collect(),
});
}
};
let xml = catalog::attachment(&mut doc, spec)?;
let metadata = catalog::metadata_stream(&mut doc, catalog)?
.map(|bytes| xmp::parse(bytes, limits.max_xmp_nodes))
.transpose()?;
Ok(Embedded {
xml,
metadata,
pdf_version: doc.version.clone(),
})
}
pub fn attachments(pdf: &[u8]) -> Result<Vec<Attachment>, Error> {
attachments_with(pdf, &Limits::default())
}
pub fn attachments_with(pdf: &[u8], limits: &Limits) -> Result<Vec<Attachment>, Error> {
let mut doc = Document::load(pdf, limits)?;
let catalog = catalog::catalog(&mut doc)?;
let object::Object::Dict(catalog) = &*catalog else {
return Err(Error::malformed(
None,
"the document catalog is not a dictionary",
));
};
let specs = catalog::file_specs(&mut doc, catalog)?;
specs
.iter()
.map(|spec| catalog::attachment(&mut doc, spec))
.collect()
}