Create PDF portfolios containing embedded files. Extract attachments from existing documents. Handle PDF/A-3 with embedded XML invoices.
use pdfluent::{Sdk, portfolio::{PortfolioOptions, EmbeddedFile}};
fn main() -> pdfluent::Result<()> {
let sdk = Sdk::new()?;
let mut doc = sdk.open("invoice.pdf")?;
// Attach a ZUGFeRD XML file for PDF/A-3 e-invoicing
let xml_bytes = std::fs::read("zugferd_invoice.xml")?;
let attachment = EmbeddedFile::builder()
.name("ZUGFeRD-invoice.xml")
.mime_type("application/xml")
.description("ZUGFeRD 2.1 invoice data")
.relationship(pdfluent::portfolio::Relationship::Alternative)
.data(xml_bytes)
.build();
let opts = PortfolioOptions::builder()
.add(PdfDocument::open(attachment)?)
.conform_to_pdfa3b(true)
.build();
let output = doc.embed_files(opts)?;
output.save("invoice_pdfa3.pdf")?;
println!("Attachments: {}", output.attachment_count());
Ok(())
}Run cargo add pdfluent@1.0.0-beta.5 to get started.
Attach any file type (XML, JSON, CSV, images, Office documents) to a PDF as an embedded file stream. Each attachment carries its name, MIME type, description, modification date, and a checksum stored in the file specification dictionary.
Build a PDF Portfolio (PDF 1.7 collection) with the Navigator layout. Each embedded file appears as a card in the portfolio view. Set display columns, sort order, and cover document. Compatible with Adobe Acrobat and other portfolio-aware viewers.
Set and read custom metadata on each attachment: description, creation date, modification date, MIME type, and relationship (Source, Alternative, Supplement, Unspecified). Metadata is stored in the EmbeddedFile stream dictionary per ISO 32000.
Produce PDF/A-3b documents with embedded XML invoices conforming to ZUGFeRD 2.1 and Factur-X. The attachment relationship is set to Alternative, the file is listed in the AF array, and the document-level XMP metadata includes the required conformance declarations.
Extract all embedded files from any PDF in one call. Returns a Vec of attachment handles, each with its name, MIME type, and raw bytes. Useful for processing incoming PDF/A-3 invoices to extract the machine-readable XML.
List all attachments in a document without extracting the data. Returns name, size, MIME type, and modification date for each file. Use this to check whether a PDF contains an embedded XML invoice before committing to a full extraction.