Phpword: Help wanted to create document using template.

Created on 11 Aug 2018  路  6Comments  路  Source: PHPOffice/PHPWord

I am using PHPword to generate the word documents. I want to generate multiple documents so that I want to use the template. I tried this code in the controller
$templateProcessor = new TemplateProcessor('resources/Sample_07_TemplateCloneRow.docx');
$templateProcessor->setValue('Name', 'John Doe');
$templateProcessor->setValue(array('City', 'Street'), array('Detroit', '12th Street'));
and following error is displayed:
copy(resources/Sample_07_TemplateCloneRow.docx): failed to open stream: No such file or directory

at :
// Template file cloning
if (false === copy($documentTemplate, $this->tempDocumentFilename)) {
throw new CopyFileException($documentTemplate, $this->tempDocumentFilename);
}

What is the default directory of Template Processor? or we have to give location manually. Please help!

All 6 comments

Can you try something like this?

...
$templateProcessor = new TemplateProcessor('resources/Sample_07_TemplateCloneRow.docx');
$templateProcessor->setValue('Name', 'John Doe');
$templateProcessor->setValue(array('City', 'Street'), array('Detroit', '12th Street'));

$export_file = public_path('filename.docx');

$templateProcessor->saveAs($export_file);
return response()->download($export_file)->deleteFileAfterSend(true);

I tried but it says.
copy(resources/Sample_07_TemplateCloneRow.docx): failed to open stream: No such file or directory
Which directory TemplateProcessor Access or we have to link controller and resources folder .?

Remove the copy(resources/Sample_07_TemplateCloneRow.docx) line.. Just use exactly as my sample.

I tried same Please have a look.
image
image

Please change your Sample_07_TemplateCloneRow.docx to be inside /storage directory instead. And use this line:

$templateProcessor = new TemplateProcessor(storage_path('Sample_07_TemplateCloneRow.docx'));

Try using the resource_path helper.

$templateProcessor = new TemplateProcessor(resource_path('Sample_07_TemplateCloneRow.docx'))

Was this page helpful?
0 / 5 - 0 ratings