einvoice_validator/pdf.rs
1//! Factur-X, `ZUGFeRD` and Order-X PDF files (feature `pdf`, on by default): the invoice
2//! XML a PDF/A-3 file embeds, extracted with [`einvoice_pdf`], to be validated as any
3//! other document.
4//!
5//! [`extract`] returns the embedded file named as the specifications require
6//! ([`INVOICE_FILE_NAMES`]), byte for byte, with its file specification
7//! (`/AFRelationship`, ...) and the XMP metadata of the document (the PDF/A
8//! identification, the Factur-X / `ZUGFeRD` properties). [`Syntax::detect`] then tells
9//! which built-in rule set the XML is for:
10//!
11//! ```
12//! # #[cfg(all(feature = "cen-ubl", feature = "cen-cii"))]
13//! fn validate(file: &[u8]) -> Result<einvoice_validator::Report, einvoice_validator::Error> {
14//! use einvoice_validator::{Syntax, Validator, pdf};
15//!
16//! let xml = if pdf::is_pdf(file) {
17//! let embedded = pdf::extract(file)?;
18//! println!("{} ({:?})", embedded.xml.file_name, embedded.xml.relationship);
19//! embedded.xml.data
20//! } else {
21//! file.to_vec()
22//! };
23//! let rule_set = Syntax::detect(&xml).map_or("cen-ubl", Syntax::builtin_rule_set);
24//! Validator::builtin(rule_set)?.validate_owned(xml)
25//! }
26//! ```
27//!
28//! Only the part of PDF on the path to the embedded files is read (see [`einvoice_pdf`]);
29//! anything else met there is refused rather than read approximately. The errors are
30//! [`Error::Pdf`].
31//!
32//! [`Syntax::detect`]: crate::Syntax::detect
33
34pub use einvoice_pdf::{
35 AfRelationship, Attachment, Embedded, INVOICE_FILE_NAMES, InvoiceMetadata, Limits, Metadata,
36};
37
38use crate::Error;
39
40/// The start of every PDF file (the header, ISO 32000-2, 7.5.2).
41const HEADER: &[u8] = b"%PDF-";
42
43/// Whether `bytes` start as a PDF file does (`%PDF-`), which no XML document does.
44#[must_use]
45pub fn is_pdf(bytes: &[u8]) -> bool {
46 bytes.starts_with(HEADER)
47}
48
49/// The embedded invoice XML of a Factur-X, `ZUGFeRD` or Order-X PDF, with its file
50/// specification and the document's XMP metadata, with the default [`Limits`].
51///
52/// # Errors
53///
54/// [`Error::Pdf`]: a file that is not a PDF, is malformed, encrypted, uses a PDF feature
55/// outside the subset read, exceeds a limit, has no embedded file with an invoice name or
56/// several, or has XMP metadata that is not well-formed.
57pub fn extract(pdf: &[u8]) -> Result<Embedded, Error> {
58 extract_with(pdf, &Limits::default())
59}
60
61/// [`extract`] with explicit [`Limits`].
62///
63/// # Errors
64///
65/// As [`extract`].
66pub fn extract_with(pdf: &[u8], limits: &Limits) -> Result<Embedded, Error> {
67 einvoice_pdf::extract_with(pdf, limits).map_err(Error::Pdf)
68}
69
70#[cfg(test)]
71mod tests {
72 use super::{extract, is_pdf};
73 use crate::{Error, ErrorKind};
74
75 #[test]
76 fn the_pdf_header_tells_a_pdf() {
77 assert!(is_pdf(b"%PDF-1.7\n"));
78 assert!(is_pdf(b"%PDF-"));
79 assert!(!is_pdf(b"<?xml version=\"1.0\"?><a/>"));
80 assert!(!is_pdf(b" %PDF-1.7"));
81 assert!(!is_pdf(b""));
82 }
83
84 #[test]
85 fn errors_are_pdf_errors_with_their_kind() {
86 let e = extract(b"%PDF-1.7\nnothing else").unwrap_err();
87 assert!(
88 matches!(e, Error::Pdf(einvoice_pdf::Error::Malformed { .. })),
89 "{e:?}"
90 );
91 assert_eq!(e.kind(), ErrorKind::Io);
92 assert!(e.to_string().starts_with("malformed PDF: "), "{e}");
93 let e = extract(b"<a/>").unwrap_err();
94 assert!(
95 matches!(e, Error::Pdf(einvoice_pdf::Error::NotPdf)),
96 "{e:?}"
97 );
98 assert_eq!(e.kind(), ErrorKind::Io);
99 }
100}