Skip to content

Conversation

@liamgold
Copy link
Owner

Summary

Adds PDF export functionality to the Sustainability tab, allowing users to download comprehensive sustainability reports as PDF documents for sharing and documentation.

Features

PDF Export

  • One-click export: Export current sustainability report as a professionally formatted PDF
  • Comprehensive content: Includes carbon rating, emissions data, page weight, resource breakdown, and methodology
  • Clean layout: Summary metrics on page 1, detailed resource breakdown on pages 2+
  • Smart pagination: CSS page breaks prevent awkward content splitting
  • Timestamped filenames: Format: {PageName}-{Timestamp}.pdf

UI Improvements

  • Action button icons: Added icons to all action buttons (Run Analysis, Export PDF, View History, Load More)
  • Loading states: All buttons show proper loading spinners and disable during execution
  • Consistent styling: Secondary button styling for export action

Code Quality

  • Dedicated PDF service: Extracted PDF generation into ISustainabilityPdfService for better separation of concerns
  • Documented patterns: Added usePageCommand loading state pattern to CLAUDE.md to prevent future issues
  • Improved architecture: ~190 lines removed from SustainabilityService, moved to focused PDF service

Documentation

  • Updated README: Added PDF export feature bullet, restructured with embedded feature screenshots
  • Historical tracking screenshot: Added visual showing trend charts and report history
  • Better organization: Screenshots now appear inline with feature descriptions

PDF Content

The exported PDF includes:

  • Header: Page title, URL, generation timestamp
  • Carbon Rating Hero: Large letter grade with efficiency description and hosting status
  • Metrics Grid: CO₂ emissions, page weight, resource count, methodology
  • Resource Breakdown: Categorized resources (Images, Scripts, CSS, etc.) with top 5 per category
  • Footer: Link to GitHub repository and SWDM v4 methodology

Technical Details

  • Uses Microsoft Playwright for PDF generation
  • A4 format with 1.5cm margins
  • Print background enabled for full styling
  • CSS page breaks for clean layout
  • Proper HTML escaping for security

Testing

  • ✅ PDF exports successfully
  • ✅ Loading states work correctly
  • ✅ Page breaks prevent content splitting
  • ✅ All icons display properly
  • ✅ Button states update correctly
  • ✅ Build succeeds with no warnings

Screenshots

See updated README for current screenshots showing:

  • Sustainability tab with new export button
  • Historical tracking with trend charts

🤖 Generated with Claude Code

liamgold and others added 12 commits November 22, 2025 16:59
Implements stakeholder-ready PDF export feature allowing users to download
and share professional sustainability reports.

## Features Added

**Frontend**:
- Added "Export as PDF" button in sustainability tab header
- Implemented base64-to-blob conversion for PDF download
- Automatic file download with timestamp-based filename
- Button shows loading state during PDF generation

**Backend**:
- New `GeneratePdfReport` method in `SustainabilityService`
- Leverages existing Playwright infrastructure for PDF generation
- Professional HTML template with embedded CSS
- Includes carbon rating, metrics, resource breakdown (top 5 per category)
- Color-coded rating badges and hosting status indicators

**PDF Contents**:
- Header: Page title, URL, generation timestamp
- Hero section: Large carbon rating badge with color gradient
- Metrics grid: CO₂ emissions, page weight, resource count, methodology
- Resource breakdown: Tables showing top 5 resources per category
- Footer: Xperience Community Sustainability branding, SWDM v4 link

## Technical Implementation

- Uses Playwright's `page.PdfAsync()` with A4 format and 1.5cm margins
- Returns PDF as base64 string via `PdfExportResult` record
- Frontend converts base64 to Blob and triggers browser download
- Reuses existing color schemes and rating descriptions
- Proper HTML escaping for security (SecurityElement.Escape)
- Print-optimized layout with embedded styles

## File Changes

- `SustainabilityService.cs`: +170 lines (GeneratePdfReport + GeneratePdfHtml)
- `ISustainabilityService.cs`: +1 line (interface method)
- `SustainabilityTab.cs`: +30 lines (ExportReportAsPdf PageCommand)
- `SustainabilityTabTemplate.tsx`: +28 lines (PDF download handler)
- `types.ts`: +1 line (ExportReportAsPdf command)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Improve visual hierarchy and UX by adding appropriate icons to buttons:
- "Export as PDF" → xp-download icon
- "View History" → xp-clock icon
- "Run New Analysis" → xp-rotate-right icon

Buttons now have better visual affordance and follow XbyK design patterns.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Complete the icon coverage for all interactive buttons:
- "Back to Current Report" → xp-arrow-left icon
- "Load More History" → xp-chevron-down icon
- "Show X more" / "Show less" → xp-chevron-down / xp-chevron-up icons (dynamic)

