Reputation: 65
I'm using the PHPOffice/Word package to generate word files from template and i use Laravel 5.8 version running on PHP Server 7.3.29.
The generation works pretty good when i download only one word document, but when i want to generate multiple word files in a foreach
loop, i got that error : ZipArchive::getFromName(): Invalid or uninitialized Zip object
.
Here is the code (the error is on the line with the saveAs
method) :
$template_path = $request->FILE_PATH;
try {
$templateProcessor = new TemplateProcessor($template_path);
}
catch(\Exception $e) {
return back()->with('danger',"Erreur lors de l'export - Vérifier le nom et le chemin de la maquette");
}
foreach(explode(",",$request->IDS_CONTACT) as $idContact) {
$contact = Contact::find($idContact);
$templateProcessor->setValue('date_jour',$request->DT_ENVOI_PACK);
$templateProcessor->setValue('IDENTITE','Test');
$templateProcessor->setValue('ADRESSE','Test');
$templateProcessor->setValue('projet','Test');
$templateProcessor->setValue('ADRESSE_BIS','Test');
$templateProcessor->setValue('CODE_POSTAL','Test');
$templateProcessor->setValue('VILLE','Test');
$templateProcessor->setValue('form_politesse','Test');
$templateProcessor->setValue('object_pack',$request->OBJ_PACK);
$templateProcessor->setValue('form_politesse2','Test');
$filename = time() . "PackBienvenue.docx";
$templateProcessor->saveAs(storage_path('exports\\'.$filename));
sleep(1);
}
What is surprising is that it still works fine on the first generation and i got my word document before it raise the Exception that i have put before. But it stop there and the following generations do not work.
I thought first that was a problem with the filename which was overwrite, so i have put the time
method to be sure that each document have different filename.
And also, i have an old version of Word (Word 2010 version 14), don't think that this is the problem but in doubt i prefer to precise it...
If someone have ever done something like this before, i would appreciate some help. Thanks in advance
Upvotes: 2
Views: 545
Reputation: 65
For those who would be interested, i fix the issue by (re)creating at each loop tour a TemplateProcessor
object.
Not sure that this is the best way to fix this error, but it works !
Full code like this :
// code
foreach(explode(",",$request->IDS_CONTACT) as $idContact) {
try {
$templateProcessor = new TemplateProcessor($template_path);
}
catch(\Exception $e) {
return back()->with('danger',"Erreur lors de l'export - Vérifier le nom et le chemin de la maquette");
}
$contact = Contact::find($idContact);
$templateProcessor->setValue('date_jour',$request->DT_ENVOI_PACK);
$templateProcessor->setValue('IDENTITE',$contact->identite);
$templateProcessor->setValue('ADRESSE',$contact->ADRESSE1);
$templateProcessor->setValue('projet',$projet->LIBELLE_PROJ);
$templateProcessor->setValue('ADRESSE_BIS',$contact->ADRESSE2);
$templateProcessor->setValue('CODE_POSTAL',$contact->CDPOST);
$templateProcessor->setValue('VILLE',$contact->COMMUNE);
$templateProcessor->setValue('form_politesse',$contact->getFormulePolitesse());
$templateProcessor->setValue('object_pack',$request->OBJ_PACK);
$templateProcessor->setValue('form_politesse2',strtolower($contact->getFormulePolitesse()));
$filename = time() . "PackBienvenue.docx";
$templateProcessor->saveAs(storage_path('exports\\'.$filename));
}
Upvotes: 1