composer require tecnickcom/tcpdf

官方网址:https://tcpdf.org/

    public function contract(Request $request){
          $user_id = isset(auth('member')->user()->id) ?auth('member')->user()->id : 0;
          //订单id
          $id = $request->get('id');
          $order = Order::with('user.company','user.user','sale','address','invoice','user_invoice')->find($id);
          
          if(!$order) dd("没有合同");
          if($user_id != $order->user_id) dd("没有权限");
          
          $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);         
          $pdf->SetCreator(PDF_CREATOR);
          $pdf->SetAuthor('**网销售合同');
          $pdf->SetTitle('**网销售合同');
          $pdf->SetSubject('**网销售合同');
         
          $pdf->setFooterData(array(0,64,0), array(0,64,128));
          // set header and footer fonts
          $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
          $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));          
          $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);          
          $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
          $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
          $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);          
          $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);          
          $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
         
          if (@file_exists(dirname(__FILE__).'/lang/chi.php')) {
               require_once(dirname(__FILE__).'/lang/chi.php');
               $pdf->setLanguageArray($l);
          }
         
          $pdf->setFontSubsetting(true);
          $pdf->SetFont('stsongstdlight', '', 12, '', true);         
          $pdf->AddPage();
         
          $pdf->setTextShadow(array('enabled'=>true,  'blend_mode'=>'Normal'));
          
          $order_no = $order->order_no;
          $created_at = date("Y年m月d日",strtotime($order->created_at));
          $created_at_last = date("Y年m月d日",strtotime("+1years",strtotime($order->created_at)));
          $buyer = $order->user->company->name_cn?$order->user->company->name_cn : $order->user->company->name_en;
          $buyer_name = $order->user->name;
          $buyer_mobile = $order->user->phone? $order->user->phone : $order->user->company->phone;
          $buyer_fax = $order->user->fax?$order->user->fax : $order->user->company->fax;
          $buyer_address = isset($order->invoice->company_address) ? $order->invoice->company_address : $order->user->company->address;
          $seller ="江苏**研发有限公司";
          $seller_name = $order->user->user->name??"王亚";
          $seller_mobile = "025-64071693";
          $seller_fax = "025-87526690";
          $seller_address = "江苏省南京市六合区中山科技园";
          $total_price = $order->total_price;
          $chinese_total_price = Util::convertAmountToCn($total_price);
          
          //收货地址
          $receive_name = $order->address->name;
          $receive_mobile = $order->address->mobile;
          $receive_address = $order->address->province.$order->address->city.$order->address->district.$order->address->address;
          $note = $order->note;
          $pay_type = '';
          if($order->pay_type == 1){
               $pay_type = '先打款后发货';
          }elseif($order->pay_type == 2){
               $pay_type = '先发货后付款';
          }elseif($order->pay_type == 3){
               $pay_type = '月结';
          }
          $itemTable = '<table cellspacing="0" border="1">
               <thead>
                    <tr>                         
                         <th width="25%">产品名称</th>
                         <th width="20%" align="center">CAS</th>
                         <th width="20%" align="center">包装规格</th>
                         <th width="15%" align="center">纯度</th>                        
                         <th width="20%">总金额</th>
                    </tr>
                </thead>
               <tbody>';
          foreach ($order->sale as $k=>$v){
               if($v['status'] == 0){
                    $itemTable.='<tr>                       
                       <td>'.$v['name_cn'].'</td>
                       <td align="center">'.$v['cas'].'</td>
                       <td align="center">'.$v['package'].$v['package_unit'].' * '.$v['num'].'</td>
                       <td align="center">'.$v['purity'].'</td>                      
                       <td>¥'.$v['price'].'</td>
                    </tr>';
               }
          }
          $itemTable .='<tr>
                         <td colspan="6" height="40"   align="right">金额合计:(人民币金额大写):'.$chinese_total_price.' (小写)¥'.$total_price.'&nbsp;&nbsp;&nbsp;&nbsp;</td>
                    </tr>
                    <tr>
                         <td colspan="6" height="40"   align="right">订单备注:'.$note.' &nbsp;&nbsp;&nbsp;&nbsp;</td>
                    </tr>
               </tbody>
          </table>';
          $html = '          
         <style>
         table{
         width: 100%;
         }
         </style>
          <h1 style="text-align:center">销售合同</h1>
          <table cellspacing="0" border="0">
               <tr>
                    <td>甲方:'.$buyer.'</td>
                    <td>已方:'.$seller.'</td>
               </tr>
                <tr>
                    <td>地址:'.$buyer_address.'</td>
                    <td>地址:'.$seller_address.'</td>
               </tr>
                <tr>
                    <td>电话:'.$buyer_mobile.'</td>
                    <td>电话:'.$seller_mobile.'</td>
               </tr>
                <tr>
                    <td>传真:'.$buyer_fax.'</td>
                    <td>传真:'.$seller_fax.'</td>
               </tr>
               <tr>
                    <td>联系人:'.$buyer_name.'</td>
                    <td>联系人:'.$seller_name.'</td>
               </tr>
          </table>
          <p>兹向贵公司订购下列货品,请确认各项交货条件并将本合同之确认联传真回本公司。</p>
          <table cellspacing="0" border="0" style="width:95%">              
               <tr>
                  <td width="60%">一、合同签订时间:'.$created_at.'</td>
                  <td>签订地点:南京</td>
               </tr> 
               <tr>                 
                  <td colspan="2">二、合同编号:'.$order_no.'</td>
               </tr> 
               <tr>                 
                  <td colspan="2">三、产品信息及价格说明:</td>
               </tr> 
                 <tr>                 
                  <td colspan="2">
                 小写金额:¥'.$total_price.'<br>
                 大写金额:'.$chinese_total_price.'<br>
                 发票类型:'.$order->user_invoice->type.'
                  </td>
               </tr>
               <tr>                 
                  <td colspan="2">'.$itemTable.'</td>
               </tr> 
             
               <tr>                 
                  <td colspan="2">四、 质量要求技术标准,乙方对甲方负责的条件和期限:包装物完好无损。</td>
               </tr>
               <tr>                 
                  <td colspan="2">五、交(提)货地点、运输方式:
                       <br>甲方付款至“户名:江苏**研发有限公司,开户行及账号:中国银行南京市高新技术开发区支行,870244725269”。
                       <br>乙方安排快递或者物流发货至甲方指定地点。
                       <br><span style="color:red;">&nbsp;'.$receive_name.' 收&nbsp;&nbsp;'.$receive_mobile.'&nbsp;&nbsp;'.$receive_address.'&nbsp;&nbsp;'.$buyer.'</p>
                  </td>
               </tr>
               <tr>                 
                  <td colspan="2">六、验收标准:标准验收</td>
               </tr> 
               <tr>                 
                  <td colspan="2">七、包装标准:包装物完好无损</td>
               </tr> 
               <tr>                 
                  <td colspan="2">八、结算方式及期限:'.$pay_type.'</td>
               </tr> 
               <tr>                 
                  <td colspan="2">九、验收标准、方法及提出异议期限:</td>
               </tr> 
               <tr>                 
                  <td colspan="2">
                    <p>1、甲方在验收中若对质量、数量有异议,在10个工作日内向乙方提出书面异议,并妥善保管有质量瑕疵货物。
