Replace placeholder text, update document dates, or redact strings across all pages of a PDF.
use pdfluent::{PdfDocument, TextReplacement};
fn main() -> pdfluent::Result<()> {
let mut doc = PdfDocument::open("template.pdf")?;
doc.replace_text_all(&[
TextReplacement::exact("{{CUSTOMER_NAME}}", "Acme Corp"),
TextReplacement::exact("{{INVOICE_DATE}}", "2024-04-01"),
TextReplacement::exact("{{TOTAL}}", "EUR 4,200.00"),
])?;
doc.save("invoice-filled.pdf")?;
Ok(())
}Text replacement is in the base crate.
# Cargo.toml
[dependencies]
pdfluent = "0.9"The source is typically a template PDF with placeholder strings. Load it as normal.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("template.pdf")?;TextReplacement::exact() matches the literal string on any page. Matching is case-sensitive by default.
use pdfluent::TextReplacement;
doc.replace_text_all(&[
TextReplacement::exact("{{CUSTOMER_NAME}}", "Acme Corp"),
TextReplacement::exact("{{INVOICE_DATE}}", "2024-04-01"),
TextReplacement::exact("{{TOTAL}}", "EUR 4,200.00"),
])?;TextReplacement::regex() accepts any regex pattern. Capture groups are supported in the replacement string.
use pdfluent::TextReplacement;
// Replace phone numbers with a redacted placeholder
doc.replace_text_all(&[
TextReplacement::regex(
r"\+?\d[\d\s\-]{8,14}\d",
"[PHONE REDACTED]",
)?,
// Update year in date strings
TextReplacement::regex(
r"2023-(\d{2}-\d{2})",
"2024-$1",
)?,
])?;Use page_mut(n).replace_text() to limit replacement to one page.
let mut page = doc.page_mut(0)?;
let count = page.replace_text(
TextReplacement::exact("DRAFT", "FINAL"),
)?;
println!("Replaced {} occurrence(s) on page 1", count);
doc.save("invoice-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.