【问题标题】:PHPOffice/Word - Error when generate multiple files from templatePHPOffice/Word - 从模板生成多个文件时出错
【发布时间】:2021-10-16 11:55:54
【问题描述】:

我正在使用 PHPOffice/Word 包从模板生成 word 文件,并且我使用在 PHP Server 7.3.29 上运行的 Laravel 5.8 版本。

当我只下载一个word文档时,生成效果很好,但是当我想在foreach循环中生成多个word文件时,我得到了那个错误:ZipArchive::getFromName(): Invalid or uninitialized Zip object

这里是代码(错误就在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);
      }

令人惊讶的是它在第一代上仍然可以正常工作,并且在它引发我之前提出的异常之前我得到了我的 word 文档。但它止步于此,随后的几代人无法正常工作。

我首先认为是文件名被覆盖的问题,所以我使用了time 方法来确保每个文档都有不同的文件名。

而且,我有一个旧版本的 Word(Word 2010 版本 14),不认为这是问题,但我怀疑我更喜欢精确...

如果有人以前做过这样的事情,我会很感激一些帮助。提前致谢

【问题讨论】:

    标签: php laravel ziparchive phpoffice


    【解决方案1】:

    对于那些感兴趣的人,我通过在每个循环游览中(重新)创建一个 TemplateProcessor 对象来解决此问题。 不确定这是修复此错误的最佳方法,但它确实有效!

    完整代码如下:

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

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多