Skip to content

adding pdf script to fill out prior auth info#33

Merged
rsalus merged 2 commits intomainfrom
pdf_script
Feb 25, 2026
Merged

adding pdf script to fill out prior auth info#33
rsalus merged 2 commits intomainfrom
pdf_script

Conversation

@itsannbat
Copy link
Collaborator

Title, example is seen in pdf-templates and instructions on how to run the script are in the README

@coderabbitai
Copy link

coderabbitai bot commented Feb 23, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a5e572c and 45b4854.

📒 Files selected for processing (2)
  • scripts/README-fill-pdf.md
  • scripts/fill_pdf_form.py

📝 Walkthrough

Walkthrough

Adds a new PDF form-filling script and companion README. The script autodetects PyMuPDF or pypdf, maps mock patient and prescription data to form fields (including checkboxes and signature), and writes a filled PDF; the README documents installation and usage steps.

Changes

Cohort / File(s) Summary
Script: PDF form filler
scripts/fill_pdf_form.py
New Python script that autodetects PyMuPDF (preferred) or pypdf (fallback), defines PATIENT_DATA, includes format_date, fill_pdf_with_pymupdf (full field & checkbox mapping, error handling, save), a pypdf stub, and main() orchestration with input validation and messages.
Documentation
scripts/README-fill-pdf.md
New README describing the Jane Doe form-filling workflow, required libraries (PyMuPDF or pypdf), install/usage instructions, expected input/output behavior, and alternative/manual filling notes.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant Script as fill_pdf_form.py
    participant Detector as LibraryDetector
    participant PyMuPDF as PyMuPDF/fitz
    participant pypdf as pypdf
    participant Template as PDF Template
    participant Output as Output PDF

    User->>Script: run script with input/output paths
    Script->>Detector: check for PyMuPDF / pypdf

    alt PyMuPDF available
        Detector-->>Script: PyMuPDF found
        Script->>Script: load PATIENT_DATA
        Script->>Template: open template
        Script->>PyMuPDF: fill_pdf_with_pymupdf(template, data)
        PyMuPDF->>Template: set text fields & checkboxes
        PyMuPDF->>Template: apply signature/pharmacy fields
        PyMuPDF->>Output: save filled PDF
    else pypdf available
        Detector-->>Script: pypdf found
        Script->>Script: load PATIENT_DATA
        Script->>Template: open template
        Script->>pypdf: fill_pdf_with_pypdf(template, data)
        pypdf->>Output: copy pages (field mapping stub)
    else No library
        Detector-->>Script: none found
        Script-->>User: print install guidance
    end

    Output-->>User: filled PDF (or guidance)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

📝 A form, a script, a patient named Jane,
Fields align as data meets plane,
Checkboxes click and signatures flow,
Templates fill, the outputs glow,
PDF chores vanish—automation reigns.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a PDF form-filling script for prior authorization workflows.
Description check ✅ Passed The description is directly related to the changeset, noting the PDF templates example and README instructions referenced in the actual changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch pdf_script

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🧹 Nitpick comments (1)
scripts/fill_pdf_form.py (1)

2-11: Docstring lists pypdf first, but PyMuPDF is the preferred/primary library.

