JewelThief
JewelThief

Reputation: 611

how to write barcode in html format when using tcpdf

I am using TCPDF to generate PDF file using following command

$pdf->writeHTML($htmlcontent, true, 0, true, 0);

TCPDF also provides a way to create barcode with following commands

$pdf->Cell(0, 0, 'C39+', 0, 1);
$pdf->write1DBarcode('Code 39', 'C39+', '', '', 80, 15, 0.4, $style, 'N');
$pdf->Ln();

I want to be able to write barcode as part of the HTML code above. Is there easy way?

I can potentially call a barcode image inthe writeHTML code above, but not sure how to use above barcode function (or any in TCPDF) which would allow me to create image and then get that image into HTML generation.

Upvotes: 4

Views: 24720

Answers (5)

Cherry Chan
Cherry Chan

Reputation: 1

I tried the following and it worked:

$params = $pdf->serializeTCPDFtagParameters(
    array('https://tcpdf.org/', 'QRCODE,H', '', '', 27, 27, '', 'N')
);

$html .= '<tcpdf method="write2DBarcode" params="'.$params.'" />';

Upvotes: 0

Jonathan Oliver
Jonathan Oliver

Reputation: 5267

When generating a barcode, make sure that you enclose the 12-digit delivery point inside of the "slash" character. Most POSTNET fonts render the slash character as the "control" character that pre/post-fixes the barcode values. Without those control characters the barcode isn't technically valid.

The POSTNET barcode font in TrueType format can be downloaded.

Upvotes: 0

Nicola Asuni
Nicola Asuni

Reputation: 1550

TCPDF barcode classes already contains methods to export barcodes in various formats (SVG, PNG and HTML).

2D example:

require_once(dirname(__FILE__).'/2dbarcodes.php');
$barcodeobj = new TCPDF2DBarcode('http://www.tcpdf.org', 'QRCODE,H');

// export as SVG image
//$barcodeobj->getBarcodeSVG(3, 3, 'black');

// export as PNG image
//$barcodeobj->getBarcodePNG(3, 3, array(0,128,0));

// export as HTML code
echo $barcodeobj->getBarcodeHTML(3, 3, 'black');

1D example:

require_once(dirname(__FILE__).'/barcodes.php');
$barcodeobj = new TCPDFBarcode('123456', 'C128');

// export as SVG image
//$barcodeobj->getBarcodeSVG(2, 30, 'black');

// export as PNG image
//$barcodeobj->getBarcodePNG(2, 30, array(0,128,0));

// export as HTML code
echo $barcodeobj->getBarcodeHTML(2, 30, 'black');

Check the documentation and examples at http://www.tcpdf.org for further information.

Upvotes: 8

Sanjeev Chauhan
Sanjeev Chauhan

Reputation: 4097

You can write TCPDF Methods in HTML as below

<?php
$params = $pdf->serializeTCPDFtagParameters(array('40144399300102444888207482244309', 'C128C', '', '', 0, 0, 0.2, array('position'=>'S', 'border'=>false, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>false, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>2), 'N'));    
$str='<table cellspacing="0" cellpadding="1" border="0">            
<tr> 
    <td align="left">barcode</td>
</tr>
<tr> 
    <td align="center" style="padding-left:5px;">';
    $str .= '<tcpdf method="write1DBarcode" params="'.$params.'" />';
    $str .='</td>
</tr>
</table>';

$pdf->writeHTML($str,true, false,false,false,'left');
$pdf->Output('example_049.pdf', 'I');
?>

For detail reference please check TCPDF example_049.php

Upvotes: 14

Truman Leung
Truman Leung

Reputation: 143

You could put your barcode number is a fake HTML tag and then parse for that tag as you write out the HTML like in this example.

This would be in your HTML:

some HTML.... <POSTNET>12345-1234</POSTNET> ....some more HTML

This is the code to parse for the fake tag.

        // look to see if there is a POSTNET tag
        if (strpos($letter_html, "<POSTNET>") !== false) {
            $postnet_pre = explode("<POSTNET>", $letter_html);
            $this->WriteHTML($postnet_pre[0], $this->line_height);

            // write the barcode
            $postnet_post = explode("</POSTNET>", $postnet_pre[1]);
            $zip_code = $postnet_post[0];
            $this->write1DBarcode($zip_code, 'POSTNET', '', '', 80, 15, 0.4, $style, 'N');

            // write rest of the letter
            $this->WriteHTML($postnet_post[1], $this->line_height);

        } else {

            // no POSTNET so just write the whole letter
            $this->WriteHTML($letter_html, $this->line_height);
        }

Upvotes: 2

Related Questions