Skip to main content

pdf_engine/
xfa.rs

1//! XFA feature surface for `pdf-engine`.
2//!
3//! This module is compiled only with the `xfa` feature and re-exports the
4//! high-level helpers needed to extract and flatten XFA content without making
5//! the base crate depend on the XFA stack by default.
6
7use crate::PdfDocument;
8
9pub use pdf_xfa::error::XfaError;
10pub use pdf_xfa::extract::XfaPackets;
11pub use xfa_json::{export_schema, form_tree_to_json, form_tree_to_value, FormData, FormSchema};
12pub use xfa_layout_engine::form::{FormNodeId, FormTree};
13
14/// Extract the XFA packets embedded in a document.
15pub fn extract_packets(document: &PdfDocument) -> Result<XfaPackets, XfaError> {
16    pdf_xfa::extract::extract_xfa(document.pdf())
17}
18
19/// Returns `true` if the document has an XFA template packet.
20///
21/// Only an active `/AcroForm` `/XFA` entry counts here. Orphaned XFA streams
22/// left behind after flattening should not trigger another flatten/render pass.
23/// XFA PDFs without a template are treated as non-XFA (they have no content to
24/// flatten), so this returns `false` for those documents.
25pub fn has_xfa(document: &PdfDocument) -> bool {
26    pdf_xfa::extract::extract_xfa_from_acroform(document.pdf())
27        .is_some_and(|packets| packets.template().is_some())
28}
29
30/// Flatten an XFA document into static PDF bytes.
31pub fn flatten(document: &PdfDocument) -> Result<Vec<u8>, XfaError> {
32    pdf_xfa::flatten_xfa_to_pdf(document.pdf().data().as_ref())
33}