PHP与出库单打印预览,包含下载与打印功能

13 篇文章 0 订阅

咱们的开发语言还是用框架:laravel5。

 

应用场景

出库单打印出三色单,这个很普遍,以下用PHP来实现出库单的打印预览,主要用到tcpdf插件,整合到laravel项目中。tcpdf插件可以到官网下载最新的版本。我这里用的是最新版本,

这里提供下载插件链接:

链接:https://pan.baidu.com/s/1Mn8ho_AxyJfWR7YUa0wzhQ

提取码:9doa

三色单截图:

 

插件截图:

 

开发功能效果

 

点击按钮后跳转到打印页面,此页面包含下载pdf功能

点击打印按钮就跳转到打印的页面了

这个时候按打印,你电脑如果有连接打印机的话就可以直接打印出出库单了

 

下面来看看如何引入tcpdf

把tcpdf文件打包放在laravel根目录或者其他目录里,这一步没关系,只要在controer我是的命名空间里能调用到就好了,我是放在app目录里,命名一个规范名字:Printer

 

路由文件

Route::any('admin/outWares/{$id}/printer', ['as'=> 'admin.outWares.printer', 'uses' => 'PrinterController@index']);

 

PrinterController.php都在这个文件里处理。首先要引入插件:use Printer; index方法输入,其他的都是处理各自的业务流程,下面看看源码就明白了。

use Printer;
public function index($id)
{
$outWare = $this->outWareRepository->findWithoutFail($id);
//获取处理的出库单详情
$out_ware_detail = $this->getWareDetail($outWare->outWareDetail);
//这里处理出库单数据
$data = [
......
    '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) {
        $item->stock = abs($item['goods_number']);
        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'      => $item->sum('stock'),
            'remark_detail'=>isset($item[0]) ? $item[0]->remark_detail : '--',
        ];
    }

    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');
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值