PHP 生成条形码每页44个

参考:
https://github.com/tecnickcom/TCPDF
下载完成放入vendor 目录

参考例子:
https://tcpdf.org/examples/example_027/

以下例子将会在A4纸张生成每页44个条形码,可以在条码下面自己定义任何文字如made in china,试用于亚马逊FBA发货贴标。

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/2/5
 * Time: 17:54
 */

namespace app\common;


class Barcode
{
    
    

    /**
     * 条形码生成
     * @param $data
     * @return mixed|null
     */
    public static function create($data)
    {
    
    
        if (!$data) {
    
    
            ApiResponse::error('请输入code');
        }
        $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        $pdf->SetAutoPageBreak(false);
        //初始化参数
        $pdf->AddPage();

        $x = 14;   #条码第一个的x坐标
        $y = 14.6; #条码第一个的Y坐标
        $t_x = 24;
        $t_y = 24;
        $ordinate = $y; #向下偏移量
        $text_y = $t_y;
        $num = 0;

        foreach ($data as $index => $v) {
    
    
            //换页,重置参数
            if ($index % 44 == 0 && $index > 0) {
    
    
                $pdf->AddPage();
                $ordinate = $y;
                $text_y = $t_y;
                $num = 0;
            } else {
    
    
                if ($index % 4 == 0 && $index > 0) {
    
     #每次换行时偏移量增加值
                    $ordinate += 25;
                    $text_y += 25;
                    $num = 0;
                }
            }

            $pdf->write1DBarcode($v['fnsku'], 'C128', $x + $num * 48.3, $ordinate, 38, 8.5);
            self::writeText($pdf, $t_x + $num * 48.3, $text_y, $v['fnsku'], $v['goods_name']);
            $num++;

        }
        $file = ROOT_PATH . 'public/uploads/' . createRandomStr(10) . '.pdf';
        $pdf->Output($file, 'F');
        $file_name = AliyunUpload::uploadPdf($file, 'barcode/' . createRandomStr(10) . '.pdf');
        unlink($file);
        return $file_name;
    }

    /**
     * 写入文字到pdf文件
     * 字体参考: https://tcpdf.org/docs/fonts/
     * @param \TCPDF $pdf
     * @param $x
     * @param $y
     * @param $code
     * @param $goods_name
     */
    private static function writeText(\TCPDF $pdf, $x, $y, $code, $goods_name)
    {
    
    
        $pdf->SetXY($x, $y);
        $pdf->SetFont('helvetica', '', 7);
        $pdf->Write(2, $code, '', 0, 'L', true, 0, false, false, 0);
        $pdf->SetXY($x - 11, $y + 4);
        $pdf->SetFont('helvetica', '', 6);
        $pdf->Write(0, self::cut_str($goods_name), '', 0, 'L', true, 0, false, false, 0);
        $pdf->SetXY($x - 11, $y + 7);
        $pdf->SetFont('times', '', 8.5);
        $pdf->Write(0, 'New Made in China ', '', 0, 'L', true, 0, false, false, 0);
    }

    /**
     * @param $str
     * @param int $begin
     * @param int $end
     * @return string
     */
    private static function cut_str($str, $begin = 12, $end = 18)
    {
    
    
        if (strlen($str) < $begin + $end + 3) {
    
    
            return $str;
        }
        return mb_substr($str, 0, $begin) . '...' . mb_substr($str, -$end);
    }
}

生成示例:

在这里插入图片描述
可以使用支付宝扫码功能验证,验证合格。

猜你喜欢

转载自blog.csdn.net/weixin_42433970/article/details/113725761