PHPWord - corrupted Word file - php

I want to download a word file from an existing template using PHPWord.
I tried this code but it generate corrupted file:
$file = ROOT . '/web/templates/recu.docx';
$PHPWord = new \PhpOffice\PhpWord\TemplateProcessor($file);
$PHPWord->setValue('Value1', date('d/m/Y'));
$PHPWord->setValue('Value2', "ffff");
$PHPWord->setValue('Value3', "sabrine");
$PHPWord->setValue('Value4', utf8_decode("555555"));
$PHPWord->setValue('Value5', "200000");
$new_file = "recu_".date('d_m_Y_H_i');
$PHPWord->saveAs(ROOT. '/web/uploads/recu/'.$new_file.'.docx');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="'.$new_file.'.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord,
'Word2007');
ob_clean();
$objWriter->save('php://output');
exit;
any suggestions why my code didn't work ?

Try this content header instead:
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');

Related

PHPWord Download Error (the office open XML file cannot be opened because there are problems with the contents)

I am having a problem with my PHPWord implementation. I am building a feature that will allow users to download content to word and am using PHPWord for this. However, after the document is downloaded I am getting an error while opening:
the office open XML file cannot be opened because there are problems with the contents
I'm only able to preview the contents of the word file after the accepting the recovery procedure, which I think is not something user friendly.
Here is my PHP code.
<?php
require_once '../assets/vendor/autoload.php';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, "Content");
header('Content-Description: File Transfer');
header("Content-Type: application/docx");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment;filename="test.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('test.docx');
?>
This is a very common issue for PHPWord.Hope the following code fragment will solve your problem as it solves mine.
$doc_filename = "Test_Report_". date("d-m-Y").".docx";
// Save file
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$temp_file_uri = tempnam('', 'anytext');
$objWriter->save($temp_file_uri);
//download code
header('Content-Description: File Transfer');
header("Content-Type: application/docx");//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$doc_filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Length: ' . filesize($temp_file_uri));
readfile($temp_file_uri);
unlink($temp_file_uri); // deletes the temporary file
exit;
-Thanks

PHPExcel excel2007 php output with charts