All buttons now have consistent visual indicators that improve UX and
follow Kentico admin UI design patterns.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add xp-file-pdf icon to Export PDF button
- Add xp-clock icon to View History button
- Add xp-rotate-right icon to Run New Analysis button
- Add xp-arrow-left icon to Back to Current Report button
- Add xp-chevron-down/up trailing icons to Load More History button
- Add xp-chevron-down/up trailing icons to Show more/less buttons
- Position expansion/pagination icons on right side using trailingIcon prop

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add manual isExportingPdf state to track PDF generation progress
- Set disabled and inProgress props on Export as PDF button
- Reset state in both after and onError callbacks
- Document usePageCommand loading state pattern in CLAUDE.md
  - Clarify that usePageCommand does NOT provide built-in inProgress property
  - Add complete code example showing correct useState pattern
  - Reference existing implementations in codebase

This ensures the Export as PDF button shows a spinner and prevents
multiple clicks during PDF generation, matching the Run New Analysis
button behavior.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Remove standalone Screenshots section
- Embed main screenshot in Sustainability Report feature section
- Embed history screenshot in Historical Tracking feature section
- Update main sustainability report screenshot with new UI buttons
  (Export as PDF, View History, Run New Analysis with icons)
- Add Report History screenshot showing trend charts and historical reports

This improves documentation flow by placing screenshots directly
in their relevant feature sections rather than separating them.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Document PDF export capability in Sustainability Report section
- Clarify that reports can be downloaded as PDF for sharing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Update carbon rating text to match UI (e.g., "Extremely efficient" vs "Exceptional - Industry Leading")
- Add "Carbon Rating" label and secondary descriptive text to hero section
- Fix page title display: use WebPageItemName (display name) instead of ContentItemName (codename)
- Improve PDF filename: include sanitized page title (max 50 chars) with timestamp
- Add CSS page-break rules to prevent resource groups from splitting across pages
- Add clickable GitHub repository link to PDF footer

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
The filename now just uses the page title and timestamp
(e.g., "Coffee-Processing-Techniques-2025-11-25-143000.pdf")
instead of the redundant "sustainability-report-" prefix.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Improves separation of concerns by moving PDF generation logic
from SustainabilityService into a new SustainabilityPdfService.

Changes:
- Created ISustainabilityPdfService interface and implementation
- Moved GeneratePdfReport and GeneratePdfHtml methods to new service
- Registered ISustainabilityPdfService in DI container
- Updated SustainabilityTab to inject and use ISustainabilityPdfService
- Removed PDF generation methods from ISustainabilityService

Benefits:
- Single Responsibility: Each service has one clear purpose
- Testability: PDF generation can be tested in isolation
- Maintainability: PDF features are contained in dedicated service
- Reduced complexity: SustainabilityService is now ~190 lines smaller

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Improves PDF layout by ensuring the Resource Breakdown section
always starts cleanly at the top of page 2 using CSS page-break.

Changes:
- Added .resource-breakdown class with page-break-before: always
- Removed margin-top from resource breakdown div (not needed with page break)
- Keeps summary metrics clean on page 1, detailed breakdown on page 2+

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings November 25, 2025 21:31
@liamgold liamgold linked an issue Nov 25, 2025 that may be closed by this pull request
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds PDF export functionality to the Sustainability tab, enabling users to download comprehensive sustainability reports as PDF documents. The implementation follows a clean separation of concerns by extracting PDF generation into a dedicated service, while maintaining consistent UI patterns for loading states and button interactions.

  • Introduces ISustainabilityPdfService for PDF generation using Playwright
  • Adds export button with proper loading states and icons to the Sustainability tab UI
  • Enhances documentation with usage patterns for usePageCommand and updated README structure

Reviewed changes

Copilot reviewed 11 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/UIPages/SustainabilityTab/SustainabilityTab.cs Adds ExportReportAsPdf command that retrieves report data and generates timestamped PDF files
src/Services/SustainabilityPdfService.cs New service implementing PDF generation with HTML templating and Playwright rendering
src/Extensions/SustainabilityServiceCollectionExtensions.cs Registers the new PDF service in DI container
src/Client/src/Sustainability/tab-template/SustainabilityTabTemplate.tsx Implements PDF export UI with manual loading state management and file download
src/Client/src/Sustainability/tab-template/types.ts Adds ExportReportAsPdf command constant
src/Client/src/Sustainability/tab-template/history/HistoryView.tsx Adds chevron icon to Load More History button
src/Client/src/Sustainability/tab-template/current/ResourceGroupCard.tsx Adds chevron icons to expand/collapse buttons
README.md Restructures documentation with embedded screenshots and adds PDF export feature
CLAUDE.md Documents usePageCommand loading state pattern to prevent future issues
.claude/agents/xbyk-component-reference.md Updates agent color configuration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Applies Copilot suggestions for code modernization:

C# Changes:
- Replace Substring(0, n) with range indexer [..n] syntax
- More concise and idiomatic for .NET 8

TypeScript Changes:
- Eliminate intermediate Array allocation in base64 to Uint8Array conversion
- Directly create Uint8Array and populate in loop for better performance

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@liamgold liamgold merged commit 28629c9 into main Nov 25, 2025
5 checks passed
@liamgold liamgold deleted the feature/24-export-pdf branch November 25, 2025 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Export/Share Functionality

2 participants