How can I use TCPDF to make 2x6 sheets of labels that include 2D barcodes WITHOUT using columns OR 3rd-party classes?

后端 未结 2 424
情深已故
情深已故 2021-01-14 05:57

For three weeks (I guess I\'m that slows!) I\'ve tried to use TCPDF\'s own examples as well as some suggestions found on this site to figure out how to do this using databas

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 06:38

    Take a look at my solution - it can print 24 labels - 3*8 into an A4 . just make a simple change in width , height and the $qty , and u can get 2*6 .

    SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Casual Creators');
    $pdf->SetTitle('Bar Code Labels');
    $pdf->SetSubject('');
    $pdf->SetKeywords('');
    
    //remove header and footer
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
    // set margins
    $pdf->setTopMargin(13.0);
    $pdf->SetRightMargin(6.0);
    
    $pdf->setHeaderMargin(13);
    $pdf->SetFooterMargin(13.0); //13mm
    
    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, 13.0);
    
    // set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
    
    
    // set a barcode on the page footer
    //$pdf->setBarcode(date('Y-m-d H:i:s'));
    
    // set font
    $pdf->SetFont('helvetica', '', 11);
    
    // add a page
    $pdf->AddPage();
    
    // print a message
    //$txt = "";
    //$pdf->MultiCell(70, 50, $txt, 0, 'J', false, 1, 125, 30, true, 0, false, true, 0, 'T', false);
    //$pdf->SetY(30);
    
    // -----------------------------------------------------------------------------
    
    $pdf->SetFont('helvetica', '', 10);
    
    // define barcode style
    $style = array(
            'position' => '',
            'align' => 'L',
            'stretch' => false,
            'fitwidth' => false,
            'cellfitalign' => '',
            'border' => false,
            'hpadding' => 'auto',
            'vpadding' => 'auto',
            'fgcolor' => array(0,0,0),
            'bgcolor' => false, //array(255,255,255),
            'text' => true,
            'font' => 'helvetica',
            'fontsize' => 8,
            'stretchtext' => 0
    );
    
    
    //63.5 mm label width
    //33.9mm label height
    //2.5 mm gap between
    
    // PRINT VARIOUS 1D BARCODES
    
    // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
    
    
    $query1 = "SELECT * FROM PRODUCTS WHERE ID = '123456' ";
    
    $result1 = $conn->query($query1);
    $qty = 24;
    $item ;
    
    if ($result1-> num_rows > 0)  {
    
        $row = $result1 -> fetch_assoc();
    
        $item = $row["id"];
    }
    else {
        echo 'DbFailed';
    }
    
    $counter = 1;
    
    $i = '0' ;
    
    for( ; $i < $qty ; $i++)
    {
    
        $x = $pdf->GetX();
        $y = $pdf->GetY();
        $pdf->setCellMargins(0,0,2.5,0);
        // The width is set to the the same as the cell containing the name.
        // The Y position is also adjusted slightly.
        $pdf->write1DBarcode($item , 'C39', $x-2.5, $y-6.5, 63.5, 18, 0.4, $style, 'L');
        //Reset X,Y so wrapping cell wraps around the barcode's cell.
        $pdf->SetXY($x,$y);
        $pdf->Cell(63.5, 25, 'MyProduct', 0, 0, 'L', FALSE, '', 0, FALSE, 'C', 'B');
        $pdf->SetXY($x,$y);
        $pdf->Cell(63.5, 33, 'Price', 0, 0, 'L', FALSE, '', 0, FALSE, 'C', 'B');
    
        if($counter == 3)
        {
            $pdf->Ln(33.9);
            $counter = 1;
        }else{
            $counter++;
        }
    
    }
    
    
    
    
    // ---------------------------------------------------------
    ob_end_clean();
    //Close and output PDF document
    $pdf->Output('barcodes.pdf', 'I');
    
    ?>
    

提交回复
热议问题