# JasperReports Configuration for cPanel Linux

## Requirements

### Java Installation
JasperStarter requires Java Runtime Environment (JRE) or Java Development Kit (JDK).

### Check Java Availability
```bash
java -version
```

If Java is not installed, you may need to:
1. Contact cPanel support to install Java
2. Use an alternative PDF generation method
3. Use a VPS instead of shared hosting for Java support

## JasperStarter Installation Options

### Option 1: Install JasperStarter via SSH (if Java is available)

```bash
# Download JasperStarter
cd ~
wget https://github.com/codetaylor/jasperstarter/releases/download/v3.4.0/jasperstarter-3.4.0-bin.zip

# Extract
unzip jasperstarter-3.4.0-bin.zip

# Move to system directory
sudo mv jasperstarter-3.4.0 /opt/jasperstarter

# Create symlink
sudo ln -s /opt/jasperstarter/jasperstarter /usr/bin/jasperstarter

# Make executable
sudo chmod +x /usr/bin/jasperstarter

# Test
jasperstarter --version
```

### Option 2: Use Local Installation in Home Directory

```bash
# Download to home directory
cd ~
wget https://github.com/codetaylor/jasperstarter/releases/download/v3.4.0/jasperstarter-3.4.0-bin.zip

# Extract
unzip jasperstarter-3.4.0-bin.zip

# Update .env file
# JASPERSTARTER_BIN=/home/yourusername/jasperstarter-3.4.0/jasperstarter
```

### Option 3: Alternative PDF Generation (if JasperStarter unavailable)

If JasperStarter cannot be installed on cPanel, consider these alternatives:

#### A. Use Laravel DomPDF (already installed)
```bash
# Already in composer.json: "barryvdh/laravel-dompdf": "^3.1"
```

#### B. Use TCPDF
```bash
composer require tecnickcom/tcpdf
```

#### C. Use Snappy (wkhtmltopdf)
```bash
composer require barryvdh/laravel-snappy
# Requires wkhtmltopdf binary installation
```

## Configuration

### Update .env File
```env
# For system-wide installation
JASPERSTARTER_BIN=/usr/bin/jasperstarter

# For local installation
# JASPERSTARTER_BIN=/home/yourusername/jasperstarter-3.4.0/jasperstarter
```

### Verify JasperStarter Path
```bash
# Check if executable
which jasperstarter

# Test execution
jasperstarter --version
```

## JasperReports Files

### Required Files
Ensure these files are uploaded to the server:
- `storage/app/reports/rapport_fr.jasper`
- `storage/app/reports/rapport_ar.jasper`
- `storage/app/reports/rapport_en.jasper`
- `storage/app/reports/logo.png`
- `storage/app/reports/Amiri-Regular.ttf` (for Arabic support)

### Upload via File Manager
1. Navigate to `storage/app/reports/`
2. Upload all .jasper files
3. Upload logo.png
4. Upload font files

### Upload via FTP/SFTP
```bash
# Upload reports directory
scp -r storage/app/reports/ user@your-domain.com:~/public_html/igen-backend/storage/app/
```

## Troubleshooting

### Issue: Java Not Found
**Error**: `java: command not found`

**Solution**: 
- Contact cPanel support to install Java
- Use alternative PDF generation method
- Consider upgrading to VPS hosting

### Issue: JasperStarter Permission Denied
**Error**: `Permission denied`

**Solution**:
```bash
chmod +x /path/to/jasperstarter
```

### Issue: JasperStarter Not Executing
**Error**: Process failed to execute

**Solution**:
- Check file permissions
- Verify Java is installed
- Check JasperStarter path in .env
- Review Laravel logs: `storage/logs/laravel.log`

### Issue: PDF Generation Timeout
**Error**: Process timeout

**Solution**:
- Increase PHP execution time in php.ini
- Increase timeout in controller
- Optimize JasperReports template

## Alternative: Implement Fallback PDF Generation

If JasperStarter is not available, implement a fallback using DomPDF:

### Create Alternative PDF Controller
```php
// In RapportPdfController.php
public function generateAlternative($id)
{
    $rapport = Rapport::findOrFail($id);
    
    $pdf = PDF::loadView('rapports.pdf', compact('rapport'));
    $pdfPath = storage_path('app/public/rapports/rapport_' . $id . '_alt.pdf');
    $pdf->save($pdfPath);
    
    return response()->json([
        'success' => true,
        'pdf_url' => asset('storage/rapports/' . basename($pdfPath)),
    ]);
}
```

## Recommendations

### For cPanel Shared Hosting
1. **Primary**: Try to install JasperStarter locally in home directory
2. **Fallback**: Implement DomPDF as alternative
3. **Best**: Upgrade to VPS hosting for full Java support

### Testing
1. Test JasperStarter installation via SSH
2. Test PDF generation via API endpoint
3. Verify PDF files are created in storage/app/public/rapports/
4. Test PDF download from Flutter app