I use latest PHPExcel library for creating .xls, .xlsx files. When i try to output created file like this:
ob_end_clean();
$file = 'test.xls';
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save('php://output');
exit;
everything is fine until I try to create and output any charts. So i changed my code according to example:
ob_end_clean();
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save(str_replace('.php', '.xlsx', __FILE__));
exit;
This is working, but the file is not downloaded. So i modified code to output created file:
ob_end_clean();
$file = 'test.xlsx';
header('Content-Type: Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save('php://output');
exit;
And this is not working, ends up with ERR_INVALID_RESPONSE.
Also when i store excel file with charts with second code block, it's not compatibile with MS Excel 2016. It says the file is corrupted and the data were replaced or deleted. What am I missing? Thanks a lot.
I figured out one simple solution, first step is to store created .xlsx file, second is to download file with header("Location:"). Redundant files are deleted with array_map function.
array_map('unlink', glob( __DIR__."/*.xlsx"));
$file = 'test.xlsx';
$objPHPWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objPHPWriter->setIncludeCharts(TRUE);
$objPHPWriter->save($file);
header("Location: $file");

Unable to download word docx using PHPWord TemplateProcessor

I can generate download a word document generated from scratch using PHPWord:
Create the PhpWord object:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
add sections and rows (ommitted) and create the header:
$datetime = date('Y_M_d_G_i');
$filename = 'receipt_' . $datetime . '.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
create a writer out of the phpWord object:
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
write to output:
$xmlWriter->save("php://output");
What I have failed to do is download a document from a template on the server:
Creating the TemplateProcessor works: No errors and PHP recognizes $templateProcessor as an object of that class:
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor(asset_url() . 'templates/receipt_template.docx');
But I cannot work out how to write to output. If I could generate a PhpWord object then I could use the IOFactory:: createWriter method, but TemplateProcessor does not have methods that return a PhpWord object.
The closest I can get is to attempt to create a $phpWord document out of the IOFactory::load. But this just creates a blank document
$phpWord = \PhpOffice\PhpWord\IOFactory::load($templateProcessor->save());
<?
require_once "../include/PHPWord-develop/bootstrap.php";
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('contrato01.docx');
$templateProcessor->setValue('variable01', 'Sun');
$templateProcessor->setValue('variable02', 'Mercury');
//#####################################################
// Save File
//#####################################################
//#####################################################
header("Content-Disposition: attachment; filename='ejemplo.docx'");
$templateProcessor->saveAs('php://output');
//#####################################################
//#####################################################
?>

create pdf to docx file using PHPword

I am using for creating a .docx file PHP word, I have one pdf file .its creating .docx file but when I tried to open this file in window machine its not opening. On Linux machine its showing correctly . I tried many thing for that.
That is my code:-
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::import('application.extensions.PHPWord.PHPWord',true);
require_once('PHPWord/PHPWord.php');
$PHPWord = new PHPWord();
spl_autoload_register(array('YiiBase','autoload'));
$document = $PHPWord->loadTemplate("/var/www/frontsite/agemodern/uploads/roadmaps/".$file);
$document->setValue('Value1', 'Sun');
$newfile='ageModern Roadmap - '.date('m-d-Y',time()).' - '.time().'.docx';
$document = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$document->save("/var/www/frontsite/agemodern/uploads/roadmaps/".$newfile);
and this is my phpword file code:-
$zip_name=$strFilename;
if(file_exists($zip_name))
{
$filename=$zip_name;
//header("Content-Type: application/force-download");
header('Content-Type: application/octet-stream');
//header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=\"{$zip_name}\"");
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
header("Content-Transfer-Encoding: binary ");
readfile($filename);
//readfile($zip_name);
// remove zip file is exists in temp path
unlink($filename);
}
Thank you in advance

Auto download the file attachment using PHPWord

I'm trying to use PHPWord to generate word documents. And the document can be generated successfully. But there is a problem where my generated word document will be saved on the server. How can I make it available to download straight away?
Sample:
$PHPWord = new PHPWord();
//Searching for values to replace
$document = $PHPWord->loadTemplate('doc/Temp1.docx');
$document->setValue('Name', $Name);
$document->setValue('No', $No);
$document->save('php://output'); //it auto save into my 'doc' directory.
How can i link to the header to download it as follows:
header("Content-Disposition: attachment; filename='php://output'"); //not sure how to link this filename to the php://output..
Kindly advise.
php://output is a write-only stream, that writes to your screen (like echo).
So, $document->save('php://output'); will not save the file anywhere on the server, it will just echo it out.
Seems, $document->save, doesn't support stream wrappers, so it literally made a file called "php://output". Try using another file name (I suggest a temp file, as you just want to echo it out).
$temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
$document->save($temp_file);
In the header, the filename field is what PHP tells the browser the file is named, it doesn't have to be a name of a file on the server. It's just the name the browser will save it as.
header("Content-Disposition: attachment; filename='myFile.docx'");
So, putting it all together:
$PHPWord = new PHPWord();
//Searching for values to replace
$document = $PHPWord->loadTemplate('doc/Temp1.docx');
$document->setValue('Name', $Name);
$document->setValue('No', $No);
// // save as a random file in temp file
$temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
$document->save($temp_file);
// Your browser will name the file "myFile.docx"
// regardless of what it's named on the server
header("Content-Disposition: attachment; filename='myFile.docx'");
readfile($temp_file); // or echo file_get_contents($temp_file);
unlink($temp_file); // remove temp file
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$filename = 'MyFile.docx';
$objWriter->save($filename);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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: ' . filesize($filename));
flush();
readfile($filename);
unlink($filename); // deletes the temporary file
exit;
This is work for me:
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007', $download = true);
header("Content-Disposition: attachment; filename='File.docx'");
$objWriter->save("php://output");
now whit Ver 0.13.0
https://github.com/PHPOffice/PHPWord
<?
require_once "../include/PHPWord-develop/bootstrap.php";
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template.docx');
$templateProcessor->setValue('var01', 'Sun');
$templateProcessor->setValue('var02', 'Mercury');
//#####################################################
// Save File
//#####################################################
//#####################################################
header("Content-Disposition: attachment; filename='output01.docx'");
$templateProcessor->saveAs('php://output');
//#####################################################
//#####################################################
?>
A simple solution for Laravel based on #user3214824 answer.
// ...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
$doc_name = 'fileName.docx';
$objWriter->save($doc_name); // saving in the public path just for testing
return response()->download(public_path($doc_name))->deleteFileAfterSend(true);
Sorry this came in later. I stumbled on this while trying to solve the same problem. I was able to get it working on Laravel 5 using below:
$file_dir = $template_upload_dir.DIRECTORY_SEPARATOR.'filename.docx';
$tags = array();
if (file_exists($file_dir)) {
$templateProcessor = new TemplateProcessor($file_dir);
$tags = $templateProcessor->getVariables();
$replace = array('');
$templateProcessor->setValue($tags, $replace);
$save_file_name = $fullname.'-'.$inv_code.'-'.date('YmdHis').'.docx';
$templateProcessor->saveAs($save_file_name);
return response()->download($save_file_name)->deleteFileAfterSend(true);
}
Hope it helps someone!!!
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
header("Content-Disposition: attachment; filename='myFile.docx'");
$objWriter->save("php://output");

Categories