Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pull-model:
docker compose exec ollama ollama pull mistral

test:
docker compose exec app python3 -m pytest src/test/
docker compose exec app python3 -m pytest tests/

clean:
docker compose down -v
Expand Down
28 changes: 26 additions & 2 deletions src/filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,32 @@ def fill_form(self, pdf_form: str, llm: LLM):
for annot in sorted_annots:
if annot.Subtype == "/Widget" and annot.T:
if i < len(answers_list):
annot.V = f"{answers_list[i]}"
annot.AP = None
answer = answers_list[i]

# Check if the field type is a Button (Checkbox/Radio)
field_type = annot.FT if annot.FT else (annot.Parent.FT if annot.Parent else None)
if str(field_type) == "/Btn":
is_truthy = str(answer).lower() in ["yes", "true", "1", "x", "on"]

# Find the 'ON' state from the appearance dictionary
on_state = "/Yes" # Default assumption
if annot.AP and annot.AP.N:
keys = [k for k in annot.AP.N.keys() if k != "/Off"]
if keys:
on_state = keys[0]

if is_truthy:
from pdfrw import PdfName
annot.V = PdfName(on_state.strip("/"))
annot.AS = PdfName(on_state.strip("/"))
else:
from pdfrw import PdfName
annot.V = PdfName("Off")
annot.AS = PdfName("Off")
else:
annot.V = f"{answer}"
annot.AP = None

i += 1
else:
# Stop if we run out of answers
Expand Down
8 changes: 5 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from typing import Union
# from backend import Fill
from commonforms import prepare_form
from pypdf import PdfReader
Expand Down Expand Up @@ -30,10 +31,11 @@ def run_pdf_fill_process(user_input: str, definitions: list, pdf_form_path: Unio

print("[3] Starting extraction and PDF filling process...")
try:
output_name = Fill.fill_form(
controller = Controller()
output_name = controller.fill_form(
user_input=user_input,
definitions=definitions,
pdf_form=pdf_form_path
fields=definitions,
pdf_form_path=pdf_form_path
)

print("\n----------------------------------")
Expand Down