Codeigniter 4 with PHPWord loadTemplate - php

I have a problem loading a template doc in Codeigniter using PHPWord. The code below works but the downloaded word file is empty. I think the problem is that the location of the template is incorrect. Can anyone show/teach me how should I properly locate the template file. The template file file is located in public/template/ folder.
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$document = $phpWord->loadTemplate(./template/filedoc.docx);
$document->setValue('fullName', 'John Doe');
$document->setValue('date', date("F j Y"));
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="output.docx"');
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');
Update:
loadTemplate is now working fine no errors, the problem now is, why is it that the downloaded doc file is empty. Can anyone help?
$document = new \PhpOffice\PhpWord\TemplateProcessor(template/filedoc.docx)
$document->setValue('fullName', 'John Doe');
$document->setValue('date', date("F j Y"));
$document->saveAs('output.docx');
Change the code based on the current PHPWord documentaion for template processing. The output on the code above is still the same, empty document or rather unreadable by MSWord. Help...

constant APPPATH
The path to the app directory.
Instead of:
// ...
$document = $phpWord->loadTemplate(./template/filedoc.docx); ❌
// ...
Use this:
// ...
$document = $phpWord->loadTemplate(APPPATH . "public/template/filedoc.docx"); ✅
// ...

works for me
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$titleText = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END));
$titleText->addText('helooo ', array('size' => 12, 'rtl' => true));
$section->addTextBreak(0.5);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save("demo.docx'");
its create file in public folder

Related

PHPWord name spaces / classes & functions

I'm aware this might be more a question of understanding PHP Namespaces,etc...but I still can't get my head around the following:
I have an MVC controller with the below usages:
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Paper;
use PhpOffice\PhpWord\Style\Language;
...and the following function:
public function word(){
$phpword= new PhpWord();
$settings=$phpword->getSettings();
//$languageFrFr = new Language('FR_FR');
//$settings->setThemeFontLang($languageFrFr);
$docInfo=$phpword->getDocInfo();
$docInfo->setCreator('JMB');
$docInfo->setCreated(date("d-m-Y"));
$paper = new Paper();
$paper->setSize( 'A4' );
$section = $phpword->addSection();
$section->addText('An example dummy text...');
$file = 'test.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
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');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpword, 'Word2007');
$xmlWriter->save("php://output");
This works perfectly (eg the WORD file & it's content are output correctly...
UNTIL...I uncomment & try to set the 'ThemeFontLang' : function hangs when I load:
$languageFrFr = new Language('FR_FR');
Any clue why this is happening, and how I could avoid / bypass the long paths\namespaces definitions inside my function ?
Many thanks,
JMB

PHPWord - corrupted Word file

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');

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

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');
//#####################################################
//#####################################################
?>

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