Convert interactive form fields into static page content. Flattening bakes the current field values into the page so they cannot be edited.
use pdfluent::PdfDocument;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("filled_form.pdf")?;
doc.flatten_form()?;
doc.save("flattened.pdf")?;
println!("Form fields removed, values baked into page content.");
Ok(())
}Open the document that has form fields with values already set. Flattening works on whatever values are currently in the fields.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("filled_application.pdf")?;Call flatten_form() to convert every field into static page content. The AcroForm dictionary is removed from the document.
doc.flatten_form()?;To flatten a subset of fields while leaving others editable, use flatten_fields() with a list of field names.
doc.flatten_fields(&["first_name", "last_name", "date_signed"])?;After flattening, doc.has_form() returns false and the page text includes the values that were in the fields.
assert!(!doc.has_form());
let text = doc.page(0)?.extract_text()?;
assert!(text.contains("Jane Smith"));Save to a new file to keep the original editable version intact.
doc.save("application_final.pdf")?;No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.