PHPOffice Word generator problem when opening file - php

Whenever I try to download a file generated by PHPWord, this error pops up

You can download source from here - "https://github.com/PHPOffice/PHPWord" or also prefar this one 'https://buildmedia.readthedocs.org/media/pdf/phpword/latest/phpword.pdf'
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpword, 'Word2007');
$filename = 'helloWorld'.date("h:i:s").'.docx';
$objWriter->save($filename);
May be issue with your ms word. try with online view or other way.
Try also below one
1.Add setCompatibility() before createWriter()
\PhpOffice\PhpWord\Settings::setCompatibility(false);
2. Add exit; after the save()
// XML Writer compatibility
\PhpOffice\PhpWord\Settings::setCompatibility(false);
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
ob_clean();
$objWriter->save('php://output');
exit;
Just add ob_clean(); before output it!

Related

How to convert xls to xlsx in codeigniter

I am using PHPExcel for downloading the file as xlsx format. I am using codeigniter framework. Previously i am downloading xls format. At that time the file is downloaded successfully. If i am trying for downloading as xlsx format it gives the error as below image.enter image description here
My code is :
$this->load->library('Excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('Sales Data');
$filename='Salesdata.xlsx';
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition:attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
There is no error when i am adding as xls format. It gives the error when i am adding as filename.xlsx. Any solution please.
you may continue with php Output buffers and codeigniter download helper
for example :-
$filename='Salesdata.xlsx';
ob_start();
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
$this->load->helper('download');
$excelFileContents = ob_get_clean();
force_download($filename, $excelFileContents);

PHPExcel can't download the file

I wrote a PHPExcel function. With $objWriter->save('plan.xls'); it works perfectly on the server. The problem is that I need to download the file in the browser. So I have to take $objWriter->save("php://output");. The result is that I get a lot of the following weird characters in the browser:
��ࡱ�;�� ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Have someone an idea why I get this result and not the correct file as download?
My code at the moment: (At the moment it is a empty file for testing).
function loadTableToExcel(){
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="filename.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
//$objWriter->save('plan.xls');
$excel->disconnectWorksheets();
unset($excel);
}

PHPExcel: Automatically download and open an Excel file

i have managed to create and save an excel file:
// Rename the file
$fileName = URL . "MODEL/case" . $caseNO . ".xlsx";
// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);
I would like now PHPExcel run Excel automatically, open the file created and maximize it.
Is it possible? Will this work even if Excel is already running?
Thank you for your help,
Donato
As per my above comment, you can only force to have a download option. For this you can set headers in this way -
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");
Reference - PHP Excel Reader
For more options you can also check the cheat sheet - Cheat Sheet
Although the best way to read here - Codeplex
EDIT
Do something like this -
$excel = new PHPExcel();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');
// Do your stuff here
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
// This line will force the file to download
$writer->save('php://output');
PHPExcel can't run MS Excel on the client.... but you can download a file directly to the browser which will offer the client the options of saving it to disk or opening it directly in MS Excel (if they have MS Excel installed) by sending the appropriate http headers, and "saving" the file to php://output.
Of course, if the client doesn't have MS Excel installed, then opening in MS Excel isn't an option; although it will still prompt for save.
The 01simple-download-xlsx.php file in the /Tests or /Examples directory does exactly this
And "yes", it will work if MS Excel is already running on the client
Insert the following headers just before creating the Writer
$filename = "filedetail". date("Y-m-d-H-i-s").".xlsx";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
header("Location: ".URL . "MODEL/case" . $caseNO . ".xlsx");

PHP Excel: Saving created PDF to remote server and not user downlaod

This is similar to my previous question re (DOMPDF), the difference is that now i am using PHPExcel, to create the PDF.
I have tried the file_put method but it downloads an empty PDF. SO where am i going wrong or can this not be done?
below is my end code that outputs the fiel and what i have tried
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="TestSheet.pdf"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
$file_location = $save_Path;
file_put_contents($file_location,$objWriter);
exit;
It seems that once its been saved its clearing the data so hence the blank PDF
Thanks
Discard the headers.... they're all about telling the web browser to expect a PDF file, but you're not sending anything back to the browser (let alone a PDF file).
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
is telling PHPExcel to send the generated PDF directly to the web browser.... you say you don't want to do that, but to save to a remote server instead.
You'll need to intercept the results from $objWriter->save() so it doesn't go to the browser. Capture it using output buffering:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
ob_start();
$objWriter->save('php://output');
$myPdfData = ob_get_contents();
ob_end_clean();
Then you've captured the PDF datastream in $myPdfData and can then do:
file_put_contents($file_location, $myPdfData);
Note that this is likely to be memory-hungry

PHPExcel_IOFactory::createWriter causes wrong behaviour

After having created a PHPExcel program, the final step consists of saving the worksheets, to do that, I want to apply these rules so as to generate the file in Excel 2007:
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="workbook1.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
Instead of getting the Excel2007 file, the ouptut with any kind of browser is something like:
07:31:12 Create new PHPExcel object 07:31:12 Set properties 07:31:12
Add some data 07:31:12 Rename sheet 07:31:12 Write to Excel2007 format
PKæ;È< —![[Content_Types].xml­”MNÃ0…÷=…å-Jܲ#%í‚Â*Q`ìIcÕ±-{úw{&I ˆE
j7±"û½oüg÷œ%”NKë”ü‰Ï¦£by‰]*y„Hª†F¦Üp4SùØH¤ß¸Aªµ\¸ï„òÁa†­ŸŽXñJD£-dÄÙHì­
.....
I tried with various browsers but I get the same result. On the other hand, if I save the file with these lines below, it works fine:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('/usr/local/myWorkbooks/workbook1.xlsx');
Any idea why it does not work through my browser? Thanks in advance for your help
If you're streaming the output to php://output then you must not echo anything else to that output stream.
You're clearly echoing the lines:
07:31:12 Create new PHPExcel object
07:31:12 Set properties
07:31:12 Add some data
07:31:12 Rename sheet
07:31:12 Write to Excel2007 format
to output before the Excel file itself
Remove those echo lines
This is standard PHP behavior, and a question that has been answered here thousands of times in the past: echoes go to php://output.... I'm surprised you're not also getting a "headers already sent" error as well
You have to clean the output buffer and turn off buffering using ob_end_clean() before saving. Do this instead:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="workbook1.xlsx"');
header('Cache-Control: max-age=0');
ob_end_clean();
$objWriter->save('php://output');
exit;

Categories