Expand description
§pdf-forms
Filling PDF forms in Rust has no ergonomic answer. pdf-forms fixes that.
A high-level Rust library for PDF form manipulation — built and maintained by FastYoke. Forked from lopdf at v0.42.0, with a clean builder API layered on top.
§Features
- Fill AcroForms — text, checkbox, radio, and choice fields
- Flatten — convert interactive fields to static page content
- Composite overlays — embed pre-rendered PDF pages or images as Form/Image XObjects
- Stamp signatures — embed PNG/JPEG images as visual signature marks
- Sanitize — strip JavaScript and external actions from uploaded PDFs
§Quick start
Add to Cargo.toml:
[dependencies]
fastyoke-pdf-forms = "0.1"use pdf_forms::{AcroFormFiller, FieldMap, FieldValue, PdfDocument, Rect, Sanitizer, SignatureStamp};
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = std::fs::read("form.pdf")?;
let mut doc = PdfDocument::open(&input)?;
// Strip JavaScript from the uploaded PDF
Sanitizer::new().apply(&mut doc)?;
// Inspect fields and fill them
let fields = AcroFormFiller::inspect(&doc)?;
let mut map: FieldMap = HashMap::new();
map.insert("FirstName".into(), FieldValue::Text("Ada".into()));
map.insert("Agree".into(), FieldValue::Checkbox(true));
// Fill and flatten (converts widgets to static content)
AcroFormFiller::new(&map).flatten(true).apply(&mut doc)?;
// Embed a signature image
let sig_png = std::fs::read("signature.png")?;
SignatureStamp::new(sig_png)
.at_page(1)
.at_rect(Rect { x: 400.0, y: 50.0, width: 150.0, height: 60.0 })
.apply(&mut doc)?;
std::fs::write("filled.pdf", doc.to_bytes()?)?;
Ok(())
}§API
| Type | Purpose |
|---|---|
PdfDocument | Core document handle — open, to_bytes, page_count, page_size |
Sanitizer | Strip JS and external actions from uploaded PDFs |
AcroFormFiller | Inspect, fill, and optionally flatten AcroForm fields |
OverlayComposite | Composite pre-rendered PDF pages or images onto pages |
SignatureStamp | Embed PNG/JPEG as a visual signature Image XObject |
PdfError | All errors returned by the public API (#[non_exhaustive]) |
§Attribution
pdf-forms is a fork of lopdf by
Junfeng Liu and contributors, used under the MIT license. The lopdf source is
vendored and maintained as part of this crate.
§License
MIT — see LICENSE.
Re-exports§
pub use acroform::AcroField;pub use acroform::AcroFont;pub use acroform::AcroFormFiller;pub use acroform::FieldMap;pub use acroform::FieldValue;pub use acroform::WidgetType;pub use overlay::OverlayComposite;pub use overlay::OverlayContent;pub use overlay::OverlayRegion;pub use signature::SignatureStamp;pub use encryption::EncryptionState;pub use encryption::EncryptionVersion;pub use encryption::Permissions;pub use sanitize::Sanitizer;
Modules§
- acroform
- AcroForm fill and flatten builder.
- content
- encryption
- filters
- overlay
- sanitize
- Pre-intake PDF sanitizer.
- signature
- xobject
- xref
Macros§
Structs§
- Bookmark
- Destination
- Dictionary
- Dictionary object.
- Font
Data - This struct represents the data of a font. It contains information about the font’s bounding box, ascent, descent, cap height, italic angle, and stemV. Reference: https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/pdfreference1.5_v6.pdf
- Incremental
Document - Load
Options - Options for loading PDF documents.
- Object
Stream - Object
Stream Builder - Object
Stream Config - Page
Size - Page dimensions in PDF user units (points; 1pt = 1/72 inch). Origin is at the bottom-left of the page.
- PdfDocument
- A parsed PDF document. All pdf-forms operations work through this type.
- PdfMetadata
- PDF metadata extracted without loading the entire document. This is useful for quickly getting basic information about large PDFs.
- Reader
- Rect
- An axis-aligned rectangle in PDF user units. Origin is at the bottom-left of the page.
- Save
Options - Options for saving PDF documents
- Save
Options Builder - Builder for SaveOptions
- Stream
- Stream object Warning - all streams must be indirect objects, while the stream dictionary may be a direct object
- Toc
Enums§
- Encoding
- Object
- Basic PDF object types defined in an enum.
- Outline
- PdfError
- All errors returned by the pdf-forms public API.
- String
Format - String objects can be written in two formats.
Functions§
- decode_
text_ string - Decodes a text string. Depending on the BOM at the start of the string, a different encoding is chosen. All encodings specified in PDF2.0 are supported (PDFDocEncoding, UTF-16BE, and UTF-8).
- encode_
utf8 - Encodes the given
strto UTF-8. This method of encoding text strings is first specified in PDF2.0 and reader support is still lacking (notably, Adobe Acrobat Reader doesn’t support it at the time of writing). Thus, using it is NOT RECOMMENDED. - encode_
utf16_ be - Encodes the given
strto UTF-16BE. The recommended way to encode text strings, as it supports all of unicode and all major PDF readers support it. - substr
- substring
- text_
string - Creates a text string. If the input only contains ASCII characters, the string is encoded in PDFDocEncoding, otherwise in UTF-16BE.
Type Aliases§
- Filter
Func - Type alias for the filter function used during PDF loading.
- Object
Id - Object identifier consists of two parts: object number and generation number.