218 lines
7.2 KiB
PHP
218 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use Dompdf\Dompdf;
|
|
use Dompdf\Options;
|
|
|
|
class TestingController extends BaseController
|
|
{
|
|
use ResponseTrait;
|
|
protected $myLogger;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->myLogger = \Config\Services::mylogger();
|
|
}
|
|
|
|
public function saveForm()
|
|
{
|
|
// Get JSON input
|
|
$data = $this->request->getPost();
|
|
|
|
// For testing: just return the received data
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'Form data received successfully!',
|
|
'data' => $data
|
|
]);
|
|
}
|
|
|
|
public function testcli()
|
|
{ echo "hi";
|
|
$this->myLogger->logme('error', "test log");
|
|
echo "hi 2";
|
|
log_message('error', 'test message for log_message_function');
|
|
echo "hi 3";
|
|
}
|
|
|
|
|
|
public function generatePDF()
|
|
{
|
|
// Sample data - replace with your actual data source
|
|
$data = [
|
|
'NAME' => 'John Doe',
|
|
'GENDER' => 'Male',
|
|
'DOB' => '01/01/1990',
|
|
'RELATION' => 'Self',
|
|
'POLICY_NO' => 'POL123456789',
|
|
'TPA_ID' => 'TPA987654321',
|
|
'POLICY_START_DATE' => '01/01/2024',
|
|
'POLICY_DATE' => '31/12/2024',
|
|
'INSURER_NAME' => 'ABC Insurance Co.',
|
|
'TPA_NAME' => 'XYZ TPA Ltd.',
|
|
'FRONT_CARD' => base_url('assets/images/front-card.jpg'), // Your card images
|
|
'BACK_CARD' => base_url('assets/images/back-card.jpg'),
|
|
'LEVELS' => 'Level 1: 1800-XXX-XXXX<br>Level 2: support@company.com'
|
|
];
|
|
|
|
// Load the HTML template
|
|
$html = $this->loadTemplate($data);
|
|
|
|
// Configure DomPDF options
|
|
$options = new Options();
|
|
$options->set('isRemoteEnabled', true); // Enable loading of remote images
|
|
$options->set('isHtml5ParserEnabled', true);
|
|
$options->set('isPhpEnabled', true);
|
|
$options->set('debugPng', false);
|
|
$options->set('debugKeepTemp', false);
|
|
$options->set('debugCss', false);
|
|
$options->set('debugLayout', false);
|
|
$options->set('debugLayoutLines', false);
|
|
$options->set('debugLayoutBlocks', false);
|
|
$options->set('debugLayoutInline', false);
|
|
$options->set('debugLayoutPaddingBox', false);
|
|
|
|
// Initialize DomPDF
|
|
$dompdf = new Dompdf($options);
|
|
|
|
// Load HTML content
|
|
$dompdf->loadHtml($html);
|
|
|
|
// Set paper size and orientation
|
|
$dompdf->setPaper('A4', 'landscape'); // or 'portrait'
|
|
|
|
// Render PDF
|
|
$dompdf->render();
|
|
|
|
// Output PDF to browser
|
|
$filename = 'ecard_' . date('Y-m-d_H-i-s') . '.pdf';
|
|
$dompdf->stream($filename, ['Attachment' => true]); // Set to false for inline view
|
|
}
|
|
|
|
public function viewPDF()
|
|
{
|
|
// Same as generatePDF but with inline view
|
|
$data = [
|
|
'NAME' => 'John Doe',
|
|
'GENDER' => 'Male',
|
|
'DOB' => '01/01/1990',
|
|
'RELATION' => 'Self',
|
|
'POLICY_NO' => 'POL123456789',
|
|
'TPA_ID' => 'TPA987654321',
|
|
'POLICY_START_DATE' => '01/01/2024',
|
|
'POLICY_DATE' => '31/12/2024',
|
|
'INSURER_NAME' => 'ABC Insurance Co.',
|
|
'TPA_NAME' => 'XYZ TPA Ltd.',
|
|
'FRONT_CARD' => base_url('assets/images/front-card.jpg'),
|
|
'BACK_CARD' => base_url('assets/images/back-card.jpg'),
|
|
'LEVELS' => 'Level 1: 1800-XXX-XXXX<br>Level 2: support@company.com'
|
|
];
|
|
|
|
$html = $this->loadTemplate($data);
|
|
$options = new Options();
|
|
$options->set('isRemoteEnabled', true);
|
|
$options->set('isHtml5ParserEnabled', true);
|
|
|
|
$dompdf = new Dompdf($options);
|
|
$dompdf->loadHtml($html);
|
|
$dompdf->setPaper('A4', 'portrait');
|
|
$dompdf->render();
|
|
|
|
$filename = 'ecard_' . date('Y-m-d_H-i-s') . '.pdf';
|
|
$dompdf->stream($filename, ['Attachment' => false]); // Inline view
|
|
}
|
|
|
|
private function loadTemplate($data)
|
|
{
|
|
// Load your HTML template
|
|
$template = file_get_contents(WRITEPATH . 'e_card_template/common.html');
|
|
|
|
// Replace placeholders with actual data
|
|
foreach ($data as $key => $value) {
|
|
$template = str_replace('{' . $key . '}', $value, $template);
|
|
}
|
|
|
|
return $template;
|
|
}
|
|
|
|
// Alternative method using CodeIgniter's view system
|
|
public function generatePDFWithView()
|
|
{
|
|
$data = [
|
|
'NAME' => 'John Doe',
|
|
'GENDER' => 'Male',
|
|
'DOB' => '01/01/1990',
|
|
'RELATION' => 'Self',
|
|
'POLICY_NO' => 'POL123456789',
|
|
'TPA_ID' => 'TPA987654321',
|
|
'POLICY_START_DATE' => '01/01/2024',
|
|
'POLICY_DATE' => '31/12/2024',
|
|
'INSURER_NAME' => 'ABC Insurance Co.',
|
|
'TPA_NAME' => 'XYZ TPA Ltd.',
|
|
'FRONT_CARD' => base_url('assets/images/front-card.jpg'),
|
|
'BACK_CARD' => base_url('assets/images/back-card.jpg'),
|
|
'LEVELS' => 'Level 1: 1800-XXX-XXXX<br>Level 2: support@company.com'
|
|
];
|
|
|
|
// Load view with data
|
|
$html = view('ecard_template', $data);
|
|
|
|
$options = new Options();
|
|
$options->set('isRemoteEnabled', true);
|
|
$options->set('isHtml5ParserEnabled', true);
|
|
|
|
$dompdf = new Dompdf($options);
|
|
$dompdf->loadHtml($html);
|
|
$dompdf->setPaper('A4', 'portrait');
|
|
$dompdf->render();
|
|
|
|
$filename = 'ecard_' . date('Y-m-d_H-i-s') . '.pdf';
|
|
$dompdf->stream($filename, ['Attachment' => true]);
|
|
}
|
|
|
|
public function generatePDFById($id)
|
|
{
|
|
// Get data from database
|
|
$ecardData = [];
|
|
|
|
if (!$ecardData) {
|
|
throw new \CodeIgniter\Exceptions\PageNotFoundException('E-card not found');
|
|
}
|
|
|
|
$data = [
|
|
'NAME' => $ecardData['name'],
|
|
'GENDER' => $ecardData['gender'],
|
|
'DOB' => $ecardData['dob'],
|
|
'RELATION' => $ecardData['relation'],
|
|
'POLICY_NO' => $ecardData['policy_no'],
|
|
'TPA_ID' => $ecardData['tpa_id'],
|
|
'POLICY_START_DATE' => $ecardData['policy_start_date'],
|
|
'POLICY_DATE' => $ecardData['policy_end_date'],
|
|
'INSURER_NAME' => $ecardData['insurer_name'],
|
|
'TPA_NAME' => $ecardData['tpa_name'],
|
|
'FRONT_CARD' => base_url('assets/images/front-card.jpg'),
|
|
'BACK_CARD' => base_url('assets/images/back-card.jpg'),
|
|
'LEVELS' => 'Level 1: 1800-XXX-XXXX<br>Level 2: support@company.com'
|
|
];
|
|
|
|
$html = view('ecard_template', $data);
|
|
|
|
$options = new Options();
|
|
$options->set('isRemoteEnabled', true);
|
|
$options->set('isHtml5ParserEnabled', true);
|
|
|
|
$dompdf = new Dompdf($options);
|
|
$dompdf->loadHtml($html);
|
|
$dompdf->setPaper('A4', 'landscape');
|
|
$dompdf->render();
|
|
|
|
$filename = 'ecard_' . $ecardData['policy_no'] . '_' . date('Y-m-d') . '.pdf';
|
|
$dompdf->stream($filename, ['Attachment' => true]);
|
|
}
|
|
|
|
}
|