Objective
Extract and store structured data from PDF forms with field mapping and validation. This enables automating form processing, data entry, and document digitization workflows.
Step 1: Create Form Templates Table
Create a table for form templates.
CREATE TABLE form_templates (
id INTEGER PRIMARY KEY,
template_name VARCHAR(255) NOT NULL,
form_type VARCHAR(50) NOT NULL,
version VARCHAR(20) DEFAULT '1.0',
field_count INTEGER DEFAULT 0,
description TEXT,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Create Form Fields Table
Define expected form fields.
CREATE TABLE form_fields (
id INTEGER PRIMARY KEY,
template_id INTEGER NOT NULL,
field_name VARCHAR(100) NOT NULL,
field_label VARCHAR(255),
field_type VARCHAR(50) NOT NULL,
is_required BOOLEAN DEFAULT FALSE,
validation_pattern VARCHAR(255),
default_value VARCHAR(255),
field_order INTEGER,
FOREIGN KEY (template_id) REFERENCES form_templates(id)
);
Step 3: Create Submitted Forms Table
Store submitted form data.
CREATE TABLE submitted_forms (
id INTEGER PRIMARY KEY,
template_id INTEGER NOT NULL,
form_pdf PDF,
submission_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
submitter_name VARCHAR(255),
submitter_email VARCHAR(255),
extraction_status VARCHAR(20) DEFAULT 'pending',
validation_status VARCHAR(20) DEFAULT 'pending',
validation_errors INTEGER DEFAULT 0,
processed_by VARCHAR(100),
processed_at TIMESTAMP,
FOREIGN KEY (template_id) REFERENCES form_templates(id)
);
Step 4: Create Extracted Values Table
Store extracted field values.
CREATE TABLE extracted_values (
id INTEGER PRIMARY KEY,
form_id INTEGER NOT NULL,
field_id INTEGER NOT NULL,
extracted_value TEXT,
confidence_score DECIMAL(5, 4),
is_valid BOOLEAN,
validation_message VARCHAR(255),
manually_corrected BOOLEAN DEFAULT FALSE,
corrected_value TEXT,
FOREIGN KEY (form_id) REFERENCES submitted_forms(id),
FOREIGN KEY (field_id) REFERENCES form_fields(id)
);
Step 5: Insert Form Templates
Add sample form templates.
INSERT INTO form_templates (id, template_name, form_type, version, field_count, description) VALUES
(1, 'Employee Onboarding Form', 'hr', '2.0', 12, 'New employee information collection form'),
(2, 'Expense Report Form', 'finance', '1.5', 10, 'Employee expense reimbursement request'),
(3, 'Customer Registration Form', 'customer', '1.0', 8, 'New customer account registration'),
(4, 'Leave Request Form', 'hr', '1.2', 6, 'Employee time off request form'),
(5, 'Vendor Registration Form', 'procurement', '1.0', 15, 'New vendor onboarding form');
Step 6: Insert Form Fields
Define fields for templates.
INSERT INTO form_fields (id, template_id, field_name, field_label, field_type, is_required, validation_pattern, field_order) VALUES
-- Employee Onboarding Form fields
(1, 1, 'full_name', 'Full Legal Name', 'text', TRUE, NULL, 1),
(2, 1, 'email', 'Email Address', 'email', TRUE, '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$', 2),
(3, 1, 'phone', 'Phone Number', 'phone', TRUE, '^[0-9]{10}$', 3),
(4, 1, 'ssn', 'Social Security Number', 'ssn', TRUE, '^[0-9]{3}-[0-9]{2}-[0-9]{4}$', 4),
(5, 1, 'start_date', 'Start Date', 'date', TRUE, NULL, 5),
(6, 1, 'department', 'Department', 'text', TRUE, NULL, 6),
(7, 1, 'job_title', 'Job Title', 'text', TRUE, NULL, 7),
(8, 1, 'manager_name', 'Manager Name', 'text', FALSE, NULL, 8),
-- Expense Report Form fields
(9, 2, 'employee_name', 'Employee Name', 'text', TRUE, NULL, 1),
(10, 2, 'employee_id', 'Employee ID', 'text', TRUE, '^EMP[0-9]{5}$', 2),
(11, 2, 'expense_date', 'Expense Date', 'date', TRUE, NULL, 3),
(12, 2, 'expense_type', 'Expense Type', 'select', TRUE, NULL, 4),
(13, 2, 'amount', 'Amount', 'currency', TRUE, '^[0-9]+\\.?[0-9]{0,2}$', 5),
(14, 2, 'description', 'Description', 'text', TRUE, NULL, 6),
-- Leave Request Form fields
(15, 4, 'employee_name', 'Employee Name', 'text', TRUE, NULL, 1),
(16, 4, 'leave_type', 'Leave Type', 'select', TRUE, NULL, 2),
(17, 4, 'start_date', 'Start Date', 'date', TRUE, NULL, 3),
(18, 4, 'end_date', 'End Date', 'date', TRUE, NULL, 4),
(19, 4, 'total_days', 'Total Days', 'number', TRUE, NULL, 5),
(20, 4, 'reason', 'Reason', 'text', FALSE, NULL, 6);