| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Product;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- class ExportController extends Controller
- {
- public function export_docx(Request $request){
- $ids = json_decode($request->ids,true);
- $products = Product::query()->whereIn('id', $ids)->orderBy('series', 'asc')->get();
- $my_template = new \PhpOffice\PhpWord\TemplateProcessor(storage_path('templates/template_maf.docx'));
- $vars_for_template = [];
- $i = 1;
- foreach ($products as $product){
- $vars_for_template = array_merge($vars_for_template, [
- 'series#' . $i => $product->series,
- 'product_group#' .$i => $product->product_group,
- 'num#' . $i => $i,
- 'name#' . $i => $product->name,
- 'name_for_form#' . $i => $product->name_for_form,
- 'price#' . $i => number_format($product->price, 0, '', ' '),
- 'image#' . $i => $product->image_path,
- 'description#' . $i => str_replace("\n", '</w:t><w:br/><w:t xml:space="preserve">', $product->characteristics . "\r\n" . $product->tech_description),
- ]);
- $i++;
- }
- // dd($vars_for_template);
- $my_template->cloneBlock('product_block', count($products), true, true, $vars_for_template);
- foreach ($vars_for_template as $k => $v){
- if(str_starts_with($k, 'image') && file_exists($file = public_path() . '/' . env('IMAGES_PATH') . '/' . $v)){
- $my_template->setImageValue($k, ['path' => $file,
- 'width' => 270, 'height' => 180
- ]);
- } else {
- $my_template->setValue($k, $v);
- }
- }
- // dd($vars_for_template);
- $file = 'Наш_Двор_' . date('YmdHis') . '.docx';
- header("Content-Description: File Transfer");
- header('Content-Disposition: attachment; filename="' . $file . '"');
- header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
- header('Content-Transfer-Encoding: binary');
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Expires: 0');
- ob_clean();
- $my_template->saveAs("php://output");
- Log::notice('Execution time: ' . microtime(true) - LARAVEL_START);
- exit();
- }
- }
|