/** * Print Barcode. * @param string $code code to print * @param string $type type of barcode. * @param int $x x position in user units * @param int $y y position in user units * @param int $w width in user units * @param int $h height position in user units * @param float $xres width of the smallest bar in user units * @param array $style array of options:<ul><li>string $style["position"] barcode position inside the specified width: L = left (default for LTR); C = center; R = right (default for RTL); S = stretch</li><li>boolean $style["border"] if true prints a border around the barcode</li><li>int $style["padding"] padding to leave around the barcode in user units</li><li>array $style["fgcolor"] color array for bars and text</li><li>mixed $style["bgcolor"] color array for background or false for transparent</li><li>boolean $style["text"] boolean if true prints text below the barcode</li><li>string $style["font"] font name for text</li><li>int $style["fontsize"] font size for text</li><li>int $style["stretchtext"]: 0 = disabled; 1 = horizontal scaling only if necessary; 2 = forced horizontal scaling; 3 = character spacing only if necessary; 4 = forced character spacing</li></ul> * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul> * @author Nicola Asuni * @since 3.1.000 (2008-06-09) * @access public */ function write1DBarcode($code, $type, $x = '', $y = '', $w = '', $h = '', $xres = 0.4, $style = '', $align = '') { if (empty($code)) { return; } // save current graphic settings $gvars = $this->getGraphicVars(); // create new barcode object $barcodeobj = new TCPDFbarcode($code, $type); $arrcode = $barcodeobj->getBarcodeArray(); if ($arrcode === false) { $this->Error('Error in barcode string'); } // set default values if (!isset($style['position'])) { if ($this->rtl) { $style['position'] = 'R'; } else { $style['position'] = 'L'; } } if (!isset($style['padding'])) { $style['padding'] = 0; } if (!isset($style['fgcolor'])) { $style['fgcolor'] = array(0, 0, 0); // default black } if (!isset($style['bgcolor'])) { $style['bgcolor'] = false; // default transparent } if (!isset($style['border'])) { $style['border'] = false; } if (!isset($style['text'])) { $style['text'] = false; $fontsize = 0; } if ($style['text'] and isset($style['font'])) { if (isset($style['fontsize'])) { $fontsize = $style['fontsize']; } else { $fontsize = 0; } $this->SetFont($style['font'], '', $fontsize); } if (!isset($style['stretchtext'])) { $style['stretchtext'] = 4; } // set foreground color $this->SetDrawColorArray($style['fgcolor']); $this->SetTextColorArray($style['fgcolor']); if (empty($w) or $w <= 0) { if ($this->rtl) { $w = $this->x - $this->lMargin; } else { $w = $this->w - $this->rMargin - $this->x; } } if (empty($x)) { $x = $this->GetX(); } if ($this->rtl) { $x = $this->w - $x; } if (empty($y)) { $y = $this->GetY(); } if (empty($xres)) { $xres = 0.4; } $fbw = $arrcode['maxw'] * $xres + 2 * $style['padding']; $extraspace = $this->cell_height_ratio * $fontsize / $this->k + 2 * $style['padding']; if (empty($h)) { $h = 10 + $extraspace; } if ($y + $h > $this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak()) { //Automatic page break $x = $this->x; $ws = $this->ws; if ($ws > 0) { $this->ws = 0; $this->_out('0 Tw'); } $this->AddPage($this->CurOrientation); if ($ws > 0) { $this->ws = $ws; $this->_out(sprintf('%.3f Tw', $ws * $k)); } $this->x = $x; $y = $this->y; } // maximum bar heigth $barh = $h - $extraspace; switch ($style['position']) { case 'L': // left if ($this->rtl) { $xpos = $x - $w; } else { $xpos = $x; } break; case 'C': // center $xdiff = ($w - $fbw) / 2; if ($this->rtl) { $xpos = $x - $w + $xdiff; } else { $xpos = $x + $xdiff; } break; case 'R': // right if ($this->rtl) { $xpos = $x - $fbw; } else { $xpos = $x + $w - $fbw; } break; case 'S': // stretch $fbw = $w; $xres = ($w - 2 * $style['padding']) / $arrcode['maxw']; if ($this->rtl) { $xpos = $x - $w; } else { $xpos = $x; } break; } $xpos_rect = $xpos; $xpos = $xpos_rect + $style['padding']; $xpos_text = $xpos; // barcode is always printed in LTR direction $tempRTL = $this->rtl; $this->rtl = false; // print background color if ($style['bgcolor']) { $this->Rect($xpos_rect, $y, $fbw, $h, 'DF', '', $style['bgcolor']); } elseif ($style['border']) { $this->Rect($xpos_rect, $y, $fbw, $h, 'D'); } // print bars if ($arrcode !== false) { foreach ($arrcode['bcode'] as $k => $v) { $bw = $v['w'] * $xres; if ($v['t']) { // braw a vertical bar $ypos = $y + $style['padding'] + $v['p'] * $barh / $arrcode['maxh']; $this->Rect($xpos, $ypos, $bw, $v['h'] * $barh / $arrcode['maxh'], 'DF', array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0), $style['fgcolor']); } $xpos += $bw; } } // print text if ($style['text']) { // print text $this->x = $xpos_text; $this->y = $y + $style['padding'] + $barh; $this->Cell($arrcode['maxw'] * $xres, $this->cell_height_ratio * $fontsize / $this->k, $code, 0, 0, 'C', 0, '', $style['stretchtext']); } // restore original direction $this->rtl = $tempRTL; // restore previous settings $this->setGraphicVars($gvars); // set bottomcoordinates $this->img_rb_y = $y + $h; if ($this->rtl) { // set left side coordinate $this->img_rb_x = $this->w - $x - $w; } else { // set right side coordinate $this->img_rb_x = $x + $w; } // set pointer to align the successive text/objects switch ($align) { case 'T': $this->y = $y; $this->x = $this->img_rb_x; break; case 'M': $this->y = $y + round($h / 2); $this->x = $this->img_rb_x; break; case 'B': $this->y = $this->img_rb_y; $this->x = $this->img_rb_x; break; case 'N': $this->SetY($this->img_rb_y); break; default: break; } }
/** * Print Barcode. * @param string $code code to print * @param string $type type of barcode. * @param int $x x position in user units * @param int $y y position in user units * @param int $w width in user units * @param int $h height position in user units * @param float $xres width of the smallest bar in user units * @param array $style array of options:<ul><li>string $style["position"] barcode position inside the specified width: L = left (default for LTR); C = center; R = right (default for RTL); S = stretch</li><li>boolean $style["border"] if true prints a border around the barcode</li><li>int $style["padding"] padding to leave around the barcode in user units</li><li>array $style["fgcolor"] color array for bars and text</li><li>mixed $style["bgcolor"] color array for background or false for transparent</li><li>boolean $style["text"] boolean if true prints text below the barcode</li><li>string $style["font"] font name for text</li><li>int $style["fontsize"] font size for text</li><li>int $style["stretchtext"]: 0 = disabled; 1 = horizontal scaling only if necessary; 2 = forced horizontal scaling; 3 = character spacing only if necessary; 4 = forced character spacing</li></ul> * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul> * @author Nicola Asuni * @since 3.1.000 (2008-06-09) * @access public */ public function write1DBarcode($code, $type, $x = '', $y = '', $w = '', $h = '', $xres = 0.4, $style = '', $align = '') { if (empty($code)) { return; } $barcodeobj = new TCPDFbarcode($code, $type); $arrcode = $barcodeobj->getBarcodeArray(); if ($arrcode === false) { $this->Error('Error in barcode string'); } // set default values if (!isset($style["position"])) { if ($this->rtl) { $style["position"] = "R"; } else { $style["position"] = "L"; } } if (!isset($style["padding"])) { $style["padding"] = 0; } if (!isset($style["fgcolor"])) { $style["fgcolor"] = array(0, 0, 0); // default black } if (!isset($style["bgcolor"])) { $style["bgcolor"] = false; // default transparent } if (!isset($style["border"])) { $style["border"] = false; } if (!isset($style["text"])) { $style["text"] = false; $fontsize = 0; } if ($style["text"] and isset($style["font"])) { $prevFontFamily = $this->FontFamily; $prevFontStyle = $this->FontStyle; $prevFontSizePt = $this->FontSizePt; if (isset($style["fontsize"])) { $fontsize = $style["fontsize"]; } else { $fontsize = 0; } $this->SetFont($style["font"], '', $fontsize); } if (!isset($style["stretchtext"])) { $style["stretchtext"] = 4; } // set foreground color $prevDrawColor = $this->DrawColor; $prevTextColor = $this->TextColor; $this->SetDrawColorArray($style["fgcolor"]); $this->SetTextColorArray($style["fgcolor"]); if (empty($w) or $w <= 0) { if ($this->rtl) { $w = $this->x - $this->lMargin; } else { $w = $this->w - $this->rMargin - $this->x; } } if (empty($x)) { $x = $this->GetX(); } if ($this->rtl) { $x = $this->w - $x; } if (empty($y)) { $y = $this->GetY(); } if (empty($xres)) { $xres = 0.4; } $fbw = $arrcode["maxw"] * $xres + 2 * $style["padding"]; $extraspace = $this->cell_height_ratio * $fontsize / $this->k + 2 * $style["padding"]; if (empty($h)) { $h = 10 + $extraspace; } if ($y + $h > $this->PageBreakTrigger and empty($this->InFooter) and $this->AcceptPageBreak()) { //Automatic page break $x = $this->x; $ws = $this->ws; if ($ws > 0) { $this->ws = 0; $this->_out('0 Tw'); } $this->AddPage($this->CurOrientation); if ($ws > 0) { $this->ws = $ws; $this->_out(sprintf('%.3f Tw', $ws * $k)); } $this->x = $x; $y = $this->y; } // maximum bar heigth $barh = $h - $extraspace; switch ($style["position"]) { case "L": // left if ($this->rtl) { $xpos = $x - $w; } else { $xpos = $x; } break; case "C": // center $xdiff = ($w - $fbw) / 2; if ($this->rtl) { $xpos = $x - $w + $xdiff; } else { $xpos = $x + $xdiff; } break; case "R": // right if ($this->rtl) { $xpos = $x - $fbw; } else { $xpos = $x + $w - $fbw; } break; case "S": // stretch $fbw = $w; $xres = ($w - 2 * $style["padding"]) / $arrcode["maxw"]; if ($this->rtl) { $xpos = $x - $w; } else { $xpos = $x; } break; } $xpos_rect = $xpos; $xpos = $xpos_rect + $style["padding"]; $xpos_text = $xpos; // barcode is always printed in LTR direction $tempRTL = $this->rtl; $this->rtl = false; // print background color if ($style["bgcolor"]) { $this->Rect($xpos_rect, $y, $fbw, $h, 'DF', '', $style["bgcolor"]); } elseif ($style["border"]) { $this->Rect($xpos_rect, $y, $fbw, $h, 'D'); } // print bars if ($arrcode !== false) { foreach ($arrcode["bcode"] as $k => $v) { $bw = $v["w"] * $xres; if ($v["t"]) { // braw a vertical bar $ypos = $y + $style["padding"] + $v["p"] * $barh / $arrcode["maxh"]; $this->Rect($xpos, $ypos, $bw, $v["h"] * $barh / $arrcode["maxh"], 'DF', array("L" => 0, "T" => 0, "R" => 0, "B" => 0), $style["fgcolor"]); } $xpos += $bw; } } // print text if ($style["text"]) { // print text $this->x = $xpos_text; $this->y = $y + $style["padding"] + $barh; $this->Cell($arrcode["maxw"] * $xres, $this->cell_height_ratio * $fontsize / $this->k, $code, 0, 0, 'C', 0, '', $style["stretchtext"]); } // restore original direction $this->rtl = $tempRTL; // restore previous font if ($style["text"] and isset($style["font"])) { $this->SetFont($prevFontFamily, $prevFontStyle, $prevFontSizePt); } // restore colors $this->DrawColor = $prevDrawColor; $this->TextColor = $prevTextColor; // set bottomcoordinates $this->img_rb_y = $y + $h; if ($this->rtl) { // set left side coordinate $this->img_rb_x = $this->w - $x - $w; } else { // set right side coordinate $this->img_rb_x = $x + $w; } // set pointer to align the successive text/objects switch ($align) { case 'T': $this->y = $y; $this->x = $this->img_rb_x; break; case 'M': $this->y = $y + round($h / 2); $this->x = $this->img_rb_x; break; case 'B': $this->y = $this->img_rb_y; $this->x = $this->img_rb_x; break; case 'N': $this->SetY($this->img_rb_y); break; default: break; } }