/** * Print a Linear Barcode. * @param string $code code to print * @param string $type type of barcode (see barcodes.php for supported formats). * @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 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 barcode insertion relative to barcode 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 ($this->empty_string($code)) { return; } require_once dirname(__FILE__) . '/barcodes.php'; // 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 1D 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; } $fontsize = 0; if (!isset($style['text'])) { $style['text'] = false; } if ($style['text'] and isset($style['font'])) { if (isset($style['fontsize'])) { $fontsize = $style['fontsize']; } $this->SetFont($style['font'], '', $fontsize); } if (!isset($style['stretchtext'])) { $style['stretchtext'] = 4; } // set foreground color $this->SetDrawColorArray($style['fgcolor']); $this->SetTextColorArray($style['fgcolor']); if ($this->empty_string($w) or $w <= 0) { if ($this->rtl) { $w = $this->x - $this->lMargin; } else { $w = $this->w - $this->rMargin - $this->x; } } if ($this->empty_string($x)) { $x = $this->GetX(); } if ($this->rtl) { $x = $this->w - $x; } if ($this->empty_string($y)) { $y = $this->GetY(); } if ($this->empty_string($xres)) { $xres = 0.4; } $fbw = $arrcode['maxw'] * $xres + 2 * $style['padding']; $extraspace = $this->cell_height_ratio * $fontsize / $this->k + 2 * $style['padding']; if ($this->empty_string($h) or $h <= 0) { $h = 10 + $extraspace; } $prev_x = $this->x; if ($this->checkPageBreak($h, $y)) { $y = $this->GetY() + $this->cMargin; if ($this->rtl) { $x += $prev_x - $this->x; } else { $x += $this->x - $prev_x; } } // maximum bar height $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, $style['border'] ? 'DF' : 'F', '', $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']) { // draw a vertical bar $ypos = $y + $style['padding'] + $v['p'] * $barh / $arrcode['maxh']; $this->Rect($xpos, $ypos, $bw, $v['h'] * $barh / $arrcode['maxh'], 'F', array(), $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 a Linear Barcode. * @param $code (string) code to print * @param $type (string) type of barcode (see tcpdf_barcodes_1d.php for supported formats). * @param $x (int) x position in user units (empty string = current x position) * @param $y (int) y position in user units (empty string = current y position) * @param $w (int) width in user units (empty string = remaining page width) * @param $h (int) height in user units (empty string = remaining page height) * @param $xres (float) width of the smallest bar in user units (empty string = default value = 0.4mm) * @param $style (array) array of options:<ul> * <li>boolean $style['border'] if true prints a border</li> * <li>int $style['padding'] padding to leave around the barcode in user units (set to 'auto' for automatic padding)</li> * <li>int $style['hpadding'] horizontal padding in user units (set to 'auto' for automatic padding)</li> * <li>int $style['vpadding'] vertical padding in user units (set to 'auto' for automatic padding)</li> * <li>array $style['fgcolor'] color array for bars and text</li> * <li>mixed $style['bgcolor'] color array for background (set to false for transparent)</li> * <li>boolean $style['text'] if true prints text below the barcode</li> * <li>string $style['label'] override default label</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> * <li>string $style['position'] horizontal position of the containing barcode cell on the page: L = left margin; C = center; R = right margin.</li> * <li>string $style['align'] horizontal position of the barcode on the containing rectangle: L = left; C = center; R = right.</li> * <li>string $style['stretch'] if true stretch the barcode to best fit the available width, otherwise uses $xres resolution for a single bar.</li> * <li>string $style['fitwidth'] if true reduce the width to fit the barcode width + padding. When this option is enabled the 'stretch' option is automatically disabled.</li> * <li>string $style['cellfitalign'] this option works only when 'fitwidth' is true and 'position' is unset or empty. Set the horizontal position of the containing barcode cell inside the specified rectangle: L = left; C = center; R = right.</li></ul> * @param $align (string) Indicates the alignment of the pointer next to barcode insertion relative to barcode 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) * @public */ public function write1DBarcode($code, $type, $x = '', $y = '', $w = '', $h = '', $xres = '', $style = '', $align = '') { if (TCPDF_STATIC::empty_string(trim($code))) { return; } require_once dirname(__FILE__) . '/tcpdf_barcodes_1d.php'; // save current graphic settings $gvars = $this->getGraphicVars(); // create new barcode object $barcodeobj = new TCPDFBarcode($code, $type); $arrcode = $barcodeobj->getBarcodeArray(); if ($arrcode === false or empty($arrcode) or $arrcode['maxw'] <= 0) { $this->Error('Error in 1D barcode string'); } if ($arrcode['maxh'] <= 0) { $arrcode['maxh'] = 1; } // set default values if (!isset($style['position'])) { $style['position'] = ''; } elseif ($style['position'] == 'S') { // keep this for backward compatibility $style['position'] = ''; $style['stretch'] = true; } if (!isset($style['fitwidth'])) { if (!isset($style['stretch'])) { $style['fitwidth'] = true; } else { $style['fitwidth'] = false; } } if ($style['fitwidth']) { // disable stretch $style['stretch'] = false; } if (!isset($style['stretch'])) { if ($w === '' or $w <= 0) { $style['stretch'] = false; } else { $style['stretch'] = true; } } 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; } $fontsize = 0; if (!isset($style['text'])) { $style['text'] = false; } if ($style['text'] and isset($style['font'])) { if (isset($style['fontsize'])) { $fontsize = $style['fontsize']; } $this->SetFont($style['font'], '', $fontsize); } if (!isset($style['stretchtext'])) { $style['stretchtext'] = 4; } if ($x === '') { $x = $this->x; } if ($y === '') { $y = $this->y; } // check page for no-write regions and adapt page margins if necessary list($x, $y) = $this->checkPageRegions($h, $x, $y); if ($w === '' or $w <= 0) { if ($this->rtl) { $w = $x - $this->lMargin; } else { $w = $this->w - $this->rMargin - $x; } } // padding if (!isset($style['padding'])) { $padding = 0; } elseif ($style['padding'] === 'auto') { $padding = 10 * ($w / ($arrcode['maxw'] + 20)); } else { $padding = floatval($style['padding']); } // horizontal padding if (!isset($style['hpadding'])) { $hpadding = $padding; } elseif ($style['hpadding'] === 'auto') { $hpadding = 10 * ($w / ($arrcode['maxw'] + 20)); } else { $hpadding = floatval($style['hpadding']); } // vertical padding if (!isset($style['vpadding'])) { $vpadding = $padding; } elseif ($style['vpadding'] === 'auto') { $vpadding = $hpadding / 2; } else { $vpadding = floatval($style['vpadding']); } // calculate xres (single bar width) $max_xres = ($w - 2 * $hpadding) / $arrcode['maxw']; if ($style['stretch']) { $xres = $max_xres; } else { if (TCPDF_STATIC::empty_string($xres)) { $xres = 0.141 * $this->k; // default bar width = 0.4 mm } if ($xres > $max_xres) { // correct xres to fit on $w $xres = $max_xres; } if (isset($style['padding']) and $style['padding'] === 'auto' or isset($style['hpadding']) and $style['hpadding'] === 'auto') { $hpadding = 10 * $xres; if (isset($style['vpadding']) and $style['vpadding'] === 'auto') { $vpadding = $hpadding / 2; } } } if ($style['fitwidth']) { $wold = $w; $w = $arrcode['maxw'] * $xres + 2 * $hpadding; if (isset($style['cellfitalign'])) { switch ($style['cellfitalign']) { case 'L': if ($this->rtl) { $x -= $wold - $w; } break; case 'R': if (!$this->rtl) { $x += $wold - $w; } break; case 'C': if ($this->rtl) { $x -= ($wold - $w) / 2; } else { $x += ($wold - $w) / 2; } break; default: break; } } } $text_height = $this->cell_height_ratio * $fontsize / $this->k; // height if ($h === '' or $h <= 0) { // set default height $h = $arrcode['maxw'] * $xres / 3 + 2 * $vpadding + $text_height; } $barh = $h - $text_height - 2 * $vpadding; if ($barh <= 0) { // try to reduce font or padding to fit barcode on available height if ($text_height > $h) { $fontsize = $h * $this->k / (4 * $this->cell_height_ratio); $text_height = $this->cell_height_ratio * $fontsize / $this->k; $this->SetFont($style['font'], '', $fontsize); } if ($vpadding > 0) { $vpadding = ($h - $text_height) / 4; } $barh = $h - $text_height - 2 * $vpadding; } // fit the barcode on available space list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false); // set alignment $this->img_rb_y = $y + $h; // set alignment if ($this->rtl) { if ($style['position'] == 'L') { $xpos = $this->lMargin; } elseif ($style['position'] == 'C') { $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2; } elseif ($style['position'] == 'R') { $xpos = $this->w - $this->rMargin - $w; } else { $xpos = $x - $w; } $this->img_rb_x = $xpos; } else { if ($style['position'] == 'L') { $xpos = $this->lMargin; } elseif ($style['position'] == 'C') { $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2; } elseif ($style['position'] == 'R') { $xpos = $this->w - $this->rMargin - $w; } else { $xpos = $x; } $this->img_rb_x = $xpos + $w; } $xpos_rect = $xpos; if (!isset($style['align'])) { $style['align'] = 'C'; } switch ($style['align']) { case 'L': $xpos = $xpos_rect + $hpadding; break; case 'R': $xpos = $xpos_rect + ($w - $arrcode['maxw'] * $xres) - $hpadding; break; case 'C': default: $xpos = $xpos_rect + ($w - $arrcode['maxw'] * $xres) / 2; break; } $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, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']); } elseif ($style['border']) { $this->Rect($xpos_rect, $y, $w, $h, 'D'); } // set foreground color $this->SetDrawColorArray($style['fgcolor']); $this->SetTextColorArray($style['fgcolor']); // print bars foreach ($arrcode['bcode'] as $k => $v) { $bw = $v['w'] * $xres; if ($v['t']) { // draw a vertical bar $ypos = $y + $vpadding + $v['p'] * $barh / $arrcode['maxh']; $this->Rect($xpos, $ypos, $bw, $v['h'] * $barh / $arrcode['maxh'], 'F', array(), $style['fgcolor']); } $xpos += $bw; } // print text if ($style['text']) { if (isset($style['label']) and !TCPDF_STATIC::empty_string($style['label'])) { $label = $style['label']; } else { $label = $code; } $txtwidth = $arrcode['maxw'] * $xres; if ($this->GetStringWidth($label) > $txtwidth) { $style['stretchtext'] = 2; } // print text $this->x = $xpos_text; $this->y = $y + $vpadding + $barh; $cellpadding = $this->cell_padding; $this->SetCellPadding(0); $this->Cell($txtwidth, '', $label, 0, 0, 'C', false, '', $style['stretchtext'], false, 'T', 'T'); $this->cell_padding = $cellpadding; } // restore original direction $this->rtl = $tempRTL; // restore previous settings $this->setGraphicVars($gvars); // set pointer to align the next 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; } $this->endlinex = $this->img_rb_x; }
/** * Print a Linear Barcode. * @param string $code code to print * @param string $type type of barcode (see barcodes.php for supported formats). * @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 in user units * @param float $xres width of the smallest bar in user units * @param array $style array of options:<ul><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 (set to 'auto' for automatic padding)</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><li>string $style['position'] barcode position on the page: L = left margin; C = center; R = right margin; S = stretch</li></ul> * @param string $align Indicates the alignment of the pointer next to barcode insertion relative to barcode 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 ($this->empty_string(trim($code))) { return; } require_once dirname(__FILE__) . '/barcodes.php'; // 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 1D barcode string'); } // set default values if (!isset($style['position'])) { $style['position'] = ''; } 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; } $fontsize = 0; if (!isset($style['text'])) { $style['text'] = false; } if ($style['text'] and isset($style['font'])) { if (isset($style['fontsize'])) { $fontsize = $style['fontsize']; } $this->SetFont($style['font'], '', $fontsize); } if (!isset($style['stretchtext'])) { $style['stretchtext'] = 4; } // set foreground color $this->SetDrawColorArray($style['fgcolor']); $this->SetTextColorArray($style['fgcolor']); if ($x === '') { $x = $this->x; } if ($y === '') { $y = $this->y; } if ($w === '' or $w <= 0) { if ($this->rtl) { $w = $this->x - $this->lMargin; } else { $w = $this->w - $this->rMargin - $this->x; } } if ($h === '' or $h <= 0) { // horizontal aspect ration by default $h = $w / 3; } if ($this->empty_string($xres)) { $xres = 0.4; } // padding if (!isset($style['padding'])) { $style['padding'] = 0; } elseif ($style['padding'] === 'auto') { $style['padding'] = $h / 4; } $fbw = $arrcode['maxw'] * $xres + 2 * $style['padding']; $extraspace = $this->cell_height_ratio * $fontsize / $this->k + 2 * $style['padding']; // maximum bar height $barh = $h; $h += $extraspace; // fit the barcode on available space $this->fitBlock($w, $h, $x, $y, false); // set alignment $this->img_rb_y = $y + $h; // set alignment if ($this->rtl) { if ($style['position'] == 'L') { $xpos = $this->lMargin; } elseif ($style['position'] == 'C') { $xpos = ($this->w + $this->lMargin - $this->rMargin - $fbw) / 2; } elseif ($style['position'] == 'R') { $xpos = $this->w - $this->rMargin - $fbw; } elseif ($style['position'] == 'S') { $fbw = $w; $xres = ($w - 2 * $style['padding']) / $arrcode['maxw']; $xpos = $x - $w; } else { $xpos = $x - $fbw; } $this->img_rb_x = $xpos; } else { if ($style['position'] == 'L') { $xpos = $this->lMargin; } elseif ($style['position'] == 'C') { $xpos = ($this->w + $this->lMargin - $this->rMargin - $fbw) / 2; } elseif ($style['position'] == 'R') { $xpos = $this->w - $this->rMargin - $fbw; } elseif ($style['position'] == 'S') { $fbw = $w; $xres = ($w - 2 * $style['padding']) / $arrcode['maxw']; $xpos = $x; } else { $xpos = $x; } $this->img_rb_x = $xpos + $fbw; } $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, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']); } elseif ($style['border']) { $this->Rect($xpos_rect, $y, $fbw, $h, 'D'); } // print bars foreach ($arrcode['bcode'] as $k => $v) { $bw = $v['w'] * $xres; if ($v['t']) { // draw a vertical bar $ypos = $y + $style['padding'] + $v['p'] * $barh / $arrcode['maxh']; $this->Rect($xpos, $ypos, $bw, $v['h'] * $barh / $arrcode['maxh'], 'F', array(), $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 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; } $this->endlinex = $this->img_rb_x; }