鉴于产品的特殊性,乙方仅对未使用的产品质量负责,乙方不对甲方使用乙方产品引起的任何利润与损失或间接
损失或损害承担责任,乙方提醒甲方在收到产品后,及时按照技术规范验收产品;在产品开始使用后,甲方对产
品的应用和应用结果负全部责任。
                    </p>
                    <p>对产品质量提出异议的产品定性判断有文献报道的以文献值为准。没有文献的、双方不能协商解决的需服从第三
方权威检测机构出具的报告,由此产生的相关费用由判断错误一方承担。</p>
                    <p>2、甲方怠于通知或者自标的物收到之日起过20日内未通知乙方的,或提出异议后未提供检测报告的,视为产品
验收合格。</p>
                  </td>
               </tr> 
               <tr>                 
                  <td colspan="2">十、由于不可抗力的原因不能履行合同时,应及时向对方通报不能履行或不能完全履行的理由。</td>
               </tr> 
               <tr>                 
                  <td colspan="2">十一、本合同如发生纠纷,当事人双方应当及时协商解决,如不能协商解决,则应提交到“乙方”所在地有管辖权
的人民法院解决。因此产生的律师费由甲方承担</td>
               </tr>
               <tr>                 
                  <td colspan="2">十二、 本合同中的未尽事宜经双方协商需补充的条款可另附协议书,亦视为合同附件。合同附件与本合同具有同等效力。</td>
               </tr> 
               <tr>                 
                  <td colspan="2"> 十三、本合同自双方签章之日起生效,合同执行期内,甲乙双方均不得随意变更或解除合同。</td>
               </tr> 
               <tr>                 
                  <td colspan="2">十四、 本合同的复印件与传真件同样有效。</td>
               </tr>    
               <tr>                 
                  <td colspan="2">十五、 合同有效期:'.$created_at.'-'.$created_at_last.'</td>
               </tr> 
          </table>
          <p>&nbsp;</p>
          <table cellspacing="0" border="0">
               <tr>
                    <td>甲方:'.$buyer.'</td>
                    <td>已方:'.$seller.'</td>
               </tr>
                <tr>
                    <td>地址:'.$buyer_address.'</td>
                    <td>地址:江苏省南京市六合区中山科技园</td>
               </tr>
                <tr>
                    <td>电话:'.$buyer_mobile.'</td>
                    <td>电话:025-68081693</td>
               </tr>
                <tr>
                    <td>传真:'.$buyer_fax.'</td>
                    <td>传真:025-87676690</td>
               </tr>
               <tr>
                    <td>联系人:'.$buyer_name.'</td>
                    <td>联系人:'.$seller_name.'</td>
               </tr>
               <tr>
                    <td>盖章(签字):</td>
                    <td>盖章(签字):</td>
               </tr>
          </table>';          
         
          $pdf->writeHTML($html, true, false, true, false, '');
          
          //盖章
          $yinzhang = public_path("data/templet/jsak.gif");          
          $pdf->Image($yinzhang, '130', '110',35, 35, '', '', '', false, 168, '', false, false, false, false, false, false);
         
          $pdf->Output($order_no."销售合同.pdf", 'I');
     }