Forum


Replies: 1   Views: 1178
How to create and download multiple word docx in a loop
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by Rain Xia  · 04-03-2020 - 04:37

I need to click a button to generate multiple docx with a same template, but only one docx is created. Any idea?????

collect($params['item_ids'])->map(function($item_id) {
            $template = resource_path('templates/letter_template.docx');
            $docx = new \Phpdocx\Create\CreateDocxFromTemplate($template);
            
            $data = [some data created by $item_id];
            $docx->replaceVariableByText($data, ['target' => 'document']);

            $file_name = implode('-', [
                 Carbon::now()->format('ymd'),
                 'letter',
                 rand(),
             ]);

            $docx->createDocxAndDownload($file_name, true);
        });