What are some considerations for choosing between different file formats like XLSX or PDF when saving PHP-generated tables?
When choosing between different file formats like XLSX or PDF for saving PHP-generated tables, consider the intended use of the file. If the data needs to be easily editable and structured in a tabular format, XLSX might be a better choice. On the other hand, if the goal is to preserve the formatting and layout of the table, PDF could be more suitable.
// Example code snippet for saving a PHP-generated table as XLSX or PDF
// Generate table data
$tableData = [
['Name', 'Age', 'Email'],
['John Doe', 30, 'john.doe@example.com'],
['Jane Smith', 25, 'jane.smith@example.com'],
];
// Save table as XLSX
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray($tableData, NULL, 'A1');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('table.xlsx');
// Save table as PDF
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->AddPage();
$pdf->writeHTML('<table border="1">' . implode('', array_map(function($row) {
return '<tr>' . implode('', array_map(function($cell) {
return '<td>' . $cell . '</td>';
}, $row)) . '</tr>';
}, $tableData)) . '</table>');
$pdf->Output('table.pdf', 'F');