Skip to main content

High-level PDF form manipulation library - Python bindings

Project description

acroform

A high-level PDF form manipulation library using lopdf.

This crate provides a simple API for reading and filling PDF forms (AcroForms). It uses the official lopdf crate for PDF operations.

Features

  • Load PDF documents from files or bytes
  • List all form fields with their properties
  • Fill form fields with typed values
  • Save filled PDFs to files or bytes
  • Support for text, boolean, choice, and integer field types
  • Automatic UTF-16BE encoding for text fields
  • Hierarchical field name resolution

Usage

Add this to your Cargo.toml:

[dependencies]
acroform = "0.2.0"

Example

use acroform::{AcroFormDocument, FieldValue};
use std::collections::HashMap;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load a PDF with form fields
    let mut doc = AcroFormDocument::from_pdf("form.pdf")?;

    // List all fields
    let fields = doc.fields()?;
    for field in &fields {
        println!("Field: {} ({})", field.name, field.field_type);
    }

    // Fill fields
    let mut values = HashMap::new();
    values.insert("name".to_string(), FieldValue::Text("John Doe".to_string()));
    values.insert("age".to_string(), FieldValue::Integer(30));
    values.insert("subscribe".to_string(), FieldValue::Boolean(true));

    // Save filled PDF
    doc.fill_and_save(values, "filled_form.pdf")?;
    
    Ok(())
}

API

AcroFormDocument

The main struct for working with PDF forms.

Methods

  • from_pdf(path: impl AsRef<Path>) -> Result<Self> - Load a PDF from a file path
  • from_bytes(data: Vec<u8>) -> Result<Self> - Load a PDF from bytes
  • fields(&self) -> Result<Vec<FormField>> - Get all form fields
  • fill(&mut self, values: HashMap<String, FieldValue>) -> Result<Vec<u8>> - Fill fields and return PDF bytes
  • fill_and_save(&mut self, values: HashMap<String, FieldValue>, output: impl AsRef<Path>) -> Result<()> - Fill fields and save to file

FormField

Represents a form field with its properties.

Fields

  • name: String - The fully qualified field name (e.g., "parent.child.field")
  • field_type: FieldType - The type of the field
  • current_value: Option<FieldValue> - The current value
  • default_value: Option<FieldValue> - The default value
  • flags: u32 - Field flags (bit field)
  • tooltip: Option<String> - Tooltip text

FieldValue

Typed values for form fields.

Variants

  • Text(String) - Text string
  • Boolean(bool) - Boolean value (for checkboxes)
  • Choice(String) - Choice value (for radio buttons, list boxes, combo boxes)
  • Integer(i32) - Integer value

FieldType

PDF form field types.

Variants

  • Text - Text field (/Tx)
  • Button - Button field (/Btn) - includes checkboxes, radio buttons, and push buttons
  • Choice - Choice field (/Ch) - includes list boxes and combo boxes
  • Signature - Signature field (/Sig)
  • Unknown(String) - Unknown or custom field type

Implementation Details

String Encoding

Text fields are automatically encoded as UTF-16BE with BOM when filling forms, which ensures proper Unicode support across PDF viewers.

Field Hierarchy

Fields can be organized hierarchically in PDF forms. This library constructs fully qualified field names using dot notation (e.g., "parent.child.field").

Appearance Updates

When filling forms, the library sets the NeedAppearances flag, which tells PDF viewers to regenerate field appearances. This ensures that filled values are properly displayed.

Python Bindings

High-performance Python bindings are available:

pip install acroform

Requires Python 3.12 or later.

import acroform

# Load a PDF form
doc = acroform.AcroFormDocument.from_pdf("form.pdf")

# List all fields
fields = doc.fields()
for field in fields:
    print(f"Field: {field.name} ({field.field_type})")

# Fill fields and save
doc.fill_and_save({"name": "John Doe"}, "filled_form.pdf")

See PYTHON_BINDINGS.md for detailed documentation.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

acroform-0.2.0.tar.gz (163.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

acroform-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (505.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file acroform-0.2.0.tar.gz.

File metadata

  • Download URL: acroform-0.2.0.tar.gz
  • Upload date:
  • Size: 163.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for acroform-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fbe75dc1bf11241e83370745cd3e199d855584079c735be84d0c5eb02742b22b
MD5 261b3048c9d9f325371751a2a0f764f3
BLAKE2b-256 ae998a66e680bff4a1035d2abf54b09960c9d5638e316b02fd397109b65cc599

See more details on using hashes here.

File details

Details for the file acroform-0.2.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for acroform-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6a918106f634164477c2969c017b8694442e549bbcf50bf2789598085332b66d
MD5 1fcaf9bf9f2463165170498bfae58b83
BLAKE2b-256 b1696fecdcff3dca71403f5ef12bbbb34df1bcdf590237fea3cb7b321545affd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page