{
  "credit_card_bill": {
    "bill_date": "2023-10-12",
    "due_date": "2023-11-01",
    "total_due": 450.75,
    "minimum_due": 50.00,
    "last_statement_balance": 430.00,
    "rewards_points_summary": {
      "total_points": 3500,
      "points_earned_this_cycle": 500,
      "points_redeemed_this_cycle": 200,
      "current_redeemable_points": 3300,
      "expiry_details": {
        "points_expiring_soon": 150,
        "expiry_date": "2024-01-31"
      },
      "redeemable_options": [
        {
          "type": "cash_back",
          "value": "0.01 USD per point",
          "min_points": 1000,
          "max_points": 5000
        },
        {
          "type": "gift_cards",
          "options": [
            {
              "merchant": "Amazon",
              "value": 25,
              "points_cost": 2500
            },
            {
              "merchant": "Starbucks",
              "value": 10,
              "points_cost": 1000
            }
          ]
        },
        {
          "type": "travel_miles",
          "value": "1 mile per point",
          "partner_airlines": [
            "Airline A",
            "Airline B",
            "Airline C"
          ]
        }
      ]
    },
    "transactions": [
      {
        "date": "2023-09-15",
        "description": "Grocery Store",
        "amount": 75.20,
        "category": "Grocery"
      },
      {
        "date": "2023-09-18",
        "description": "Online Subscription",
        "amount": 15.00,
        "category": "Entertainment"
      },
      {
        "date": "2023-09-22",
        "description": "Restaurant",
        "amount": 45.50,
        "category": "Dining"
      },
      {
        "date": "2023-10-01",
        "description": "Gas Station",
        "amount": 40.00,
        "category": "Fuel"
      }
    ],
    "payment_history": [
      {
        "date": "2023-09-05",
        "amount": 430.00,
        "status": "Paid"
      },
      {
        "date": "2023-08-03",
        "amount": 400.00,
        "status": "Paid"
      }
    ]
  }
}
import os
from playwright.sync_api import sync_playwright
from PIL import Image
from io import BytesIO
import json

def generate_document(data):
    # HTML Content
    html_content = f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            body {{ font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 0; }}
            .container {{ padding: 20px; max-width: 800px; margin: auto; }}
            .header {{ background-color: #4CAF50; color: white; padding: 10px 0; text-align: center; }}
            .bill-section, .rewards-section, .transactions-section, .payments-section {{ margin-bottom: 20px; }}
            .section-title {{ background-color: #f2f2f2; padding: 10px; font-size: 1.2em; border-bottom: 2px solid #ccc; }}

            table {{ width: 100%; border-collapse: collapse; }}
            table, th, td {{ border: 1px solid #ddd; }}
            th, td {{ padding: 8px; text-align: left; }}

            .points-box {{ padding: 10px; border: 1px solid #ddd; display: inline-block; width: 47%; margin-right: 10px; margin-bottom: 15px; }}
            .points-box:last-child {{ margin-right: 0; }}

            .redeem-options {{ display: flex; flex-wrap: wrap; }}
            .redeem-option {{ border: 1px solid #4CAF50; border-radius: 5px; padding: 10px; margin: 10px; width: calc(50% - 40px); }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>Credit Card Statement</h1>
                <p>Bill Date: {data["credit_card_bill"]["bill_date"]} | Due Date: {data["credit_card_bill"]["due_date"]}</p>
            </div>

            <div class="bill-section">
                <div class="section-title">Statement Summary</div>
                <p>Total Due: ${data["credit_card_bill"]["total_due"]:.2f}</p>
                <p>Minimum Due: ${data["credit_card_bill"]["minimum_due"]:.2f}</p>
                <p>Last Statement Balance: ${data["credit_card_bill"]["last_statement_balance"]:.2f}</p>
            </div>

            <div class="rewards-section">
                <div class="section-title">Rewards Points Summary</div>
                <div class='points-box'>
                    <p>Total Points: {data["credit_card_bill"]["rewards_points_summary"]["total_points"]}</p>
                    <p>Points Earned This Cycle: {data["credit_card_bill"]["rewards_points_summary"]["points_earned_this_cycle"]}</p>
                    <p>Points Redeemed This Cycle: {data["credit_card_bill"]["rewards_points_summary"]["points_redeemed_this_cycle"]}</p>
                </div>
                <div class='points-box'>
                    <p>Current Redeemable Points: {data["credit_card_bill"]["rewards_points_summary"]["current_redeemable_points"]}</p>
                    <p>Points Expiring Soon: {data["credit_card_bill"]["rewards_points_summary"]["expiry_details"]["points_expiring_soon"]}</p>
                    <p>Expiry Date: {data["credit_card_bill"]["rewards_points_summary"]["expiry_details"]["expiry_date"]}</p>
                </div>
                <div class="redeem-options">
                    """

    for option in data["credit_card_bill"]["rewards_points_summary"]["redeemable_options"]:
        if option["type"] == "cash_back":
            html_content += f"""
            <div class="redeem-option">
                <strong>Cash Back:</strong>
                <p>Value: {option["value"]}</p>
                <p>Minimum Points: {option["min_points"]}</p>
                <p>Maximum Points: {option["max_points"]}</p>
            </div>
            """
        elif option["type"] == "gift_cards":
            for gift_card in option["options"]:
                html_content += f"""
                <div class="redeem-option">
                    <strong>Gift Card:</strong>
                    <p>Merchant: {gift_card["merchant"]}</p>
                    <p>Value: ${gift_card["value"]}</p>
                    <p>Points Cost: {gift_card["points_cost"]}</p>
                </div>
                """
        elif option["type"] == "travel_miles":
            html_content += f"""
            <div class="redeem-option">
                <strong>Travel Miles:</strong>
                <p>Value: {option["value"]}</p>
                <p>Partner Airlines: {", ".join(option["partner_airlines"])}</p>
            </div>
            """

    html_content += """
                </div>
            </div>

            <div class="transactions-section">
                <div class="section-title">Transactions</div>
                <table>
                    <tr>
                        <th>Date</th>
                        <th>Description</th>
                        <th>Amount</th>
                        <th>Category</th>
                    </tr>
    """

    for transaction in data["credit_card_bill"]["transactions"]:
        html_content += f"""
        <tr>
            <td>{transaction["date"]}</td>
            <td>{transaction["description"]}</td>
            <td>${transaction["amount"]:.2f}</td>
            <td>{transaction["category"]}</td>
        </tr>
        """

    html_content += """
                </table>
            </div>

            <div class="payments-section">
                <div class="section-title">Payment History</div>
                <table>
                    <tr>
                        <th>Date</th>
                        <th>Amount</th>
                        <th>Status</th>
                    </tr>
    """

    for payment in data["credit_card_bill"]["payment_history"]:
        html_content += f"""
        <tr>
            <td>{payment["date"]}</td>
            <td>${payment["amount"]:.2f}</td>
            <td>{payment["status"]}</td>
        </tr>
        """

    html_content += """
                </table>
            </div>
        </div>
    </body>
    </html>
    """

    # File paths
    html_file = "credit_card_statement.html"

    # Write the HTML content to a file
    with open(html_file, "w") as file:
        file.write(html_content)

    # Generate screenshot using Playwright
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page()
        page.goto(f"file:///{os.path.abspath(html_file)}")
        screenshot_bytes = page.screenshot(full_page=True)
        browser.close()
    
    os.remove(html_file)  # Clean up the HTML file after usage

    return Image.open(BytesIO(screenshot_bytes))
The Credit Card Statement web page opens with a distinct header section that includes the title "Credit Card Statement" against a background of green color. This header also lists two key dates: the Bill Date, specified as October 12, 2023, and the Due Date, specified as November 1, 2023.

Below the header, the main content is divided into several sections.

**Section 1: Statement Summary**
This section begins with a light grey-colored title block labeled "Statement Summary." Within this section, three key financial figures are presented in paragraph form:
- Total Due: $450.75
- Minimum Due: $50.00
- Last Statement Balance: $430.00

**Section 2: Rewards Points Summary**
The second section has a title block labeled "Rewards Points Summary." Inside this section, there are two boxes placed side by side, each displaying summary information about the user's reward points.

The left box includes:
- Total Points: 3500
- Points Earned This Cycle: 500
- Points Redeemed This Cycle: 200

The right box includes:
- Current Redeemable Points: 3300
- Points Expiring Soon: 150
- Expiry Date: January 31, 2024

Following these boxes is an area labeled "Redeem Options," where three types of redeemable rewards are shown in green-bordered cards.

1. **Cash Back Option:** This card details that the value is "0.01 USD per point," with a minimum redeem requirement of 1000 points and a maximum of 5000 points.
   
2. **Gift Cards Option:** Two gift cards are shown:
   - **Amazon:** Value is $25, costing 2500 points.
   - **Starbucks:** Value is $10, costing 1000 points.

3. **Travel Miles Option:** This card states "1 mile per point" and lists partner airlines as "Airline A, Airline B," and "Airline C." 

**Section 3: Transactions**
This section starts with a block titled "Transactions." Below it is a table that itemizes the recent transactions in four columns: Date, Description, Amount, and Category. The transactions included are:
- Date: September 15, Grocery Store, Amount: $75.20, Category: Grocery
- Date: September 18, Online Subscription, Amount: $15.00, Category: Entertainment
- Date: September 22, Restaurant, Amount: $45.50, Category: Dining
- Date: October 1, Gas Station, Amount: $40.00, Category: Fuel

**Section 4: Payment History**
The final section is labeled "Payment History" and features another table showing past payment activities. The columns in this table are Date, Amount, and Status. These payments include:
- Date: September 5, Amount: $430.00, Status: Paid
- Date: August 3, Amount: $400.00, Status: Paid

Each table and box is neatly organized with clear boundaries separating different sections and categories, providing a structured and easy-to-navigate layout of the user's credit card billing information. The overall design is clean and user-friendly, ensuring that all essential details are easily accessible and understandable at a glance.
[
    {
        "question": "What is the total due amount for this billing cycle?",
        "answer": "450.75",
        "explanation": "The total due amount for this billing cycle is displayed in the Statement Summary section."
    },
    {
        "question": "When is the due date for the current credit card bill?",
        "answer": "2023-11-01",
        "explanation": "The due date of the current credit card bill is shown in the header section of the page."
    },
    {
        "question": "How many rewards points were earned in this cycle?",
        "answer": "500",
        "explanation": "The rewards points earned in this cycle are shown in the Rewards Points Summary section under 'Points Earned This Cycle'."
    },
    {
        "question": "How much is the minimum due for this billing cycle?",
        "answer": "50.00",
        "explanation": "The minimum due amount is mentioned in the Statement Summary section."
    },
    {
        "question": "What was the balance of the last statement?",
        "answer": "430.00",
        "explanation": "The last statement balance is provided in the Statement Summary section."
    },
    {
        "question": "How many points are expiring soon and when?",
        "answer": "150 points expiring on 2024-01-31",
        "explanation": "The section titled Rewards Points Summary provides details on points expiring soon and the expiry date."
    },
    {
        "question": "What is the value of cash back per rewards point?",
        "answer": "0.01 USD per point",
        "explanation": "The value for cash back per rewards point is provided in the Rewards Points Summary section under redeemable options."
    },
    {
        "question": "How many points are currently redeemable?",
        "answer": "3300",
        "explanation": "The current redeemable points are listed in the Rewards Points Summary section."
    },
    {
        "question": "Which partner airlines are available for redeeming travel miles?",
        "answer": "Airline A, Airline B, Airline C",
        "explanation": "The partner airlines are listed under the Travel Miles option in the redeemable options section."
    },
    {
        "question": "What is the points cost for an Amazon gift card worth $25?",
        "answer": "2500 points",
        "explanation": "In the Rewards Points Summary section, under redeemable options for gift cards, the points cost for an Amazon gift card is specified."
    },
    {
        "question": "What transactions were made at a restaurant and what was the amount?",
        "answer": "Transaction on 2023-09-22 with the amount of 45.50",
        "explanation": "Under the Transactions section, there is a row with a description 'Restaurant' on the date 2023-09-22 and the amount of 45.50."
    },
    {
        "question": "How many points were redeemed in this cycle?",
        "answer": "200",
        "explanation": "The Rewards Points Summary section provides information about the points redeemed in this cycle."
    },
    {
        "question": "What is the total number of reward points?",
        "answer": "3500",
        "explanation": "The total number of reward points is shown in the Rewards Points Summary section."
    },
    {
        "question": "What was the payment amount on 2023-09-05 and its status?",
        "answer": "A payment of 430.00 was made and its status is Paid.",
        "explanation": "The payment details for the amount, date, and status are found in the Payment History section."
    },
    {
        "question": "Which merchant gift card can be redeemed for 1000 points and what is its value?",
        "answer": "Starbucks gift card worth $10",
        "explanation": "In the Rewards Points Summary under redeemable options, the Starbucks gift card can be redeemed for 1000 points with a value of $10."
    },
    {
        "question": "What are the four categories of the transactions listed?",
        "answer": "Grocery, Entertainment, Dining, Fuel",
        "explanation": "The categories of transactions are listed under the Transactions section for each transaction entry."
    }
]