在phpword中自定义文件名

Customise the filename in phpword

下载phpword时可以自定义文件名吗?

我希望文件采用导出行的 prenoms

我的代码:

public function edit (Stagiaire $stagiaire)
  {
    $id = $stagiaire ->id;
    $desc1 = Stagiaire::find($id);
    $my_template = new \PhpOffice\PhpWord\TemplateProcessor(public_path('templateStagiaire.docx'));
    $my_template->setValue('id, $desc->id);
    $my_template->setValue('prenoms, $desc->prenoms);
    $my_template->setValue('nom, $desc->nom);

    $filename = $stagiaire->prenoms;
    try{
       $my_template->saveAs(storage_path('templateStagiaire.docx'));
       }catch (Réception $e){}
     return response()->download(storage_path('".$filename.".docx));

     }

需要帮助。 提前致谢。

Laravel 下载文件示例:


    public function edit(Stagiaire $stagiaire, $downloadName = null)
    {
        $id = $stagiaire->id;
        $desc = Stagiaire::find($id);
        $my_template = new \PhpOffice\PhpWord\TemplateProcessor(public_path('templateStagiaire.docx'));
        $my_template->setValue('id', $desc->id);
        $my_template->setValue('prenoms', $desc->prenoms);
        $my_template->setValue('nom', $desc->nom);

        // save as `prenoms` filename
        $filename = $stagiaire->prenoms;
        try {
            $my_template->saveAs(storage_path("$filename.docx"));
        } catch (Reception $e) {
        }

        // if download name is null, then use filename
        $downloadName = $downloadName??$filename;
        
        return response()->download(storage_path("$filename.docx"))
            ->header('Content-disposition','attachment; filename="'.$downloadName.'"');

     }

Similar Laravel example