下載phpword插件
composer require phpoffice/phpword
?生成word文件接口
static public function word(){//接收傳值$order_id = request()->get('order_id');$tpl_id = request()->get('tpl_id');//查詢出對應的數據以及關聯數據$sale_order = \App\Models\SaleOrder::with(['customer','items','user.employee','amount'])->where('id',$order_id)->first();//我的模版存儲在一個單獨的表里,查詢出使用的那一個模版文件$modelcontract = ContractTplModel::where('id',$tpl_id)->first();$file = $modelcontract->file;//循環產品數據存入變量中foreach($sale_order->items as $key => $val){$item_data[$key]['id'] = $key+1;$item_data[$key]['item_id'] = $val->sku_id;$item_data[$key]['goods_name'] = $val->sku->goods_name ?? '';$item_data[$key]['sku_name'] = $val->sku->sku_name ?? '';$item_data[$key]['num'] = $val->should_num ?? '';$item_data[$key]['after_tax_price'] = $val->after_tax_price ?? '';$item_data[$key]['point_price'] = $val->point_price ?? '';$item_data[$key]['point'] = $val->point ?? '';$goods_name[] = $val->sku->goods_name ?? '';}$trade_name = implode("、", array_unique($goods_name));//獲取模版文件完整的路徑$file_path = storage_path('app/public/' . $file);//實例化phpword內置控制器,傳值模版文件路徑(重要,一定要加這一行實例化)$templateProcessor = new TemplateProcessor($file_path);//這里是變量取值,根據自己的數據賦值即可$party_a = $sale_order->customer->name;//甲方名稱$product_name = $trade_name;//產品名稱$total_amount = $sale_order->total_amount ?? ''; //含稅總價$first_price = $total_amount * 0.3;$tow_price = $total_amount * 0.6 - $first_price;
// dd($after_tax_price,$first_price,$tow_price);$price3 = $total_amount - $first_price - $tow_price;$support = new Support();$total_amount_cn = $support->convertAmountToCn($sale_order->total_amount);//含稅總價大寫$first_price_cn = $support->convertAmountToCn($first_price);$tow_price_cn = $support->convertAmountToCn($tow_price);$price3_cn = $support->convertAmountToCn($price3);$prefix = str_replace('-','','sk'.$sale_order->user->prefix.build_ws_no());//合同前綴//頁面賦值,將頁面上對應的值替換$templateProcessor->setValues(array('合同編號'=>$prefix,'甲方名稱'=>$party_a,'產品名稱'=>$product_name,'含稅總價'=>$sale_order->total_amount,'大寫總價'=>$support->convertAmountToCn($sale_order->total_amount),'三個工作日內百分之'=>30,'第一筆金額'=>$first_price,'第一筆金額大寫'=>$first_price_cn,'一筆貨款發出時間'=>3,'收到貨物工作日'=>2,'收到貨物百分比'=>30,'第二筆金額'=>$tow_price,'第二筆金額大寫'=>$tow_price_cn,'第二筆貨款工作日'=>$tow_price,'第三筆貨款'=>$price3,'第三筆貨款大寫'=>$price3_cn,'乙方收到款項后工作日'=>10,'甲方負責人姓名'=>$sale_order->customer->contacts_name??'','甲方負責人電話'=>$sale_order->customer->contacts_mobile ?? '','乙方負責人姓名'=>$sale_order->user->employee->name ?? '','乙方負責人電話'=>$sale_order->user->employee->mobile ?? '','增值稅專用發票'=> 6 ?? '',));//這里是word模版文件中有表格,情況下的替換,如果你的模版文件沒有表格到這里就不需要了,直接開始寫生成文件即可。if (count($item_data)){$templateProcessor->cloneRow('id',count($item_data));foreach ($item_data as $k => $v) {$templateProcessor->setValue('id#'. ($k + 1), $v['id']);$templateProcessor->setValue('goods_name#'. ($k + 1), $v['goods_name']);$templateProcessor->setValue('sku_name#'. ($k + 1), $v['sku_name']);$templateProcessor->setValue('num#' . ($k + 1), $v['num']);$templateProcessor->setValue('after_tax_price#' . ($k + 1), $v['after_tax_price']);$templateProcessor->setValue('point#' . ($k + 1), $v['point']);$templateProcessor->setValue('point_price#' . ($k + 1), $v['point_price']);}}//生成word文件(保存路徑)$templateProcessor->saveAs( storage_path().'/app/public/files/合同.docx');//下載文件$file_path = storage_path().'/app/public/files/合同.docx'; // 文件的路徑和名稱$file_name = basename($file_path); // 獲取文件名header("Content-Type: application/octet-stream");header("Content-Disposition: attachment; filename=" . $file_name);ob_clean();flush();readfile($file_path);}