I am generating .xlsx, .ods, .pdf and .cvs files on some large data arrays using PHPExcel (with DomPDF for the .pdf). I followed the examples on their github page and the stackoverflow posts, if there were problems. I managed to get the .csv export working, but the rest is not.
When running .xlsx export i get a .xlsx file that cannot be opened on my mac and neither in google docs. So i commented all header() calls in php out in order to watch for any PHP error messages. Turns out there are none and i am getting the .xlsx, .pdf or .ods binary as expected in the browser, even though i can't open them later. So it seems my scripts are working yet somehow producing corrupted binaries.
What am i doing wrong here? The following code is for my .xlsx export, the code for .pdf / .ods export is nearly similar.
$excel = new PHPExcel();
// Now set some meta data for that object
// ...
$cellRow = 2;
$cellColumn = "B";
// Add data to the excel file
foreach($entries as $entry) {
// ...
$cellColumn = "B";
$cellRow += 1;
}
// Export
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="export_' . $dateGenerated . '.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: ' . gmdate('D, d M Y H:i:s'));
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
saveViaTempFile($writer);
The saveViaTempFile() function is taken from this stackoverflow post: PHPExcel_Writer_Exception with message "Could not close zip file php://output." to avoid some other problems.
TLDR: I am generating .xlsx, .ods, .pdf binaries, code should be correct. Binaries are somehow corrupted. What am i doing wrong? Thanks!
EDIT: As requested some more information:
I am using PHPExcel 1.8.1. Installed with composer and imported in my local script with require "vendor/autoload.php";
I am using error_reporting(E_ALL);
and not surpressing anything. Still there is no single Notice / Error shown at all. It just simply generates the .xlsx which i am then not being able to open.
The function saveVieTempFile() i am using is the following one and straight copied from the linked SO thread above:
static function saveViaTempFile($objWriter) {
$filePath = "tmp/" . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp";
$objWriter->save($filePath);
readfile($filePath);
unlink($filePath);
}
And yes, i set CHMOD(777) to the /tmp directory. Otherwise i wouldn't been able to actually receive binary output.
I even tested if only adding extremely simple data to the file works like so:
$excel->setActiveSheetIndex(0)->setCellValue("A3", "hello");
$excel->setActiveSheetIndex(0)->setCellValue("A4", "world");
$excel->setActiveSheetIndex(0)->setCellValue("A5", "this");
$excel->setActiveSheetIndex(0)->setCellValue("A6", "isn't");
$excel->setActiveSheetIndex(0)->setCellValue("C1", "working");
$excel->setActiveSheetIndex(0);
Turns out even this easy data isn't exported properly and i am not able to open this file on my mac or with google docs afterwards.
For more information, just ask in the comments and i will update this post.