Home > other >  Codeigniter 4 with PHPWord loadTemplate
Codeigniter 4 with PHPWord loadTemplate

Time:02-24

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

CodePudding user response:

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"); ✅
// ...

CodePudding user response:

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