#!/usr/bin/env python3 """Generate a proposal PDF locally using the Lambda's template. Usage: python3 scripts/generate-pdf-local.py 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 ", 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()