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.1.tar.gz (164.7 kB view details)

Uploaded Source

Built Distributions

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

acroform-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (566.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

acroform-0.2.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (536.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

acroform-0.2.1-cp313-cp313-macosx_11_0_arm64.whl (546.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

acroform-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (567.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

acroform-0.2.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (536.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

acroform-0.2.1-cp312-cp312-macosx_11_0_arm64.whl (546.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

acroform-0.2.1-cp311-cp311-macosx_11_0_arm64.whl (499.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for acroform-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5de038a53a06bc9bfc2487e8d1c2a81767c73d841d1ece220d3e2339cec50ce7
MD5 fe4da81c03e9f516fb72e4b53b5142de
BLAKE2b-256 d0dd27bf5c083c6b0a924a358c849852ec95c46d4dfc62b2b5e702ba9a5c9f6d

See more details on using hashes here.

File details

Details for the file acroform-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for acroform-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5868eeb37f8673979f8d05b3784842fea3346e8832a63360c5bb4417f153ffe7
MD5 9b134f791e7402256e2e139f6c542d55
BLAKE2b-256 a8f6132c914ded1b6a18e04a9b0ce3160217667a8fa8c1e434a91c0101393c58

See more details on using hashes here.

File details

Details for the file acroform-0.2.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for acroform-0.2.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7a7154bd1b7ed2549484a144771b895a1a007530d1c5230b1c7578edd027e5d2
MD5 b76942d5c576f152ef7e96069da23a0d
BLAKE2b-256 cbb3524e8cfd8710f8b5781026f2118601c384a2d49d4ce28b44366a22289aa7

See more details on using hashes here.

File details

Details for the file acroform-0.2.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for acroform-0.2.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2e1ba96c1cda4fc61a5eb75f04750011f2be607282cccae6f21e4d581cb0209c
MD5 8f5c55aa5146d97d82cbb2dbc4354c54
BLAKE2b-256 d8362fc2dd1939d51eb9554f2ebeaef3183ffa6300c1ccfe527340c6a52e697e

See more details on using hashes here.

File details

Details for the file acroform-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for acroform-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5590100aef6ec274f08af486b04bc1c56ee92bcea3bb9b01003f8019d443bc6d
MD5 64d886ef2a1d73e33a3c3c931bbf497c
BLAKE2b-256 304cf61bd76616dd7972a2cd682afdfacdef6d816c04996582a8f5988e052f29

See more details on using hashes here.

File details

Details for the file acroform-0.2.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for acroform-0.2.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d4fb28f77325495f698f85cb05e7501cad6f2eaac4afb193570e0506a39d0705
MD5 b6a2bbbfc8e9aa0769b1dfef95e3b932
BLAKE2b-256 25c7bd9ddc2b44e0fefa1a4d5c04c45d9df9b7523c5a2e712a6993c897f82a23

See more details on using hashes here.

File details

Details for the file acroform-0.2.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for acroform-0.2.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 62b7bbf4b0857c603fa7f1079022e74025467fbcc8817bd017430642ba87a680
MD5 a702aee9658f92407ad81d1c75e82649
BLAKE2b-256 c1e95912442b6d49deb6e0009d0c166cc895450764b18ff643df0bd5e0895eb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for acroform-0.2.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e26a23c68eda5beafa8c73dc767a3b47b012a5b03427283a3e7c17e0f97a42c7
MD5 ae7759d1fc5d7ae87b3a6b05296f6f5f
BLAKE2b-256 51bd580b2360ccbaeee18d2d4add5d2250117207240d05cfb01ad2d2fce62be7

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