Skip to main content

Crate pdf_forms

Crate pdf_forms 

Source
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

TypePurpose
PdfDocumentCore document handle — open, to_bytes, page_count, page_size
SanitizerStrip JS and external actions from uploaded PDFs
AcroFormFillerInspect, fill, and optionally flatten AcroForm fields
OverlayCompositeComposite pre-rendered PDF pages or images onto pages
SignatureStampEmbed PNG/JPEG as a visual signature Image XObject
PdfErrorAll 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§

dictionary

Structs§

Bookmark
Destination
Dictionary
Dictionary object.
FontData
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
IncrementalDocument
LoadOptions
Options for loading PDF documents.
ObjectStream
ObjectStreamBuilder
ObjectStreamConfig
PageSize
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.
SaveOptions
Options for saving PDF documents
SaveOptionsBuilder
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.
StringFormat
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 str to 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 str to 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§

FilterFunc
Type alias for the filter function used during PDF loading.
ObjectId
Object identifier consists of two parts: object number and generation number.