Reputation: 151
I'm trying to work with the PhpWord library for generating a Word file using Laravel.
I search for the documentation of this library and how it worked, I found that I should have already a template to work with.
But In my case, I should create this template from the database and then generate this file in word format.
I'm trying the following code :
$preambule = DB::table('modele_contrat')->select('preambule')->where('id_modele_contrat', $value['id_modele_contrat'])->value('preambule');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$text = $section->addText($preambule);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('testt.docx');
return response()->download(public_path('testt.docx'));
But in this case the file does not open with the error, problems are detected in the content of the file.
But when I change the code of this line :
$text = $section->addText($preambule);
to :
$text = $section->addText("hello");
The file opens normally.
When I did dd($preambule) to see the value of this variable, I get the value stored in database column, a varchar elements.
Upvotes: 0
Views: 226
Reputation: 2834
$preambule = DB::table('modele_contrat')->where('id_modele_contrat', $value['id_modele_contrat'])->value('preambule');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$sectionStyle = array(
//'orientation' => 'landscape',
'marginTop' => 600,
'marginRight' => 600,
'marginBottom' => 600,
'marginLeft' => 600,
//'text-align' => 'right',
'footerHeight' => 100
);
$section = $phpWord->addSection($sectionStyle);
$section->addText($preambule);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
try {
$objWriter->save(storage_path('preambule.docx'));
} catch (Exception $e) {
}
return response()->download(storage_path('preambule.docx'));
Upvotes: 1