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.2.tar.gz (165.1 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.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (567.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

acroform-0.2.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (536.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

acroform-0.2.2-cp313-cp313-macosx_11_0_arm64.whl (546.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

acroform-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (567.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

acroform-0.2.2-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.2-cp312-cp312-macosx_11_0_arm64.whl (546.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

acroform-0.2.2-cp311-cp311-macosx_11_0_arm64.whl (499.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for acroform-0.2.2.tar.gz
Algorithm Hash digest
SHA256 54727d14c140b8ac839f197dbf616cd27f96e1c6da626f51c6cddaf5dd279190
MD5 628247a336edd18ed5de8543240f8343
BLAKE2b-256 6debe3666e45347a0544a266f760a7c7a2a4f5681968483b96890699f008d2f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for acroform-0.2.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b4fd625dc9787a140da6d857d6235c026576c237e90963c51f2938640f31b2c8
MD5 8e13d5421fb72428fa0a31b852ecda14
BLAKE2b-256 53de360a91af3c3f6127f0b3dc1d5ac24182713ef70ffe5cfdd8264724aafd19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for acroform-0.2.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7db8633fa2e25bc3a48ab92560db38b920125bc9ed6d289a019e310c94f195e3
MD5 bd9ce4409f7df4ec17f07945dc74207d
BLAKE2b-256 3b545648873de2b0ed1eed9e6e40469c5b69fcddc08d4c61d9348174cc4e5806

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for acroform-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bc4842ecefa49407b3ac0a97c47ba620deb7602928e597e9f87e1540b01ba0e5
MD5 0d2ae16d9f09a0e14389567cae3dbec2
BLAKE2b-256 4b2a3852a878dd20044bfccb3f595d2bb1ef3038c060016f1fabc52e1fd2d136

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for acroform-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9dad1ad06fa03525b4511ee0ef79fa5a22973fa48d0c8aaca9a5e0e8545d9659
MD5 8eba459d25f9084ec7fbd3cd3312ca4b
BLAKE2b-256 9b127011f3500c3809195446b79f905b36027f4e6e643463d2e1eaa2a779ea30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for acroform-0.2.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 177d9fa0a5832b8de0d5d2d14d67a554cdb6ea394bd812ae23261d3b19516d56
MD5 8f72016403623ab9958b548abe49031a
BLAKE2b-256 df9fb380f10ab96b3c189f8b23169ee9b4ceb3ae63843fb4a9e24db236ce7592

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for acroform-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9f3fbba7131bd9965fd3e00f492fd28372b757d669d1c7db671d48c9ef4a65f0
MD5 2af271331c09cbc729098d5404f207cb
BLAKE2b-256 fb646a606d65114f6a5efbe0ed305658062b0b9ba1a948f7407c08f9a6a49c32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for acroform-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e3a5e9c007fa8d659766dfcd372adbd57897e914e598dc7d1b4f0263bfbecfaa
MD5 d628261ff8fbcd28808614db72cb8b8a
BLAKE2b-256 597c69789287214053af86b29505f9cd908ee7fe55fc58e1b4f8b1684ec72d60

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