""" modules/proposal_pdf.py ChemeNova × OrbitChem — Branded Qualification Proposal PDF Generates a professional PDF report from Full Qualification Suite results. Uses reportlab (MIT) for PDF generation. Author: ChemeNova LLC — PROPRIETARY """ from __future__ import annotations import io from datetime import datetime from typing import Dict, Optional try: from reportlab.lib.pagesizes import A4 from reportlab.lib.units import mm from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT from reportlab.lib import colors from reportlab.platypus import ( SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, HRFlowable, KeepTogether ) from reportlab.platypus.flowables import HRFlowable REPORTLAB_OK = True except ImportError: REPORTLAB_OK = False # ── ChemeNova brand colours ─────────────────────────────────────────────────── CN_NAVY = colors.HexColor('#0A1628') CN_TEAL = colors.HexColor('#0D9488') CN_AMBER = colors.HexColor('#D97706') CN_WHITE = colors.white CN_LIGHT = colors.HexColor('#F0F2F5') CN_MUTED = colors.HexColor('#64748B') CN_GREEN = colors.HexColor('#1D9E75') CN_RED = colors.HexColor('#E24B4A') CN_WARN = colors.HexColor('#EF9F27') def _pass_color(passed: bool): return CN_GREEN if passed else CN_RED def _rating_color(rating: str): r = rating.lower() if r in ('excellent', 'good'): return CN_GREEN if r in ('marginal',): return CN_WARN return CN_RED def generate_qualification_pdf( blend: Dict[str, float], orbit: str, mission_years: float, substrate: str, outgassing, radiation, thermal, org_name: str = "ChemeNova LLC", ) -> bytes: """ Generate a branded ChemeNova qualification proposal PDF. Returns bytes suitable for st.download_button. """ if not REPORTLAB_OK: raise ImportError("reportlab not installed. Run: pip install reportlab") buf = io.BytesIO() doc = SimpleDocTemplate( buf, pagesize=A4, leftMargin=18*mm, rightMargin=18*mm, topMargin=14*mm, bottomMargin=18*mm, ) styles = getSampleStyleSheet() W = A4[0] - 36*mm # usable width # ── Custom styles ───────────────────────────────────────────────────────── def _sty(name, parent='Normal', **kw): s = ParagraphStyle(name, parent=styles[parent], **kw) return s H1 = _sty('H1', fontSize=22, textColor=CN_WHITE, fontName='Helvetica-Bold', spaceAfter=4, leading=26) H2 = _sty('H2', fontSize=13, textColor=CN_TEAL, fontName='Helvetica-Bold', spaceBefore=10, spaceAfter=4) H3 = _sty('H3', fontSize=10, textColor=CN_AMBER, fontName='Helvetica-Bold', spaceBefore=6, spaceAfter=2) BODY= _sty('BODY', fontSize=9, textColor=colors.HexColor('#1e293b'), leading=13, spaceAfter=3) SMALL=_sty('SMALL',fontSize=7.5,textColor=CN_MUTED, leading=10) CAP = _sty('CAP', fontSize=8, textColor=CN_MUTED, fontName='Helvetica-Oblique') RHD = _sty('RHD', fontSize=8, textColor=CN_WHITE, fontName='Helvetica-Bold', alignment=TA_CENTER) RVAL= _sty('RVAL', fontSize=8.5,textColor=colors.HexColor('#0f172a'), alignment=TA_CENTER) ts_hdr = TableStyle([ ('BACKGROUND', (0,0), (-1,0), CN_NAVY), ('TEXTCOLOR', (0,0), (-1,0), CN_WHITE), ('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'), ('FONTSIZE', (0,0), (-1,0), 8), ('ROWBACKGROUNDS',(0,1),(-1,-1),[CN_LIGHT, CN_WHITE]), ('FONTSIZE', (0,1), (-1,-1), 8), ('GRID', (0,0), (-1,-1), 0.3, colors.HexColor('#CBD5E1')), ('ALIGN', (1,0), (-1,-1), 'CENTER'), ('TOPPADDING', (0,0), (-1,-1), 4), ('BOTTOMPADDING',(0,0),(-1,-1), 4), ('LEFTPADDING', (0,0), (-1,-1), 6), ('RIGHTPADDING',(0,0),(-1,-1), 6), ]) story = [] # ── Header banner ───────────────────────────────────────────────────────── banner_data = [[ Paragraph('OrbitChem™', H1), Paragraph( f'SPACECRAFT MATERIAL
QUALIFICATION REPORT
', _sty('BT', fontSize=11, textColor=CN_AMBER, fontName='Helvetica-Bold', alignment=TA_RIGHT, leading=15)), ]] banner = Table(banner_data, colWidths=[W*0.6, W*0.4]) banner.setStyle(TableStyle([ ('BACKGROUND', (0,0),(-1,-1), CN_NAVY), ('VALIGN', (0,0),(-1,-1), 'MIDDLE'), ('TOPPADDING', (0,0),(-1,-1), 10), ('BOTTOMPADDING', (0,0),(-1,-1), 10), ('LEFTPADDING', (0,0),(-1,-1), 12), ('RIGHTPADDING', (0,0),(-1,-1), 12), ('ROUNDEDCORNERS',(0,0),(-1,-1), [6,6,6,6]), ])) story.append(banner) story.append(Spacer(1, 4*mm)) # Sub-header meta = Table([[ Paragraph(f'Prepared for: {org_name}', BODY), Paragraph(f'Date: {datetime.now().strftime("%d %B %Y")}', BODY), Paragraph(f'Orbit: {orbit} · Mission: {mission_years:.1f} yr · Substrate: {substrate.replace("_"," ").title()}', BODY), ]], colWidths=[W*0.35, W*0.25, W*0.40]) meta.setStyle(TableStyle([ ('BACKGROUND', (0,0),(-1,-1), colors.HexColor('#F8FAFC')), ('GRID', (0,0),(-1,-1), 0.3, colors.HexColor('#E2E8F0')), ('TOPPADDING', (0,0),(-1,-1), 5), ('BOTTOMPADDING',(0,0),(-1,-1), 5), ('LEFTPADDING', (0,0),(-1,-1), 8), ])) story.append(meta) story.append(Spacer(1, 5*mm)) # ── Qualification Summary ────────────────────────────────────────────────── story.append(Paragraph('1. Qualification Summary', H2)) story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) story.append(Spacer(1, 2*mm)) def verdict(passed, label): col = CN_GREEN if passed else CN_RED sym = '✓ PASS' if passed else '✗ FAIL' return Paragraph(f'{sym}', RVAL), label summary_rows = [ [Paragraph('Standard', RHD), Paragraph('Result', RHD), Paragraph('Key Metric', RHD), Paragraph('Value', RHD)], ['NASA MSFC-SPEC-1238 (Outgassing)', Paragraph(f'{"✓ PASS" if outgassing.nasa_pass else "✗ FAIL"}', RVAL), 'TML / CVCM', f'{outgassing.blend_tml_pct:.4f}% / {outgassing.blend_cvcm_pct:.5f}%'], ['ESA ECSS-Q-ST-70-02 (Outgassing)', Paragraph(f'{"✓ PASS" if outgassing.esa_pass else "✗ FAIL"}', RVAL), 'VCM', f'{outgassing.blend_vcm_pct:.5f}%'], ['Radiation Stability (G-value model)', Paragraph(f'{radiation.overall_rating.upper()}', RVAL), 'Tensile retention', f'{radiation.tensile_retention_pct:.1f}% @ {radiation.total_tid_krad:.0f} kRad'], ['Thermal Cycling (Coffin-Manson)', Paragraph(f'{"✓ PASS" if thermal.blend_passes else "✗ FAIL"}', RVAL), 'Safety factor (min Nf / cycles)', f'{thermal.safety_factor:.2f}×'], ] sum_t = Table(summary_rows, colWidths=[W*0.40, W*0.15, W*0.28, W*0.17]) sum_t.setStyle(ts_hdr) story.append(sum_t) story.append(Spacer(1, 5*mm)) # ── Blend Composition ───────────────────────────────────────────────────── story.append(Paragraph('2. Blend Composition', H2)) story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) story.append(Spacer(1, 2*mm)) blend_rows = [[Paragraph('Material', RHD), Paragraph('wt %', RHD), Paragraph('VP (Pa@25°C)', RHD), Paragraph('Outgas Risk', RHD)]] for rec in outgassing.records: risk_col = {'low':'#1D9E75','medium':'#EF9F27','high':'#E24B4A','critical':'#7c0000'}.get(rec.risk_level,'#64748B') blend_rows.append([ rec.ingredient, f'{rec.pct:.1f}%', f'{rec.vapour_pressure_Pa:.2e}', Paragraph(f'{rec.risk_level.upper()}', RVAL), ]) blend_t = Table(blend_rows, colWidths=[W*0.50, W*0.10, W*0.20, W*0.20]) blend_t.setStyle(ts_hdr) story.append(blend_t) story.append(Spacer(1, 5*mm)) # ── Outgassing Detail ───────────────────────────────────────────────────── story.append(Paragraph('3. Outgassing Screen — ASTM E595 Conditions (125°C / 24h / 10⁻⁴ Pa)', H2)) story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) story.append(Spacer(1, 2*mm)) story.append(Paragraph( f'Blend TML = {outgassing.blend_tml_pct:.4f}% (limit ≤ 1.0%) · ' f'Blend CVCM = {outgassing.blend_cvcm_pct:.5f}% (limit ≤ 0.1%) · ' f'Blend VCM = {outgassing.blend_vcm_pct:.5f}% (ESA limit ≤ 0.1%)', BODY)) story.append(Spacer(1, 2*mm)) og_rows = [[Paragraph(h, RHD) for h in ['Material','wt%','TML contrib %','CVCM contrib %','MW g/mol','Risk']]] for r in outgassing.records: og_rows.append([r.ingredient, f'{r.pct:.1f}%', f'{r.tml_contribution_pct:.5f}', f'{r.cvcm_contribution_pct:.6f}', f'{r.mw:.0f}', r.risk_level.upper()]) og_t = Table(og_rows, colWidths=[W*0.38,W*0.07,W*0.15,W*0.16,W*0.10,W*0.14]) og_t.setStyle(ts_hdr) story.append(og_t) for w in outgassing.notes: story.append(Paragraph(f'• {w}', SMALL)) story.append(Spacer(1, 5*mm)) # ── Radiation ───────────────────────────────────────────────────────────── story.append(Paragraph(f'4. Radiation Stability — {radiation.environment} ({radiation.mission_years:.0f} yr, {radiation.total_tid_krad:.0f} kRad)', H2)) story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) story.append(Spacer(1, 2*mm)) story.append(Paragraph( f'Blend tensile retention = {radiation.tensile_retention_pct:.1f}% · ' f'Yellowing index = {radiation.yellowing_index:.2f} · ' f'Overall rating = {radiation.overall_rating.upper()}', BODY)) story.append(Spacer(1, 2*mm)) rad_rows = [[Paragraph(h, RHD) for h in ['Material','wt%','Polymer class','G(S)','G(X)','Net G','Tensile ret.','Rating']]] for r in radiation.records: rad_rows.append([ r.ingredient, f'{r.pct:.1f}%', r.polymer_class, f'{r.G_scission:.1f}', f'{r.G_crosslink:.1f}', f'{r.net_G:.2f}', f'{r.dose_response*100:.1f}%', r.rad_hardness.upper() ]) rad_t = Table(rad_rows, colWidths=[W*0.28,W*0.06,W*0.13,W*0.07,W*0.07,W*0.07,W*0.13,W*0.19]) rad_t.setStyle(ts_hdr) story.append(rad_t) for rec in radiation.recommendations: story.append(Paragraph(f'• {rec}', SMALL)) story.append(Spacer(1, 5*mm)) # ── Thermal Cycling ─────────────────────────────────────────────────────── story.append(Paragraph(f'5. Thermal Cycling — {thermal.orbit} (ΔT={thermal.delta_T:.0f}°C, {thermal.total_cycles:,} cycles)', H2)) story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) story.append(Spacer(1, 2*mm)) story.append(Paragraph( f'Blend CTE = {thermal.blend_cte_ppm_K:.1f} ppm/K · ' f'Min Nf = {int(min(thermal.blend_min_Nf,1e9)):,} cycles · ' f'Safety factor = {thermal.safety_factor:.2f}× · ' f'Result = {"PASS" if thermal.blend_passes else "FAIL"}', BODY)) story.append(Spacer(1, 2*mm)) tc_rows = [[Paragraph(h, RHD) for h in ['Material','wt%','CTE ppm/K','E (GPa)','Nf cycles','Delam. G (J/m²)','Risk']]] for r in thermal.records: tc_rows.append([ r.ingredient, f'{r.pct:.1f}%', f'{r.cte_ppm_K:.1f}', f'{r.modulus_GPa:.2f}', f'{int(min(r.cycles_to_fail,1e9)):,}', f'{r.delamination_GR:.1f}', r.risk_level.upper() ]) tc_t = Table(tc_rows, colWidths=[W*0.32,W*0.07,W*0.11,W*0.09,W*0.14,W*0.14,W*0.13]) tc_t.setStyle(ts_hdr) story.append(tc_t) for rec in thermal.recommendations: story.append(Paragraph(f'• {rec}', SMALL)) story.append(Spacer(1, 5*mm)) # ── Recommendations ─────────────────────────────────────────────────────── all_recs = list(outgassing.warnings) + list(radiation.recommendations) + list(thermal.recommendations) if all_recs: story.append(Paragraph('6. Recommendations & Next Steps', H2)) story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) story.append(Spacer(1, 2*mm)) for i, rec in enumerate(all_recs[:8], 1): story.append(Paragraph(f'{i}. {rec}', BODY)) story.append(Spacer(1, 3*mm)) story.append(Paragraph('7. Qualification Pathway', H2)) story.append(HRFlowable(width=W, thickness=0.5, color=CN_TEAL)) story.append(Spacer(1, 2*mm)) story.append(Paragraph( 'This report constitutes a screening-level qualification assessment using physics-based ' 'surrogate models (Clausius-Clapeyron outgassing, G-value radiation, Coffin-Manson thermal fatigue). ' 'Formal flight qualification requires:', BODY)) steps = [ ('Outgassing', 'ASTM E595-22 coupon test at 125°C / 24h / 7×10⁻⁵ Torr — TML ≤ 1.0%, CVCM ≤ 0.1%'), ('Radiation', 'Co-60 gamma or proton beam TID test to mission dose + 25% margin'), ('Thermal', 'Thermal vacuum cycling per NASA-STD-7001 / ECSS-E-ST-32 — 100+ cycles minimum'), ('Mechanical', 'Random vibration + shock per MIL-STD-810 / ECSS-E-ST-32'), ] step_rows = [[Paragraph('Test', RHD), Paragraph('Standard / Requirement', RHD)]] for s, d in steps: step_rows.append([s, d]) step_t = Table(step_rows, colWidths=[W*0.20, W*0.80]) step_t.setStyle(ts_hdr) story.append(step_t) story.append(Spacer(1, 5*mm)) # ── Footer ──────────────────────────────────────────────────────────────── story.append(HRFlowable(width=W, thickness=1.0, color=CN_TEAL)) story.append(Spacer(1, 2*mm)) footer_data = [[ Paragraph('OrbitChem™ · ChemeNova LLC · chemenova.com/orbitchem', SMALL), Paragraph(f'Generated {datetime.now().strftime("%Y-%m-%d %H:%M UTC")}', _sty('FR', fontSize=7.5, textColor=CN_MUTED, alignment=TA_RIGHT)), ]] footer_t = Table(footer_data, colWidths=[W*0.65, W*0.35]) footer_t.setStyle(TableStyle([('TOPPADDING',(0,0),(-1,-1),2)])) story.append(footer_t) story.append(Paragraph( '⚠ This report is generated by OrbitChem™ AI screening models and does not constitute formal ' 'flight qualification. All results must be verified by laboratory testing before use in space hardware.', _sty('DISC', fontSize=6.5, textColor=CN_MUTED, fontName='Helvetica-Oblique'))) doc.build(story) buf.seek(0) return buf.read()