TCPDFBarcode::getBarcodeSVGcode PHP Method

getBarcodeSVGcode() public method

Return a SVG string representation of barcode.
public getBarcodeSVGcode ( $w = 2, $h = 30, $color = 'black' ) : string
$w (int) Minimum width of a single bar in user units.
$h (int) Height of barcode in user units.
$color (string) Foreground color (in SVG format) for bar elements (background is transparent).
return string SVG code.
    public function getBarcodeSVGcode($w = 2, $h = 30, $color = 'black')
    {
        // replace table for special characters
        $repstr = array("" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
        $svg = '<' . '?' . 'xml version="1.0" standalone="no"' . '?' . '>' . "\n";
        $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
        $svg .= '<svg width="' . round($this->barcode_array['maxw'] * $w, 3) . '" height="' . $h . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . "\n";
        $svg .= "\t" . '<desc>' . strtr($this->barcode_array['code'], $repstr) . '</desc>' . "\n";
        $svg .= "\t" . '<g id="bars" fill="' . $color . '" stroke="none">' . "\n";
        // print bars
        $x = 0;
        foreach ($this->barcode_array['bcode'] as $k => $v) {
            $bw = round($v['w'] * $w, 3);
            $bh = round($v['h'] * $h / $this->barcode_array['maxh'], 3);
            if ($v['t']) {
                $y = round($v['p'] * $h / $this->barcode_array['maxh'], 3);
                // draw a vertical bar
                $svg .= "\t\t" . '<rect x="' . $x . '" y="' . $y . '" width="' . $bw . '" height="' . $bh . '" />' . "\n";
            }
            $x += $bw;
        }
        $svg .= "\t" . '</g>' . "\n";
        $svg .= '</svg>' . "\n";
        return $svg;
    }

Usage Example

コード例 #1
0
ファイル: example_1d_svgi.php プロジェクト: ade24/vcorner
//
// TCPDF is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with TCPDF.  If not, see <http://www.gnu.org/licenses/>.
//
// See LICENSE.TXT file for more information.
// -------------------------------------------------------------------
//
// Description : Example for tcpdf_barcodes_1d.php class
//
//============================================================+
/**
 * @file
 * Example for tcpdf_barcodes_1d.php class
 * @package com.tecnick.tcpdf
 * @author Nicola Asuni
 * @version 1.0.000
 */
// include 1D barcode class
require_once dirname(__FILE__) . '/../../tcpdf_barcodes_1d.php';
// set the barcode content and type
$barcodeobj = new TCPDFBarcode('http://www.tcpdf.org', 'C128');
// output the barcode as SVG inline code
echo $barcodeobj->getBarcodeSVGcode(2, 40, 'black');
//============================================================+
// END OF FILE
//============================================================+