The module docstring (lines 6–7) says "This script requires pypdf" before mentioning PyMuPDF as an alternative, which inverts the actual priority (PyMuPDF is tried first and recommended). Swap the order to match the runtime behavior.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/fill_pdf_form.py` around lines 2 - 11, The module docstring currently
lists pypdf first but the code prefers PyMuPDF; update the top-of-file docstring
in scripts/fill_pdf_form.py to mention PyMuPDF (pymupdf) as the
primary/recommended library and list pypdf as the alternative, so the order
matches runtime behavior and the recommendation in the script.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@scripts/fill_pdf_form.py`:
- Around line 78-79: The mock data in scripts/fill_pdf_form.py is inconsistent:
the "diagnosis" field currently reads "Chronic migraine without aura,
intractable, with status migrainosus" while documentation/profile state "with
aura"; update the "diagnosis" value to match the intended scenario (e.g.,
"Chronic migraine with aura, intractable, with status migrainosus") or, if the
diagnosis is correct, update the README/profile to match; also verify that the
"icd_code" ("G43.709") corresponds to the chosen wording and adjust it if
necessary so the "diagnosis" and "icd_code" fields are consistent.
- Around line 279-302: The pypdf fallback in fill_pdf_with_pypdf is a no-op
because update_page_form_field_values is called with an empty dict; change
fill_pdf_with_pypdf to either (A) build and pass a real field mapping by
iterating reader.get_form_text_fields() and mapping those names to values
(mirroring the PyMuPDF field names used elsewhere) before calling
writer.update_page_form_field_values(writer.pages[0], mapping), or (B) if you
don't want to implement filling, replace the silent behavior with a clear
warning and a non-success signal: log/print a message indicating pypdf
form-filling is unsupported for this PDF and ensure main() does not print
"Successfully filled PDF form!" when no fields were filled (e.g., return a
status or raise an informative exception). Ensure references to
reader.get_form_text_fields(), update_page_form_field_values, and
fill_pdf_with_pypdf are updated accordingly so the caller can detect and handle
the no-op case.
- Around line 305-331: The main() function prints a success message
unconditionally even if fill_pdf_with_pymupdf or fill_pdf_with_pypdf raises;
wrap the branch that calls these functions in a try/except around the fill
call(s) (guarded by USE_PYMUPDF / USE_PYPDF), catch Exception as e, print a
clear error including e (and optionally the input/output paths and
PATIENT_DATA['patient_name']), and sys.exit(1) on failure so the final success
prints only run when the fill completes without exception.
- Around line 143-276: The function fill_pdf_with_pymupdf currently opens the
PDF with fitz.open(input_pdf_path) and calls doc.save(...) then doc.close(),
which leaks the fitz.Document if doc.save raises; change to open the document
using a context manager (with fitz.open(input_pdf_path) as doc:) so the document
is always closed even on exceptions, move all code that uses doc (page = doc[0],
widgets collection, widget updates, the widgets_to_update loop, and doc.save
call) into that with-block, and remove the explicit doc.close() call; keep
references to functions set_widget_value, set_checkbox, and the widget
update/exception handling unchanged.
- Around line 17-31: The boolean flags for PDF libraries are not both
initialized, so USE_PYPDF can be undefined when fitz (PyMuPDF) imports
successfully; to fix, initialize USE_PYMUPDF = False and USE_PYPDF = False
before any imports, then in the existing try/except set USE_PYMUPDF = True when
import fitz succeeds and set USE_PYPDF = True inside the inner except when
importing PdfReader/PdfWriter from pypdf succeeds, ensuring both flags always
exist for later checks (references: USE_PYMUPDF, USE_PYPDF, import fitz, from
pypdf import PdfReader, PdfWriter).

In `@scripts/README-fill-pdf.md`:
- Around line 42-44: Rename the misspelled asset and all references from
"co-prescription-drug-prior-authorizathion-request-form.pdf" to
"co-prescription-drug-prior-authorization-request-form.pdf"; update the README
entry and the script fill_pdf_form.py (replace occurrences such as
TEMPLATE_PATH, OUTPUT_FILENAME or any variables/constants and the save path used
around lines 309–310) so the script reads the corrected template name and writes
the filled PDF to "...-authorization-request-form-filled-jane-doe.pdf"; ensure
any other references in the repo (tests, docs, asset lists) are updated to the
corrected filename.

---

Nitpick comments:
In `@scripts/fill_pdf_form.py`:
- Around line 2-11: The module docstring currently lists pypdf first but the
code prefers PyMuPDF; update the top-of-file docstring in
scripts/fill_pdf_form.py to mention PyMuPDF (pymupdf) as the primary/recommended
library and list pypdf as the alternative, so the order matches runtime behavior
and the recommendation in the script.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8323b59 and a5e572c.

