Here's the updated code with the PDF export functionality fixed:
// Handle PDF export
if (isset($_GET['export_pdf'])) {
// Clear any previous output
if (ob_get_level()) {
ob_end_clean();
}
// Check if dompdf is properly installed
$dompdf_path = ABSPATH . 'wp-content/plugins/dompdf/autoload.inc.php';
if (!file_exists($dompdf_path)) {
die('Dompdf library not found. Please ensure dompdf is installed in: ' . $dompdf_path);
}
require_once($dompdf_path);
try {
// Create PDF document with improved settings
$options = new \Dompdf\Options();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', false); // Disable PHP for security
$options->set('defaultFont', 'helvetica');
$options->set('isFontSubsettingEnabled', true);
$dompdf = new \Dompdf\Dompdf($options);
// Get current date in Philippines timezone
$timezone = new DateTimeZone('Asia/Manila');
$export_date = (new DateTime('now', $timezone))->format('M j, Y g:i A');
// Build HTML content with simplified styling
$html = '
<style>
body { font-family: Arial, sans-serif; margin: 15px; }
h1 { text-align: center; color: #333; margin-bottom: 5px; }
h2 { text-align: center; color: #555; margin-top: 0; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th { background-color: #D1B48C; color: #000; padding: 8px; text-align: left; }
td { padding: 8px; border-bottom: 1px solid #ddd; }
.text-right { text-align: right; }
.total-row { font-weight: bold; background-color: #f5f5f5; }
</style>
<h1>ArKi Inventory</h1>
<h2>Sales Report</h2>
<p style="text-align: center;">Exported on: ' . $export_date . '</p>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Date Sold</th>
<th>Category</th>
<th>Unit</th>
<th class="text-right">Unit Price</th>
<th class="text-right">Qty Sold</th>
<th class="text-right">Product Revenue</th>
<th class="text-right">Product Profit</th>
</tr>
</thead>
<tbody>';
foreach ($sales_data as $sale) {
$html .= '
<tr>
<td>' . htmlspecialchars($sale['product_name']) . '</td>
<td>' . htmlspecialchars($sale['date_sold']) . '</td>
<td>' . htmlspecialchars($sale['category']) . '</td>
<td>' . htmlspecialchars($sale['unit']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($sale['unit_price'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($sale['quantity_sold']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($sale['product_revenue'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($sale['product_profit'], 2) . '</td>
</tr>';
}
$html .= '
<tr class="total-row">
<td colspan="6" class="text-right">Total Product Revenue:</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($total_revenue, 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($total_profit, 2) . '</td>
</tr>
</tbody>
</table>';
// Load HTML to dompdf
$dompdf->loadHtml($html);
// Set paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Generate a filename
$filename = 'ArKi-Sales-Report_' . date('Y-m-d') . '.pdf';
// Output the PDF with proper headers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// Force download
$dompdf->stream($filename, ['Attachment' => 1]);
exit;
} catch (Exception $e) {
die('Error generating PDF: ' . $e->getMessage());
}
}
// Handle PDF export
if (isset($_GET['export_pdf'])) {
// Clear any previous output
if (ob_get_level()) {
ob_end_clean();
}
// Check if dompdf is properly installed
$dompdf_path = ABSPATH . 'wp-content/plugins/dompdf/autoload.inc.php';
if (!file_exists($dompdf_path)) {
die('Dompdf library not found. Please ensure dompdf is installed in: ' . $dompdf_path);
}
require_once($dompdf_path);
try {
// Create PDF document with improved settings
$options = new \Dompdf\Options();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', false); // Disable PHP for security
$options->set('defaultFont', 'helvetica');
$options->set('isFontSubsettingEnabled', true);
$dompdf = new \Dompdf\Dompdf($options);
// Get current date in Philippines timezone
$timezone = new DateTimeZone('Asia/Manila');
$export_date = (new DateTime('now', $timezone))->format('M j, Y g:i A');
// Build HTML content with simplified styling
$html = '
<style>
body { font-family: Arial, sans-serif; margin: 15px; }
h1 { text-align: center; color: #333; margin-bottom: 5px; }
h2 { text-align: center; color: #555; margin-top: 0; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th { background-color: #D1B48C; color: #000; padding: 8px; text-align: left; }
td { padding: 8px; border-bottom: 1px solid #ddd; }
.text-right { text-align: right; }
.total-row { font-weight: bold; background-color: #f5f5f5; }
</style>
<h1>ArKi Inventory</h1>
<h2>Sales Report</h2>
<p style="text-align: center;">Exported on: ' . $export_date . '</p>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Date Sold</th>
<th>Category</th>
<th>Unit</th>
<th class="text-right">Unit Price</th>
<th class="text-right">Qty Sold</th>
<th class="text-right">Product Revenue</th>
<th class="text-right">Product Profit</th>
</tr>
</thead>
<tbody>';
foreach ($sales_data as $sale) {
$html .= '
<tr>
<td>' . htmlspecialchars($sale['product_name']) . '</td>
<td>' . htmlspecialchars($sale['date_sold']) . '</td>
<td>' . htmlspecialchars($sale['category']) . '</td>
<td>' . htmlspecialchars($sale['unit']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($sale['unit_price'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($sale['quantity_sold']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($sale['product_revenue'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($sale['product_profit'], 2) . '</td>
</tr>';
}
$html .= '
<tr class="total-row">
<td colspan="6" class="text-right">Total Product Revenue:</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($total_revenue, 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . number_format($total_profit, 2) . '</td>
</tr>
</tbody>
</table>';
// Load HTML to dompdf
$dompdf->loadHtml($html);
// Set paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Generate a filename
$filename = 'ArKi-Sales-Report_' . date('Y-m-d') . '.pdf';
// Output the PDF with proper headers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// Force download
$dompdf->stream($filename, ['Attachment' => 1]);
exit;
} catch (Exception $e) {
die('Error generating PDF: ' . $e->getMessage());
}
}
Key improvements made:
echo $dompdf->output() to $dompdf->stream() which is more reliable for downloadsAlso, make sure you have the dompdf library properly installed in your WordPress plugins directory (wp-content/plugins/dompdf/). If you're still having issues, check:
The PDF should now download properly when clicking the "Export as PDF" button.
Here's the updated PDF export code that ensures the generated PDF is compatible with all PDF viewers:
// Handle PDF export
if (isset($_GET['export_pdf'])) {
// Clear any previous output
while (ob_get_level()) {
ob_end_clean();
}
// Check if dompdf is properly installed
$dompdf_path = ABSPATH . 'wp-content/plugins/dompdf/autoload.inc.php';
if (!file_exists($dompdf_path)) {
die('Dompdf library not found. Please ensure dompdf is installed in: ' . $dompdf_path);
}
require_once($dompdf_path);
try {
// Create PDF document with improved settings for compatibility
$options = new \Dompdf\Options();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', false);
$options->set('defaultFont', 'helvetica');
$options->set('isFontSubsettingEnabled', true);
$options->set('isJavascriptEnabled', false);
$options->set('debugKeepTemp', false);
$options->set('debugCss', false);
$options->set('debugLayout', false);
$dompdf = new \Dompdf\Dompdf($options);
// Get current date in Philippines timezone
$timezone = new DateTimeZone('Asia/Manila');
$export_date = (new DateTime('now', $timezone))->format('M j, Y g:i A');
// Build HTML content with basic styling for maximum compatibility
$html = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
body { font-family: Arial, sans-serif; margin: 15px; }
h1 { text-align: center; color: #333; margin-bottom: 5px; }
h2 { text-align: center; color: #555; margin-top: 0; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th { background-color: #D1B48C; color: #000; padding: 8px; text-align: left; }
td { padding: 8px; border-bottom: 1px solid #ddd; }
.text-right { text-align: right; }
.total-row { font-weight: bold; background-color: #f5f5f5; }
</style>
</head>
<body>
<h1>ArKi Inventory</h1>
<h2>Sales Report</h2>
<p style="text-align: center;">Exported on: ' . $export_date . '</p>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Date Sold</th>
<th>Category</th>
<th>Unit</th>
<th class="text-right">Unit Price</th>
<th class="text-right">Qty Sold</th>
<th class="text-right">Product Revenue</th>
<th class="text-right">Product Profit</th>
</tr>
</thead>
<tbody>';
foreach ($sales_data as $sale) {
$html .= '
<tr>
<td>' . htmlspecialchars($sale['product_name']) . '</td>
<td>' . htmlspecialchars($sale['date_sold']) . '</td>
<td>' . htmlspecialchars($sale['category']) . '</td>
<td>' . htmlspecialchars($sale['unit']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['unit_price'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($sale['quantity_sold']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['product_revenue'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['product_profit'], 2) . '</td>
</tr>';
}
$html .= '
<tr class="total-row">
<td colspan="6" class="text-right">Total Product Revenue:</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($total_revenue, 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($total_profit, 2) . '</td>
</tr>
</tbody>
</table>
</body>
</html>';
// Load HTML to dompdf
$dompdf->loadHtml($html, 'UTF-8');
// Set paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Generate a filename
$filename = 'ArKi-Sales-Report_' . date('Y-m-d') . '.pdf';
// Output the PDF with proper headers
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . strlen($dompdf->output()));
// Output the PDF and force download
echo $dompdf->output();
exit;
} catch (Exception $e) {
die('Error generating PDF: ' . $e->getMessage());
}
}
// Handle PDF export
if (isset($_GET['export_pdf'])) {
// Clear any previous output
while (ob_get_level()) {
ob_end_clean();
}
// Check if dompdf is properly installed
$dompdf_path = ABSPATH . 'wp-content/plugins/dompdf/autoload.inc.php';
if (!file_exists($dompdf_path)) {
die('Dompdf library not found. Please ensure dompdf is installed in: ' . $dompdf_path);
}
require_once($dompdf_path);
try {
// Create PDF document with improved settings for compatibility
$options = new \Dompdf\Options();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', false);
$options->set('defaultFont', 'helvetica');
$options->set('isFontSubsettingEnabled', true);
$options->set('isJavascriptEnabled', false);
$options->set('debugKeepTemp', false);
$options->set('debugCss', false);
$options->set('debugLayout', false);
$dompdf = new \Dompdf\Dompdf($options);
// Get current date in Philippines timezone
$timezone = new DateTimeZone('Asia/Manila');
$export_date = (new DateTime('now', $timezone))->format('M j, Y g:i A');
// Build HTML content with basic styling for maximum compatibility
$html = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
body { font-family: Arial, sans-serif; margin: 15px; }
h1 { text-align: center; color: #333; margin-bottom: 5px; }
h2 { text-align: center; color: #555; margin-top: 0; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th { background-color: #D1B48C; color: #000; padding: 8px; text-align: left; }
td { padding: 8px; border-bottom: 1px solid #ddd; }
.text-right { text-align: right; }
.total-row { font-weight: bold; background-color: #f5f5f5; }
</style>
</head>
<body>
<h1>ArKi Inventory</h1>
<h2>Sales Report</h2>
<p style="text-align: center;">Exported on: ' . $export_date . '</p>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Date Sold</th>
<th>Category</th>
<th>Unit</th>
<th class="text-right">Unit Price</th>
<th class="text-right">Qty Sold</th>
<th class="text-right">Product Revenue</th>
<th class="text-right">Product Profit</th>
</tr>
</thead>
<tbody>';
foreach ($sales_data as $sale) {
$html .= '
<tr>
<td>' . htmlspecialchars($sale['product_name']) . '</td>
<td>' . htmlspecialchars($sale['date_sold']) . '</td>
<td>' . htmlspecialchars($sale['category']) . '</td>
<td>' . htmlspecialchars($sale['unit']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['unit_price'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($sale['quantity_sold']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['product_revenue'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['product_profit'], 2) . '</td>
</tr>';
}
$html .= '
<tr class="total-row">
<td colspan="6" class="text-right">Total Product Revenue:</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($total_revenue, 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($total_profit, 2) . '</td>
</tr>
</tbody>
</table>
</body>
</html>';
// Load HTML to dompdf
$dompdf->loadHtml($html, 'UTF-8');
// Set paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Generate a filename
$filename = 'ArKi-Sales-Report_' . date('Y-m-d') . '.pdf';
// Output the PDF with proper headers
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . strlen($dompdf->output()));
// Output the PDF and force download
echo $dompdf->output();
exit;
} catch (Exception $e) {
die('Error generating PDF: ' . $e->getMessage());
}
}
Key improvements for compatibility:
Also, make sure to:
The PDF should now be properly formatted and compatible with all PDF viewers including Microsoft Edge, Adobe Reader, and others.
The errors indicate two main issues: undefined $currency_symbol variable and headers already sent. Here's the fixed version that will work properly:
// Handle PDF export
if (isset($_GET['export_pdf'])) {
// Clear any previous output
while (ob_get_level()) {
ob_end_clean();
}
// Check if dompdf is properly installed
$dompdf_path = ABSPATH . 'wp-content/plugins/dompdf/autoload.inc.php';
if (!file_exists($dompdf_path)) {
die('Dompdf library not found. Please ensure dompdf is installed in: ' . $dompdf_path);
}
require_once($dompdf_path);
try {
// Get currency symbol (define if not already set)
if (!isset($currency_symbol)) {
$currency_symbol = function_exists('get_woocommerce_currency_symbol') ?
get_woocommerce_currency_symbol() : '₱';
}
// Create PDF document with improved settings for compatibility
$options = new \Dompdf\Options();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', false);
$options->set('defaultFont', 'helvetica');
$options->set('isFontSubsettingEnabled', true);
$options->set('isJavascriptEnabled', false);
$options->set('debugKeepTemp', false);
$options->set('debugCss', false);
$options->set('debugLayout', false);
$dompdf = new \Dompdf\Dompdf($options);
// Get current date in Philippines timezone
$timezone = new DateTimeZone('Asia/Manila');
$export_date = (new DateTime('now', $timezone))->format('M j, Y g:i A');
// Build HTML content with basic styling for maximum compatibility
$html = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
body { font-family: Arial, sans-serif; margin: 15px; }
h1 { text-align: center; color: #333; margin-bottom: 5px; }
h2 { text-align: center; color: #555; margin-top: 0; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th { background-color: #D1B48C; color: #000; padding: 8px; text-align: left; }
td { padding: 8px; border-bottom: 1px solid #ddd; }
.text-right { text-align: right; }
.total-row { font-weight: bold; background-color: #f5f5f5; }
</style>
</head>
<body>
<h1>ArKi Inventory</h1>
<h2>Sales Report</h2>
<p style="text-align: center;">Exported on: ' . $export_date . '</p>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Date Sold</th>
<th>Category</th>
<th>Unit</th>
<th class="text-right">Unit Price</th>
<th class="text-right">Qty Sold</th>
<th class="text-right">Product Revenue</th>
<th class="text-right">Product Profit</th>
</tr>
</thead>
<tbody>';
foreach ($sales_data as $sale) {
$html .= '
<tr>
<td>' . htmlspecialchars($sale['product_name']) . '</td>
<td>' . htmlspecialchars($sale['date_sold']) . '</td>
<td>' . htmlspecialchars($sale['category']) . '</td>
<td>' . htmlspecialchars($sale['unit']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['unit_price'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($sale['quantity_sold']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['product_revenue'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['product_profit'], 2) . '</td>
</tr>';
}
$html .= '
<tr class="total-row">
<td colspan="6" class="text-right">Total Product Revenue:</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($total_revenue, 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($total_profit, 2) . '</td>
</tr>
</tbody>
</table>
</body>
</html>';
// Load HTML to dompdf
$dompdf->loadHtml($html, 'UTF-8');
// Set paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Generate a filename
$filename = 'ArKi-Sales-Report_' . date('Y-m-d') . '.pdf';
// Output the PDF with proper headers
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// Output the PDF and force download
echo $dompdf->output();
exit;
} catch (Exception $e) {
// Ensure no output has been sent before showing error
while (ob_get_level()) {
ob_end_clean();
}
die('Error generating PDF: ' . $e->getMessage());
}
}
// Handle PDF export
if (isset($_GET['export_pdf'])) {
// Clear any previous output
while (ob_get_level()) {
ob_end_clean();
}
// Check if dompdf is properly installed
$dompdf_path = ABSPATH . 'wp-content/plugins/dompdf/autoload.inc.php';
if (!file_exists($dompdf_path)) {
die('Dompdf library not found. Please ensure dompdf is installed in: ' . $dompdf_path);
}
require_once($dompdf_path);
try {
// Get currency symbol (define if not already set)
if (!isset($currency_symbol)) {
$currency_symbol = function_exists('get_woocommerce_currency_symbol') ?
get_woocommerce_currency_symbol() : '₱';
}
// Create PDF document with improved settings for compatibility
$options = new \Dompdf\Options();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', false);
$options->set('defaultFont', 'helvetica');
$options->set('isFontSubsettingEnabled', true);
$options->set('isJavascriptEnabled', false);
$options->set('debugKeepTemp', false);
$options->set('debugCss', false);
$options->set('debugLayout', false);
$dompdf = new \Dompdf\Dompdf($options);
// Get current date in Philippines timezone
$timezone = new DateTimeZone('Asia/Manila');
$export_date = (new DateTime('now', $timezone))->format('M j, Y g:i A');
// Build HTML content with basic styling for maximum compatibility
$html = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
body { font-family: Arial, sans-serif; margin: 15px; }
h1 { text-align: center; color: #333; margin-bottom: 5px; }
h2 { text-align: center; color: #555; margin-top: 0; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th { background-color: #D1B48C; color: #000; padding: 8px; text-align: left; }
td { padding: 8px; border-bottom: 1px solid #ddd; }
.text-right { text-align: right; }
.total-row { font-weight: bold; background-color: #f5f5f5; }
</style>
</head>
<body>
<h1>ArKi Inventory</h1>
<h2>Sales Report</h2>
<p style="text-align: center;">Exported on: ' . $export_date . '</p>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Date Sold</th>
<th>Category</th>
<th>Unit</th>
<th class="text-right">Unit Price</th>
<th class="text-right">Qty Sold</th>
<th class="text-right">Product Revenue</th>
<th class="text-right">Product Profit</th>
</tr>
</thead>
<tbody>';
foreach ($sales_data as $sale) {
$html .= '
<tr>
<td>' . htmlspecialchars($sale['product_name']) . '</td>
<td>' . htmlspecialchars($sale['date_sold']) . '</td>
<td>' . htmlspecialchars($sale['category']) . '</td>
<td>' . htmlspecialchars($sale['unit']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['unit_price'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($sale['quantity_sold']) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['product_revenue'], 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($sale['product_profit'], 2) . '</td>
</tr>';
}
$html .= '
<tr class="total-row">
<td colspan="6" class="text-right">Total Product Revenue:</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($total_revenue, 2) . '</td>
<td class="text-right">' . htmlspecialchars($currency_symbol) . ' ' . number_format($total_profit, 2) . '</td>
</tr>
</tbody>
</table>
</body>
</html>';
// Load HTML to dompdf
$dompdf->loadHtml($html, 'UTF-8');
// Set paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Generate a filename
$filename = 'ArKi-Sales-Report_' . date('Y-m-d') . '.pdf';
// Output the PDF with proper headers
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// Output the PDF and force download
echo $dompdf->output();
exit;
} catch (Exception $e) {
// Ensure no output has been sent before showing error
while (ob_get_level()) {
ob_end_clean();
}
die('Error generating PDF: ' . $e->getMessage());
}
}
Key fixes made:
$currency_symbol variable and defined it if not setwhile (ob_get_level()) to ensure all buffers are clearedThis should now:
Make sure you have the dompdf library properly installed in your WordPress plugins directory (wp-content/plugins/dompdf/). If you're still having issues, you might want to check your PHP error logs for any other underlying problems._