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
Just see this question with the above answer, I think it is a width and height calculation question only.
The following solution may useful for almost 1D TCPDF label printing. The sample is fit for 4 columns and support page break, then adjust parameter you wish.
$pdf->SetMargins(10, 30);
$pdf->AddPage('P', 'A4');
$style_barcode = array(
'position' => '',
'align' => 'C',
'stretch' => false,
'fitwidth' => true,
'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' => 4
);
// Set some content to print
$counter = 1;
$label_cols = 4; // No. of Columns
$label_w = 40; // Label width
$label_w_base_margin = 10; // Label width base margin
$label_h = 15; // Label height
$label_h_base_margin = 10; // Label height base margin - Do not change except you know what you are doing
$label_h_margin = $label_h - 6.5; // Label height margin - Do not change except you know what you are doing
// print a block of text using Write()
$txt = "Test";
$pdf->SetFont('helvetica', '', 10); // cid0jp or msungstdlight can display chinese
//$style_barcode['position'] = 'L';
$x = $pdf->GetX();
$y = $pdf->GetY();
for ($i=1; $i <= 80; $i++) {
if ($y >= 250) { // Set Page break if GetY >= 250 (may be use Constructor but no time to review class code at all, sorry)
$pdf->AddPage('P', 'A4');
$y = $pdf->GetY();
}
$pdf->write1DBarcode('AABBCCDD'.$i, 'C39', $x, $y, $label_w, $label_h, 0.4, $style_barcode, 'L');
$x = $x + $label_w + $label_w_base_margin;
if($counter == $label_cols){
$pdf->Ln($label_h);
$counter = 1;
$x = $pdf->GetX();
$y = $y + $label_h_base_margin + $label_h_margin;
}else{
$counter++;
}
}
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 .
<?php
include("dbconfig.php");
require_once('../tcpdf/tcpdf.php');
// include 1D barcode class (search for installation path)
require_once('../tcpdf/tcpdf_barcodes_1d.php');
date_default_timezone_set('Asia/Kolkata');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, 'mm', 'A4', true, 'UTF-8', false);
// set document information
$pdf->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');
?>