📒 Files selected for processing (2)
  • scripts/README-fill-pdf.md
  • scripts/fill_pdf_form.py

Comment on lines +279 to +302
def fill_pdf_with_pypdf(input_pdf_path: str, output_pdf_path: str) -> None:
"""Fill PDF using pypdf (limited - mainly for form fields)."""
# pypdf is better for form fields, but this PDF may not have fillable fields
# So we'll create a text summary instead
reader = PdfReader(input_pdf_path)
writer = PdfWriter()

# Copy pages
for page in reader.pages:
writer.add_page(page)

# Try to fill form fields if they exist
if reader.get_form_text_fields():
# PDF has form fields - fill them
writer.update_page_form_field_values(
writer.pages[0],
{
# Map field names if they exist
}
)

# Save
with open(output_pdf_path, "wb") as output_file:
writer.write(output_file)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

The pypdf fallback fills zero fields — it's effectively a no-op.

update_page_form_field_values on line 293 receives an empty dict {}, so the "filled" output is identical to the input. This is misleading because main() prints "Successfully filled PDF form!" regardless. Either populate the field mapping (mirroring the PyMuPDF field names) or clearly warn the user that pypdf support is incomplete.

Option A: stub out with a clear warning
 def fill_pdf_with_pypdf(input_pdf_path: str, output_pdf_path: str) -> None:
-    """Fill PDF using pypdf (limited - mainly for form fields)."""
-    # pypdf is better for form fields, but this PDF may not have fillable fields
-    # So we'll create a text summary instead
+    """Fill PDF using pypdf (limited - field mapping not yet implemented)."""
     reader = PdfReader(input_pdf_path)
     writer = PdfWriter()
 
     # Copy pages
     for page in reader.pages:
         writer.add_page(page)
 
-    # Try to fill form fields if they exist
-    if reader.get_form_text_fields():
-        # PDF has form fields - fill them
-        writer.update_page_form_field_values(
-            writer.pages[0],
-            {
-                # Map field names if they exist
-            }
-        )
+    print("Warning: pypdf field mapping is not yet implemented. Output will be a copy of the original.")
 
     # Save
     with open(output_pdf_path, "wb") as output_file:
         writer.write(output_file)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/fill_pdf_form.py` around lines 279 - 302, The pypdf fallback in
fill_pdf_with_pypdf is a no-op because update_page_form_field_values is called
with an empty dict; change fill_pdf_with_pypdf to either (A) build and pass a
real field mapping by iterating reader.get_form_text_fields() and mapping those
names to values (mirroring the PyMuPDF field names used elsewhere) before
calling writer.update_page_form_field_values(writer.pages[0], mapping), or (B)
if you don't want to implement filling, replace the silent behavior with a clear
warning and a non-success signal: log/print a message indicating pypdf
form-filling is unsupported for this PDF and ensure main() does not print
"Successfully filled PDF form!" when no fields were filled (e.g., return a
status or raise an informative exception). Ensure references to
reader.get_form_text_fields(), update_page_form_field_values, and
fill_pdf_with_pypdf are updated accordingly so the caller can detect and handle
the no-op case.

- Initialize USE_PYMUPDF/USE_PYPDF flags up front to avoid NameError
- Fix diagnosis inconsistency: "without aura" → "with aura" (matches profile/README), update ICD code to G43.109
- Use context manager for fitz.open to prevent resource leak on save failure
- Add warning for unimplemented pypdf fallback instead of silent no-op
- Wrap fill call in try/except so success message only prints on actual success
- Fix "authorizathion" typo → "authorization" in filenames

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

@rsalus rsalus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All CodeRabbit feedback items addressed: flag initialization, diagnosis consistency, context manager for resource safety, pypdf fallback warning, error handling in main(), and typo fix. LGTM.

@rsalus rsalus merged commit 9d5b82b into main Feb 25, 2026
1 of 2 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Authscript Demo Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants