proposal-system/scripts/generate-pdf-local.py
Adam Moussa 9b502045dd Add site search, PO number, service category Other, and form enhancements
- Structured manual site entry with 5 separate address fields
- Site search via Autocomplete with server-side filtering
- Added PO number field to proposal form and AddPoNumber migration
- Added Other to ServiceCategory enum with custom category text input
- Label consistency: "Work Order #" → "Work Order Number", "PO Number"
- Top row reflow to 3-column layout (4/4/4)
- Dev PDF generation script for local testing
2026-05-27 18:17:48 -04:00

67 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Generate a proposal PDF locally using the Lambda's template.
Usage: python3 scripts/generate-pdf-local.py <proposal-id> <output-dir>
Fetches proposal data from the local API, generates a PDF, and saves it.
Prints JSON with the output path and s3Key on success.
"""
import json
import os
import sys
import httpx
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lambdas", "pdf-generate"))
from app import build_pdf # noqa: E402
API_URL = os.environ.get("API_URL", "http://localhost:5000")
def main():
if len(sys.argv) < 3:
print("Usage: generate-pdf-local.py <proposal-id> <output-dir>", file=sys.stderr)
sys.exit(1)
proposal_id = sys.argv[1]
output_dir = sys.argv[2]
token_resp = httpx.post(
f"{API_URL}/api/auth/dev-login",
json={"email": "adam@seahavenind.com", "role": "SysAdmin"},
timeout=10,
)
token_resp.raise_for_status()
token = token_resp.json()["token"]
headers = {"Authorization": f"Bearer {token}"}
proposal_resp = httpx.get(
f"{API_URL}/api/proposals/{proposal_id}", headers=headers, timeout=10
)
proposal_resp.raise_for_status()
proposal = proposal_resp.json()
li_resp = httpx.get(
f"{API_URL}/api/proposals/{proposal_id}/line-items",
headers=headers,
timeout=10,
)
li_resp.raise_for_status()
line_items = li_resp.json()
pdf_bytes = build_pdf(proposal, line_items)
proposal_number = proposal["proposalNumber"]
revision = proposal.get("currentRevision", 1)
s3_key = f"{proposal_number}/rev-{revision}.pdf"
output_path = os.path.join(output_dir, s3_key)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
with open(output_path, "wb") as f:
f.write(pdf_bytes)
print(json.dumps({"path": output_path, "s3Key": s3_key, "size": len(pdf_bytes)}))
if __name__ == "__main__":
main()