PHP实现打印出库单,有没有实现过?
生活随笔
收集整理的這篇文章主要介紹了
PHP实现打印出库单,有没有实现过?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景
有時候你在實現一個出庫訂單之類的功能模塊,這里也有可能要你的站點也實現相應的打印出庫單預覽,今天給大家分享用laravel(TP也行),PHP原生的也行。有需要的可以學習學習
?
源碼實現,php原生的話,需要include相關的print文件
laravel,首先先引入print插件包,我是放在app目錄里
?
路由文件
Route::any('admin/outWares/{outWares}/printer', ['as'=> 'admin.outWares.printer', 'uses' => 'PrinterController@index']);控制器文件?PrinterController
public function index($id){$outWare = $this->outWareRepository->findWithoutFail($id);$since = $outWare->outWareSince;if (empty($outWare)) {Flash::error('OutWare not found');return redirect(route('admin.outWares.index'));}//處理地址,當為自提時,地址為倉庫的地址if($outWare->mode == 0){$province_name = count($outWare->regionProvince->where('region_type', 1)) ? current($outWare->regionProvince->where('region_type', 1))[0]->region_name : '';$city_name = count($outWare->regionCity->where('region_type', 2)) ? current(current($outWare->regionCity->where('region_type', 2)))->region_name : '';$district_name = count($outWare->regionDistrict->where('region_type', 3)) ? current(current($outWare->regionDistrict->where('region_type', 3)))->region_name : '';$address = $province_name.$city_name.$district_name.$outWare->address;}else{$province_name = count($outWare->ware->regionProvince->where('region_type', 1)) ? current($outWare->ware->regionProvince->where('region_type', 1))[0]->region_name : '';$city_name = count($outWare->ware->regionCity->where('region_type', 2)) ? current(current($outWare->ware->regionCity->where('region_type', 2)))->region_name : '';$district_name = count($outWare->ware->regionDistrict->where('region_type', 3)) ? current(current($outWare->ware->regionDistrict->where('region_type', 3)))->region_name : '';$address = isset($outWare->ware) ? $province_name.$city_name.$district_name.$outWare->ware->address : '';}$consignee = [];if($outWare->mode==0){$consignee = $outWare->customer_name;$consignee_phone = $outWare->customer_phone;}else{foreach($since as $so){$consignee[$so->consignee] = $so->consignee_phone;}list($keys, $values) = array_divide($consignee);if(count($keys) > 0){$consignee = implode('<br>',$keys);$consignee_phone = implode('<br>',$values);}else{$consignee = '';$consignee_phone = '';}}if($outWare->demand_time == '0000-00-00 00:00:00' || empty($outWare->demand_time)){$demand_time = '';}else{$demand_time = date('Y-m-d',strtotime($outWare->demand_time));}$out_ware_detail = $this->getWareDetail($outWare->outWareDetail);$data = ['out_sn' => $outWare->out_sn,'ware' => isset($outWare->ware) ? $outWare->ware->name : '','company' => isset($outWare->company) ? $outWare->company : '','telephone' => isset($outWare->ware) ? $outWare->ware->phone_1 : '','consignor' => isset($outWare->ware) ? $outWare->ware->director_1 : '','consignee' => $consignee,'consignee_phone' => $consignee_phone,'remark' => $outWare->remark,'demand_time' => $demand_time,'created_at' => $outWare->created_at->format('Y-m-d')];$address = $this->getWareAddress($address);$this->TCPDF($data,$out_ware_detail,$address);}一些數據的處理,這里只做參考
/*** Function:處理地址樣式居中* User:wucy* @param $address* @return string*/public function getWareAddress($address){if(strlen($address) < 80){return <<<Eof<td rowspan="2" colspan="2" style="font-size: 16px;width: 455px;line-height:60px;">{$address}</td> Eof;}else{return <<<Eof<td rowspan="2" colspan="2" style="font-size: 16px;width: 455px;">{$address}</td> Eof;}}/*** Function:獲取出庫單商品詳情* User:wucy* @param $outWareDetail* @return string*/public function getWareDetail($outWareDetail){$temp_row_data = [];$collection = collect($outWareDetail);$grouped = $collection->groupBy(function ($item, $key) {return $item['sku_id'];});$i=1;foreach ($grouped as $key => $item){$temp_row_data[$key] = ['key_num' => $i++,'goods_name' => isset($item[0]->goodsSku) ? $item[0]->goodsSku->goods->goods_name : '--','attr_name' => isset($item[0]->goodsSku) ? $item[0]->goodsSku->value_name : '--','goods_unit' => isset($item[0]->goodsSku) ? $item[0]->goodsSku->goods->goods_unit : '--','total' => abs($item->sum('goods_number')),'remark_detail'=>isset($item[0]) ? $item[0]->remark_detail : '--',];}//dd($temp_row_data);if ($temp_row_data) {$item = '';foreach ($temp_row_data as $v) {$item.= $this->getRowsTable($v);}return $item;}}/*** Function:* User:wucy* @param $data* @return string*/public function getRowsTable($data){if($data){return <<<Eof<tr><td style="font-size: 16px;text-align: center;">{$data['key_num']}</td><td style="font-size: 16px;">{$data['goods_name']}</td><td style="font-size: 16px;text-align: center;">{$data['attr_name']}</td><td style="font-size: 16px;text-align: center;">{$data['goods_unit']}</td><td style="font-size: 16px;text-align: center;">{$data['total']}</td><td></td><td></td><td>{$data['remark_detail']}</td></tr> Eof;}}模板文件
/*** Function:TCPDF* User:wucy* @param $data* @param $out_ware_detail*/public function TCPDF($data,$out_ware_detail,$address){// create new PDF document$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);// set document information$pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('倉庫系統');$pdf->SetTitle('出庫單');$pdf->SetSubject('TCPDF Tutorial');$pdf->SetKeywords('TCPDF, PDF, example, test, guide');// set default header data//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 048', PDF_HEADER_STRING);// 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->setPrintHeader(false);$pdf->setPrintFooter(false);// set default monospaced font$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);// set margins$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);// set auto page breaks$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);// set image scale factor$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);//$pdf->SetFont('stsongstdlight','', 14);$pdf->SetFont('droidsansfallback','', 14);// add a page$pdf->AddPage();$pdf->Write(0, '', '', 0, 'L', true, 0, false, false, 0);$pdf->setCellHeightRatio(1.3);$pdf->SetLineWidth(2);$tbl = <<<EOD<table cellpadding="0" cellspacing="0"><tr><th><img src="/adminResource/img/logo_5100.png" width="140" height="40"></th><th style="font-size: 25px;font-weight: bold">出庫單</th></tr></table><table cellpadding="0" cellspacing="0"><tr><td style="font-weight: bold">單據日期:{$data['created_at']}</td><td colspan="1"></td><td style="font-weight: bold;text-align: right;width:377px;">出庫單號:{$data['out_sn']}</td> </tr></table><table cellpadding="2" cellspacing="0" border="1" summary="出庫單"><tr><th style="font-size: 18px;width:80px;font-weight: bold;">發貨倉</th><td style="font-size: 16px;width: 100px;">{$data['ware']}</td><th style="font-size: 18px;width:120px;font-weight: bold">收貨公司</th><td style="font-size: 16px;width: 150px;">{$data['company']}</td><th rowspan="2" style="font-size: 18px;width:130px;font-weight: bold;line-height:60px;">提貨/收貨地址</th>{$address}</tr><tr><th style="font-size: 18px;font-weight: bold">發貨人</th><td style="font-size: 16px;">{$data['consignor']}</td><th style="font-size: 18px;font-weight: bold">發貨人電話</th><td style="font-size: 16px;">{$data['telephone']}</td></tr><tr><th style="font-size: 18px;font-weight: bold">提貨人/收貨人信息</th><td colspan="2" style="font-size: 16px;line-height:60px;">{$data['consignee']}</td><td style="font-size: 16px;line-height:60px;">{$data['consignee_phone']}</td><th style="font-size: 18px;font-weight: bold;line-height:60px;">要求配送時間</th><td colspan="2" style="font-size: 16px;line-height:60px;">{$data['demand_time']}</td></tr><tr><th style="font-size: 18px;font-weight: bold">訂單備注</th><td colspan="6" style="font-size: 16px;">{$data['remark']}</td></tr><tr><th colspan="7" style="font-size: 18px;text-align: center;font-weight: bold">出庫明細</th></tr><tr><td style="font-size: 18px;text-align: center;font-weight: bold;width:80px;">編號</td><td style="font-size: 18px;text-align: center;font-weight: bold;width:275px;">貨品名稱</td><td style="font-size: 18px;text-align: center;font-weight: bold;width:60px;">屬性</td><td style="font-size: 18px;text-align: center;font-weight: bold;width:70px;">單位</td><td style="font-size: 18px;text-align: center;font-weight: bold;width:90px;">出貨數量</td><td style="font-size: 18px;text-align: center;font-weight: bold;width:90px;">實發數量</td><td style="font-size: 18px;text-align: center;font-weight: bold;width:90px;">實收數量</td><td style="font-size: 18px;text-align: center;font-weight: bold;width:280px;">備注</td></tr>{$out_ware_detail}<tr><th style="font-size: 18px;font-weight: bold">簽收人</th><td colspan="3"></td><th style="font-size: 18px;font-weight: bold">簽收日期</th><td colspan="3"></td></tr><b>請簽收人簽字后務必將掃描件發至我司聯系人郵箱,否則默認實收與實發數量一致</b></table> EOD;$pdf->writeHTML($tbl, true, false, false, false, '');// -----------------------------------------------------------------------------//Close and output PDF document$pdf->Output('出庫單_'.date('YmdHis').'.pdf', 'I');}全部文件都分享了,因為需求不一樣,這里只做參考!
總結
以上是生活随笔為你收集整理的PHP实现打印出库单,有没有实现过?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑主机前置耳机插孔没声音——解决办法
- 下一篇: 24h脚本,24h脚本