简体   繁体   English

PHPOffice/Word - 从模板生成多个文件时出错

[英]PHPOffice/Word - Error when generate multiple files from template

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.我正在使用 PHPOffice/Word package 从模板生成 word 文件,我使用运行在 PHP Server 7.3.29 上的 Laravel 5.8 版本。

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 .当我只下载一个 word 文档时,生成效果很好,但是当我想在foreach循环中生成多个 word 文件时,我得到了这个错误: ZipArchive::getFromName(): Invalid or uninitialized Zip object

Here is the code (the error is on the line with the saveAs method):这是代码(错误与saveAs方法有关):

      $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.令人惊讶的是它在第一代上仍然可以正常工作,并且在它引发我之前提出的异常之前我得到了我的 word 文档。 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.我首先想到的是被覆盖的文件名有问题,所以我使用time方法来确保每个文档都有不同的文件名。

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...而且,我有一个旧版本的 Word(Word 2010 版本 14),不认为这是问题所在但我怀疑我更愿意精确...

If someone have ever done something like this before, i would appreciate some help.如果有人以前做过这样的事情,我将不胜感激。 Thanks in advance提前致谢

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 !对于那些感兴趣的人,我通过在每次循环游览时(重新)创建一个TemplateProcessor object 来解决这个问题。不确定这是解决此错误的最佳方法,但它有效!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM