File size: 3,139 Bytes
6cf8871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06675a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4a400b
06675a6
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from pdfrw import PdfReader, PdfWriter
from datetime import datetime

def fill_child_fee_pdf(
    template_pdf_path,
    output_pdf_path,
    emp_name,
    emp_code,
    department,
    bill_month,
    items,   # List of dicts: [{'bill_date': ..., 'description': ..., 'amount': ...}]
    total
):
    data_dict = {
        'emp_name': emp_name,
        'emp_code': emp_code,
        'department': department,
        'bill_month': bill_month,
        'total': str(total),
        'current_date': datetime.now().strftime("%d-%b-%Y"),  # e.g. "25-May-2025"

    }

    # Map each row of items to field names
    for idx, item in enumerate(items, start=1):
        data_dict[f'date_{idx}'] = item.get('bill_date', '')
        data_dict[f'description_{idx}'] = item.get('description', '')
        data_dict[f'amount_{idx}'] = str(item.get('amount', ''))

    # Fill the PDF
    template_pdf = PdfReader(template_pdf_path)
    for page in template_pdf.pages:
        if not hasattr(page, 'Annots') or not page.Annots:
            continue
        for annotation in page.Annots:
            if annotation.T:
                key = annotation.T[1:-1]  # Remove parentheses
                if key in data_dict:
                    annotation.V = str(data_dict[key])
                    annotation.AP = None  # Remove old appearance so new value appears
    PdfWriter().write(output_pdf_path, template_pdf)
    return output_pdf_path


def fill_medical_pdf(
    template_pdf_path,
    output_pdf_path,
    company,
    extension_no,
    employee_name,
    employee_code,
    department,
    date,
    total,
    designation,
    billing_month,
    claims  # List of dicts: [{'name': ..., 'relationship': ..., 'category': ..., 'detail': ..., 'amount': ...}]
):
    data_dict = {
        'company': company,
        'extension_no': extension_no,
        'employee_name': employee_name,
        'employee_code': employee_code,
        'department': department,
        'designation': designation,
        'date': date,
        'billing_month': billing_month,
        'total': str(total),
        'current_date': datetime.now().strftime("%d-%b-%Y"),
    }

    # Map each row of claims to field names
    for idx, claim in enumerate(claims, start=1):
        data_dict[f'name_{idx}'] = claim.get('name', '')
        data_dict[f'relationship_{idx}'] = claim.get('relationship', '')
        data_dict[f'category_{idx}'] = claim.get('category', '')
        data_dict[f'detail_{idx}'] = claim.get('detail', '')
        data_dict[f'amount_{idx}'] = str(claim.get('amount', ''))

    # Fill the PDF
    template_pdf = PdfReader(template_pdf_path)
    for page in template_pdf.pages:
        if not hasattr(page, 'Annots') or not page.Annots:
            continue
        for annotation in page.Annots:
            if annotation.T:
                key = annotation.T[1:-1]  # Remove parentheses
                if key in data_dict:
                    annotation.V = str(data_dict[key])
                    annotation.AP = None  # Remove old appearance so new value appears
    PdfWriter().write(output_pdf_path, template_pdf)
    
    return output_pdf_path