/** * PowerPoint Generator - PPTX Presentation Output * * Generates PowerPoint presentations using PptxGenJS with: * - Professional layouts * - Embedded charts * - Images * - Consistent styling */ import PptxGenJS from 'pptxgenjs'; import type { Slide, PresentationConfig, SlideType, MetricData } from '../../types/index.js'; import { createDefaultChartProvider, type ChartRequest } from '../../media/ChartProvider.js'; // Type definitions for PptxGenJS (simplified for our use) type PptxSlide = ReturnType; type TextProps = Parameters[1]; interface SlideLayout { title: { x: number; y: number; w: number; h: number; fontSize: number }; subtitle?: { x: number; y: number; w: number; h: number; fontSize: number }; body?: { x: number; y: number; w: number; h: number; fontSize: number }; image?: { x: number; y: number; w: number; h: number }; } const LAYOUTS: Record = { title: { title: { x: 0.5, y: 2.5, w: 9, h: 1.5, fontSize: 44 }, subtitle: { x: 0.5, y: 4, w: 9, h: 1, fontSize: 24 } }, 'big-idea': { title: { x: 0.5, y: 2, w: 9, h: 2.5, fontSize: 48 } }, 'single-statement': { title: { x: 0.5, y: 2, w: 9, h: 2.5, fontSize: 36 } }, 'big-number': { title: { x: 0.5, y: 1.5, w: 9, h: 2, fontSize: 72 }, subtitle: { x: 0.5, y: 3.5, w: 9, h: 1, fontSize: 20 } }, 'bullet-points': { title: { x: 0.5, y: 0.5, w: 9, h: 1, fontSize: 32 }, body: { x: 0.5, y: 1.5, w: 9, h: 4, fontSize: 18 } }, 'two-column': { title: { x: 0.5, y: 0.5, w: 9, h: 1, fontSize: 32 }, body: { x: 0.5, y: 1.5, w: 4.25, h: 4, fontSize: 16 }, image: { x: 5, y: 1.5, w: 4.5, h: 4 } }, quote: { title: { x: 0.5, y: 1.5, w: 9, h: 2.5, fontSize: 28 }, subtitle: { x: 0.5, y: 4, w: 9, h: 0.5, fontSize: 16 } }, 'thank-you': { title: { x: 0.5, y: 2.5, w: 9, h: 1.5, fontSize: 44 }, subtitle: { x: 0.5, y: 4, w: 9, h: 1, fontSize: 24 } }, default: { title: { x: 0.5, y: 0.5, w: 9, h: 1, fontSize: 32 }, body: { x: 0.5, y: 1.5, w: 9, h: 4, fontSize: 18 } } }; // Color themes const COLORS = { primary: '1a1a2e', secondary: '4a4a68', accent: '0f3460', highlight: 'e94560', white: 'ffffff', lightGray: 'f5f5f5' }; export class PowerPointGenerator { private chartProvider = createDefaultChartProvider(); /** * Generate a PowerPoint presentation. */ async generate(slides: Slide[], config: PresentationConfig): Promise { // Create new presentation const pptx = new PptxGenJS(); // Set presentation properties pptx.title = config.title; pptx.author = config.author ?? 'Claude Presentation Master'; pptx.subject = config.subject ?? ''; pptx.company = 'Generated by Claude Presentation Master'; // Set slide size (16:9) pptx.layout = 'LAYOUT_16x9'; // Define master slide pptx.defineSlideMaster({ title: 'MASTER_SLIDE', background: { color: COLORS.white } }); // Generate each slide for (const slide of slides) { await this.addSlide(pptx, slide, config); } // Generate buffer const data = await pptx.write({ outputType: 'nodebuffer' }); return data as Buffer; } /** * Add a slide to the presentation. */ private async addSlide(pptx: PptxGenJS, slide: Slide, config: PresentationConfig): Promise { const pptxSlide = pptx.addSlide({ masterName: 'MASTER_SLIDE' }); const layout = LAYOUTS[slide.type] ?? LAYOUTS['default']!; // Add background for special slides if (['title', 'thank-you', 'section-divider'].includes(slide.type)) { pptxSlide.background = { color: COLORS.lightGray }; } // Route to appropriate handler switch (slide.type) { case 'title': this.addTitleSlide(pptxSlide, slide, layout); break; case 'big-idea': case 'single-statement': this.addBigIdeaSlide(pptxSlide, slide, layout); break; case 'big-number': this.addBigNumberSlide(pptxSlide, slide, layout); break; case 'quote': this.addQuoteSlide(pptxSlide, slide, layout); break; case 'bullet-points': this.addBulletSlide(pptxSlide, slide, layout); break; case 'two-column': await this.addTwoColumnSlide(pptxSlide, slide, layout); break; case 'metrics-grid': this.addMetricsSlide(pptxSlide, slide, layout); break; case 'thank-you': this.addThankYouSlide(pptxSlide, slide, layout); break; case 'agenda': this.addAgendaSlide(pptxSlide, slide, layout); break; case 'section-divider': this.addSectionDividerSlide(pptxSlide, slide, layout); break; default: this.addDefaultSlide(pptxSlide, slide, layout); } // Add speaker notes if present if (slide.notes) { pptxSlide.addNotes(slide.notes); } // Add slide number pptxSlide.addText(String(slide.index + 1), { x: 9.3, y: 5.2, w: 0.5, h: 0.3, fontSize: 10, color: COLORS.secondary, align: 'right' }); } /** * Add title slide. */ private addTitleSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { if (slide.data.title) { pptxSlide.addText(slide.data.title, { ...this.positionToProps(layout.title), bold: true, color: COLORS.primary, align: 'center', valign: 'middle' }); } if (slide.data.subtitle && layout.subtitle) { pptxSlide.addText(slide.data.subtitle, { ...this.positionToProps(layout.subtitle), color: COLORS.secondary, align: 'center', valign: 'top' }); } if (slide.data.author) { pptxSlide.addText(slide.data.author as string, { x: 0.5, y: 4.8, w: 9, h: 0.5, fontSize: 14, color: COLORS.secondary, align: 'center' }); } } /** * Add big idea / single statement slide. */ private addBigIdeaSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { if (slide.data.title) { pptxSlide.addText(slide.data.title, { ...this.positionToProps(layout.title), bold: true, color: COLORS.primary, align: 'center', valign: 'middle' }); } } /** * Add big number slide. */ private addBigNumberSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { if (slide.data.title) { pptxSlide.addText(slide.data.title, { ...this.positionToProps(layout.title), bold: true, color: COLORS.highlight, align: 'center', valign: 'middle' }); } if (slide.data.subtitle && layout.subtitle) { pptxSlide.addText(slide.data.subtitle, { ...this.positionToProps(layout.subtitle), color: COLORS.secondary, align: 'center' }); } if (slide.data.source) { pptxSlide.addText(`Source: ${slide.data.source}`, { x: 0.5, y: 5, w: 9, h: 0.3, fontSize: 10, color: COLORS.secondary, align: 'right' }); } } /** * Add quote slide. */ private addQuoteSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { if (slide.data.quote) { pptxSlide.addText(`"${slide.data.quote}"`, { ...this.positionToProps(layout.title), italic: true, color: COLORS.primary, align: 'center', valign: 'middle' }); } if (slide.data.attribution && layout.subtitle) { pptxSlide.addText(`— ${slide.data.attribution}`, { ...this.positionToProps(layout.subtitle), color: COLORS.secondary, align: 'right' }); } } /** * Add bullet points slide. */ private addBulletSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { if (slide.data.title) { pptxSlide.addText(slide.data.title, { ...this.positionToProps(layout.title), bold: true, color: COLORS.primary }); } if (slide.data.bullets && layout.body) { const bulletText = slide.data.bullets.map(bullet => ({ text: bullet, options: { bullet: true, indentLevel: 0 } })); pptxSlide.addText(bulletText, { ...this.positionToProps(layout.body), color: COLORS.primary, paraSpaceAfter: 10 }); } } /** * Add two-column slide. */ private async addTwoColumnSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): Promise { if (slide.data.title) { pptxSlide.addText(slide.data.title, { ...this.positionToProps(layout.title), bold: true, color: COLORS.primary }); } // Left column: bullets or body if (layout.body) { if (slide.data.bullets) { const bulletText = slide.data.bullets.map(bullet => ({ text: bullet, options: { bullet: true, indentLevel: 0 } })); pptxSlide.addText(bulletText, { ...this.positionToProps(layout.body), color: COLORS.primary, paraSpaceAfter: 10 }); } else if (slide.data.body) { pptxSlide.addText(slide.data.body, { ...this.positionToProps(layout.body), color: COLORS.primary }); } } // Right column: image or metrics if (layout.image) { if (slide.data.images && slide.data.images.length > 0) { const img = slide.data.images[0]; if (img) { try { pptxSlide.addImage({ path: img.src, x: layout.image.x, y: layout.image.y, w: layout.image.w, h: layout.image.h }); } catch { // Image failed, add placeholder this.addImagePlaceholder(pptxSlide, layout.image, img.alt); } } } else if (slide.data.metrics) { this.addMetricsToSlide(pptxSlide, slide.data.metrics as MetricData[], { x: layout.image.x, y: layout.image.y, w: layout.image.w, h: layout.image.h }); } } } /** * Add metrics grid slide. */ private addMetricsSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { if (slide.data.title) { pptxSlide.addText(slide.data.title, { ...this.positionToProps(layout.title), bold: true, color: COLORS.primary }); } if (slide.data.metrics) { this.addMetricsToSlide(pptxSlide, slide.data.metrics, { x: 0.5, y: 1.5, w: 9, h: 4 }); } } /** * Add metrics to a slide at specified position. */ private addMetricsToSlide( pptxSlide: PptxSlide, metrics: MetricData[], bounds: { x: number; y: number; w: number; h: number } ): void { const cols = Math.min(metrics.length, 4); const colWidth = bounds.w / cols; metrics.slice(0, 4).forEach((metric, index) => { const x = bounds.x + (index * colWidth); // Value pptxSlide.addText(String(metric.value), { x, y: bounds.y, w: colWidth, h: 1, fontSize: 36, bold: true, color: COLORS.highlight, align: 'center' }); // Label pptxSlide.addText(metric.label, { x, y: bounds.y + 1, w: colWidth, h: 0.5, fontSize: 14, color: COLORS.secondary, align: 'center' }); // Change indicator if (metric.change) { const trendColor = metric.trend === 'up' ? '27ae60' : metric.trend === 'down' ? 'e74c3c' : COLORS.secondary; const trendIcon = metric.trend === 'up' ? '↑' : metric.trend === 'down' ? '↓' : '→'; pptxSlide.addText(`${trendIcon} ${metric.change}`, { x, y: bounds.y + 1.5, w: colWidth, h: 0.3, fontSize: 12, color: trendColor, align: 'center' }); } }); } /** * Add thank you slide. */ private addThankYouSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { pptxSlide.addText(slide.data.title ?? 'Thank You', { ...this.positionToProps(layout.title), bold: true, color: COLORS.primary, align: 'center', valign: 'middle' }); if (slide.data.subtitle && layout.subtitle) { pptxSlide.addText(slide.data.subtitle, { ...this.positionToProps(layout.subtitle), color: COLORS.secondary, align: 'center' }); } } /** * Add agenda slide. */ private addAgendaSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { pptxSlide.addText(slide.data.title ?? 'Agenda', { ...this.positionToProps(layout.title), bold: true, color: COLORS.primary }); if (slide.data.bullets && layout.body) { const bulletText = slide.data.bullets.map((bullet, index) => ({ text: bullet, options: { bullet: { type: 'number' as const }, indentLevel: 0 } })); pptxSlide.addText(bulletText, { ...this.positionToProps(layout.body), color: COLORS.primary, paraSpaceAfter: 15 }); } } /** * Add section divider slide. */ private addSectionDividerSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { pptxSlide.background = { color: COLORS.primary }; pptxSlide.addText(slide.data.title ?? '', { x: 0.5, y: 2, w: 9, h: 2, fontSize: 40, bold: true, color: COLORS.white, align: 'center', valign: 'middle' }); if (slide.data.subtitle) { pptxSlide.addText(slide.data.subtitle, { x: 0.5, y: 4, w: 9, h: 0.5, fontSize: 20, color: COLORS.lightGray, align: 'center' }); } } /** * Add default slide (fallback). */ private addDefaultSlide(pptxSlide: PptxSlide, slide: Slide, layout: SlideLayout): void { if (slide.data.title) { pptxSlide.addText(slide.data.title, { ...this.positionToProps(layout.title), bold: true, color: COLORS.primary }); } if (slide.data.body && layout.body) { pptxSlide.addText(slide.data.body, { ...this.positionToProps(layout.body), color: COLORS.primary }); } if (slide.data.bullets && layout.body) { const bulletText = slide.data.bullets.map(bullet => ({ text: bullet, options: { bullet: true, indentLevel: 0 } })); pptxSlide.addText(bulletText, { x: layout.body.x, y: layout.body.y + 1, w: layout.body.w, h: layout.body.h - 1, fontSize: layout.body.fontSize, color: COLORS.primary, paraSpaceAfter: 10 }); } } /** * Add image placeholder. */ private addImagePlaceholder( pptxSlide: PptxSlide, bounds: { x: number; y: number; w: number; h: number }, alt: string ): void { pptxSlide.addShape('rect', { x: bounds.x, y: bounds.y, w: bounds.w, h: bounds.h, fill: { color: COLORS.lightGray }, line: { color: COLORS.secondary, width: 1 } }); pptxSlide.addText(`[Image: ${alt}]`, { x: bounds.x, y: bounds.y + bounds.h / 2 - 0.2, w: bounds.w, h: 0.4, fontSize: 12, color: COLORS.secondary, align: 'center' }); } /** * Convert layout position to PptxGenJS text props. */ private positionToProps(pos: { x: number; y: number; w: number; h: number; fontSize: number }): TextProps { return { x: pos.x, y: pos.y, w: pos.w, h: pos.h, fontSize: pos.fontSize }; } }