<?php
class QGPDF extends TCPDF
{
    public function __construct($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false)
    {
        parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
    }

    public function SetXY($x, $y, $rtloff = false)
    {
        parent::SetXY($x, $y, $rtloff);
    }    
}


DEFINE('META_DIR', \mvc::$cfg['dir']['static'] . '/images/pdf/ss');
DEFINE('LEFT_MARGIN', 3); //左边距 mm
DEFINE('RIGHT_MARGIN', 3); //左边距 mm
DEFINE('TOP_MARGIN', 5); //左边距 mm
DEFINE('BOTTOM_MARGIN', 5); //左边距 mm
class qgpdfSS {
    public $pdf;
    public $activeHeight;
    public function __construct(QGPDF $pdf)
    {
        $this->pdf = $pdf;
        //文档信息
        $this->pdf->SetCreator('QGPDF of YIPARTS');
        $this->pdf->SetAuthor('GSP PDC');
        $this->pdf->SetTitle('GSP 悬挂目录');
        $this->pdf->SetSubject('GSP 悬挂目录' . ' ' . date('Ym'));  
        
        //边距
        DEFINE('PAGE_WIDTH', $this->pdf->getPageWidth() - LEFT_MARGIN - RIGHT_MARGIN); //204
        DEFINE('PAGE_HEIGHT', $this->pdf->getPageHeight() - TOP_MARGIN - BOTTOM_MARGIN); //287
        $this->pdf->SetMargins(LEFT_MARGIN, TOP_MARGIN, RIGHT_MARGIN); // 左、上、右边距（单位：毫米）
        $this->pdf->SetAutoPageBreak(TRUE, BOTTOM_MARGIN); // 自动分页的底部边距

        //支持中文字体
        //$this->pdf->SetFont('stsongstdlight', '', 14);
        //arialuni
        $fontname = TCPDF_FONTS::addTTFfont(META_DIR . '/arial/arialuni.ttf', 'TrueTypeUnicode', '', 32);
    }
    public function addPage($type) {
        $this->pdf->AddPage();
        // 设置背景颜色 (RGB)
        $this->pdf->SetFillColor(0xF4, 0xF4, 0xF4);
        $this->pdf->Rect(LEFT_MARGIN, TOP_MARGIN, PAGE_WIDTH, PAGE_HEIGHT, 'F');
    }

    public function insertItem($x, $y, array $item, $stype = [])
    {
        $imgWidth = $stype['width'] ?: 30;
        $this->pdf->Image($item['imgfile'], LEFT_MARGIN, $y, $imgWidth, 0, 'PNG', '', '', false, 300, '', false, false, 0);
    }


    public function insertHeader(array $headerData) {
        $this->addPage('Header');

        $this->pdf->Image(META_DIR . '/1.png', LEFT_MARGIN, TOP_MARGIN, PAGE_WIDTH, 0, 'PNG', '', '', false, 300, '', false, false, 0);
        $this->pdf->SetTextColor(0xFF, 0xFF, 0xFF);

        //make
        $y = 5;
        $this->pdf->SetXY(27, $y);
        $this->pdf->SetFont('helvetica', 'BI', 16);
        $this->pdf->Write(0, $headerData['make']);

        //models
        $y = 12.5;
        $this->pdf->SetXY(25, $y);
        $this->pdf->SetFont('arialuni', 'I', 14);
        $this->pdf->Write(0, '①');
        $y = 13;
        $this->pdf->SetXY(31.5, $y);
        $this->pdf->SetFont('helvetica', 'I', 14);
        $this->pdf->Write(0, $headerData['models'][0]);
        $this->activeHeight = $y + 20;
    }
}

$pdf = new QGPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$qgpdfSS = new qgpdfSS($pdf);




$headerData = [
    'make' => 'AUDI',
    'models' => [
        'A4 B8 (8K2) 2007.11-2015.12',
        'A4 B8 Avant (8K5) 2007.11-2015.12',
        'A4/S4 B8 (8K2) 2012.09-2015.12',
    ]
];
$qgpdfSS->insertHeader($headerData);

$item = [
    'imgfile' => META_DIR . '/test/1.png',
];
$qgpdfSS->insertItem(LEFT_MARGIN, $qgpdfSS->activeHeight, $item, ['width'=> 68]);

$pdf->Output('example.pdf', 'I');