php使用mpdf制作物流标签面单

京漂大叔 2022-10-18 AM 4056℃ 0条

通过mpdf制作物流面单,电子标签。
mpdf地址:https://github.com/mpdf/mpdf
http://mpdf.github.io/installation-setup/installation-v7-x.html

require_once(DOCUROOT."/include/mpdf/vendor/autoload.php");//mpdf库
require_once(DOCUROOT."/include/phpqrcode/phpqrcode.php");//二维码生成库
//创建mpdf对象,设置参数

    $mpdf = new \Mpdf\Mpdf([
        'mode' => 'utf-8',
        'format' => [60,100],
        'allow_html_optional_endtags' => true,
        'orientation' => 'L',
        'margin_left' => 0,
        'margin_right' => 0,
        'margin_top' => 0,
        'margin_bottom' => 0,
        'margin_header' => 0,
        'margin_footer' => 0
    ]);
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->autoLangToFont = true;
    $mpdf->autoScriptToLang = true;
    if($type==1){
        $file = '/images/common/calca_title.png';
    } else {
        $file = '/images/common/all_bg.png';
    }
   //$productinfo 是产品信息数组,循环生成多个产品标签
    foreach($productinfo as $k=>$v){
        $name = $v['product_name'];//产品名
        $namenumber = strlen($name);
        if($namenumber>120){
            $name = substr($name,0,105).'...';
        }
        $serial_number = $v['serial_number'];//条码
        $product_number = $v['product_number'];//编码
        $unit = $v['product_unit'];//单位
        $price = number_format(round($v['product_price_rmb']/$usd_exc,2), 2);//美金价格
        $priceexp = explode('.',$price);
        $pricebefore = $priceexp[0];//小数点前
        $priceafter = $priceexp[1];//小数点后
        $url=$domain.$v['url'];
        $path = DOCUROOT.'/data/qrcode/'.$v['id'].'-qrcode.png';
        //echo $url;die;
        QRcode::png($url,$path,QR_ECLEVEL_L,10,1,false);//二维码生成方法;
        $code = $path;
        if($type==1){
            $mpdf->Image($file, 40, 40);
            if($namenumber<40){
               //设置排版布局通过WriteHTML方法
                $mpdf->WriteHTML('<h1 style="font-family:montserrat;margin: 0; padding: 0; font-size: 14px; line-height: 18px; font-weight: 200;padding: 25px 25px 10px 25px;">'.$name.'</h1>');
                $mpdf->WriteHTML('<h2 style="font-family:montserrat;margin: 0; padding: 0; font-size: 12px; line-height: 14px; padding: 0px 25px 5px 25px;"><strong>'.$product_number.'</strong></h2>');
                $mpdf->WriteHTML('<h2 style="font-family:montserrat;margin: 0; padding: 0; font-size: 12px; line-height: 14px; padding: 0px 25px 20px 25px;"><strong>'.$serial_number.'</strong></h2>');
            } else {
                $mpdf->WriteHTML('<h1 style="font-family:montserrat;margin: 0; padding: 0; font-size: 14px; line-height: 18px; font-weight: 200;padding: 10px 25px 5px 25px;">'.$name.'</h1>');
                $mpdf->WriteHTML('<h2 style="font-family:montserrat;margin: 0; padding: 0; font-size: 12px; line-height: 14px; padding: 0px 25px 2px 25px;"><strong>'.$product_number.'</strong></h2>');
                $mpdf->WriteHTML('<h2 style="font-family:montserrat;margin: 0; padding: 0; font-size: 12px; line-height: 14px; padding: 0px 25px 5px 25px;"><strong>'.$serial_number.'</strong></h2>');
            }
            $mpdf->WriteHTML('<p style="font-family:montserrat;position: absolute; left:25px; bottom: 10px; font-size: 22px; font-weight: 400;">$<b style="font-size: 30px; font-weight: 800;">'.$pricebefore.'</b>.'.$priceafter.'<span style="font-size:20px;">/'.$unit.'</span></p>');
            //$mpdf->WriteHTML('<img src="code.png" style="position: absolute; top:0;right: 30px; bottom:0px; width: 60px; height: 60px;">');
            $mpdf->Image($code, 70, 33.5,22,22,'png','',true,false);
            $mpdf->WriteHTML('<div style="position: absolute; left:0; bottom:0;width:100%; height:10px; background:#000;"></div>');
        } else {
            /*$mpdf->SetDefaultBodyCSS('background', "url($file)");
            $mpdf->SetDefaultBodyCSS('background-image-resize', 6);*/
            if($namenumber<40){
                $mpdf->WriteHTML('<h1 style="font-family:montserrat;margin: 0; padding: 0; font-size: 14px; line-height: 18px; font-weight: 200;padding: 40px 25px 8px 25px;">'.$name.'</h1>');
                $mpdf->WriteHTML('<h2 style="font-family:montserrat;margin: 0; padding: 0; font-size: 12px; line-height: 14px; padding: 0px 25px 5px 25px;"><strong>'.$product_number.'</strong></h2>');
                $mpdf->WriteHTML('<h2 style="font-family:montserrat;margin: 0; padding: 0; font-size: 12px; line-height: 14px; padding: 0px 25px 20px 25px;"><strong>'.$serial_number.'</strong></h2>');
            } else {
                $mpdf->WriteHTML('<h1 style="font-family:montserrat;margin: 0; padding: 0; font-size: 14px; line-height: 18px; font-weight: 200;padding: 25px 25px 8px 25px;">'.$name.'</h1>');
                $mpdf->WriteHTML('<h2 style="font-family:montserrat;margin: 0; padding: 0; font-size: 12px; line-height: 14px; padding: 0px 25px 5px 25px;"><strong>'.$product_number.'</strong></h2>');
                $mpdf->WriteHTML('<h2 style="font-family:montserrat;margin: 0; padding: 0; font-size: 12px; line-height: 14px; padding: 0px 25px 20px 25px;"><strong>'.$serial_number.'</strong></h2>');
            }
            $mpdf->WriteHTML('<p style="font-family:montserrat;position: absolute; left:25px; bottom: 30px; font-size: 22px; font-weight: 400;">$<b style="font-size: 30px; font-weight: 800;">'.$pricebefore.'</b>.'.$priceafter.'<span style="font-size:20px;">/'.$unit.'</span></p>');
            $mpdf->Image($code, 67, 27,25,25,'png','',true,false);
        }

    }
    $mpdf->Output('productlable.pdf','I');
标签: PHP, mpdf

非特殊说明,文章均为原创。

评论啦~