Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions PDFInvoiceGeneration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# PDF Invoice Generation

This module provides PDF invoice generation functionality for the e-commerce system, allowing orders to be converted to professional-looking PDF invoices.

## Components

### PDFInvoiceGenerator Class
The main invoice generation engine that:
- Creates professional invoice layouts in HTML
- Converts HTML to PDF using DomPDF (if available) or falls back to HTML
- Includes all relevant order information:
- Company details
- Customer information
- Order details and items
- Shipping information
- Detailed totals (subtotal, tax, shipping, discounts, grand total)
- Invoice number and dates

### Order Model Integration
The Order model has been enhanced with:
- `generateInvoice()` method to create PDF invoices
- `getInvoiceLink()` method to retrieve download links
- `getInvoiceFilename()` method to get appropriate filenames

### Invoice Controller
A dedicated controller to handle invoice downloads at `/order/invoice/{order-id}`

## Features

### Professional Layout
- Clean, business-appropriate design
- Organized sections for all invoice information
- Proper alignment and spacing
- Print-ready formatting

### Complete Information
- Company header with contact information
- Customer billing and shipping details
- Detailed order items with quantities and prices
- Complete breakdown of costs (subtotal, tax, shipping, discounts)
- Grand total prominently displayed

### Flexible Output Modes
- Download PDF directly
- View PDF inline in browser
- Generate PDF string for custom handling

### Fallback Mechanism
- Uses DomPDF if available for proper PDF generation
- Falls back to HTML with print suggestions if library missing

## Usage

### In Templates
```ss
<a href="$InvoiceLink" class="btn btn-primary">Download Invoice</a>
```

### In Code
```php
// Generate and download invoice
$order->generateInvoice('download');

// Generate invoice to view inline
$order->generateInvoice('inline');

// Get invoice download link
$link = $order->getInvoiceLink();
```

### Direct Access
Invoices can be accessed directly via:
```
/order/invoice/{order-id}
```

## Requirements

The system works best with the DomPDF library installed:
```
composer require dompdf/dompdf
```

If DomPDF is not available, the system will generate HTML that can be printed to PDF.

## Configuration

The system is configured via YAML:
- Routes for invoice access
- Default settings for PDF generation
- Integration with Order model

## Integration Points

The PDF invoice system integrates seamlessly with:
- The existing Order model
- The e-commerce workflow
- The CMS for administrative access
- Customer-facing pages for download access

This provides a complete solution for generating professional invoices that meet business requirements while maintaining integration with the existing e-commerce system.
31 changes: 31 additions & 0 deletions app/_config/pdf_invoice.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
Name: pdf-invoice-config
---
# Register the PDF invoice functionality

# Add invoice extension to Order model
Order:
extensions:
- OrderInvoiceExtension

Comment on lines +6 to +10
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

Config attaches OrderInvoiceExtension to Order, but Order already defines generateInvoice(), getInvoiceLink(), and getInvoiceFilename() directly. This makes the extension redundant (and can cause confusion about which implementation is used). Prefer keeping these methods in one place: either remove the extension/config or remove the methods from Order and rely on the extension.

Suggested change
# Add invoice extension to Order model
Order:
extensions:
- OrderInvoiceExtension

Copilot uses AI. Check for mistakes.
# Configure routes for invoice downloads
---
Name: invoice-routes
After:
- '#coreroutes'
- '#modelascontrollerroutes'
---
SilverStripe\Control\Director:
rules:
'order/invoice//$ID': 'InvoiceController'

# Define required dependencies
---
Name: dompdf-defaults
---
# Default settings for DomPDF if installed
Dompdf\Dompdf:
options:
default_font: 'Arial'
isRemoteEnabled: true
isJavascriptEnabled: true
Comment on lines +30 to +31
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

This config enables isRemoteEnabled and isJavascriptEnabled for Dompdf by default. With invoice HTML containing order/customer data, enabling remote resources and JS increases SSRF/XSS risk if any field is ever rendered unescaped. Prefer disabling these by default, or strictly limiting remote asset loading to trusted whitelisted domains/paths.

Suggested change
isRemoteEnabled: true
isJavascriptEnabled: true
isRemoteEnabled: false
isJavascriptEnabled: false

Copilot uses AI. Check for mistakes.
Loading
Loading