Search code examples
phpcodeigniterzend-framework

How to generate barcode in PDF using zend library in codeigniter 3.x


i am using zend library to generate barcode how to generate barcode with heading. below is my code

public function barcode($visitor_id) {
        $this->load->library('zend');
        $this->zend->load('Zend/Barcode');
        Zend_Barcode::render('code39', 'image', array('text' => $visitor_id, 'drawText' => TRUE), array());
    }

Solution

  • First create a pdf file such as viewpdf.php using mpdf and call the barcode function in tag.

    create viewpdf.php in view folder

      <?php
     $code= KD12345;
    
    include("mpdf/mpdf.php");
    $html='<html>
    <head>
      <style>
        .container{width: 1450px; padding-bottom:2px;}
        body, table {
            /* to centre page on screen*/
            margin:0 auto;
            font-size: 12px;
            border-collapse: collapse;
        }
    </style>
    </head>
    <body>
    <div class="container" style="padding-top:2%;">
               <img class="img-responsive" style="text-align:center;" src="'. base_url().'index.php/Controller/barcode?bcd='.$code.'" alt="">
            </div> 
    </body>
    </html>';
    
    $mpdf=new mPDF('', 'A4', 0, '', 2, 2,5, 0, 0, 0);
    header("Content-type:application/pdf");
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->WriteHTML($html);
    $mpdf->Output($code.'.PDF','I');
    exit;
    ?>
    

    In controller folder Controller.php write a following function to display viewpdf.php

    public function ViewPdf()
        {   
            $this->load->view("viewpdf.php");
        }
    

    and another function for generate barcode

    public function barcode()
        {
            $code=$_GET['bcd'];
            $this->load->library('zend');
            $this->zend->load('Zend/Barcode');
    
            $barcodeOptions = array('text' => $code);
            $rendererOptions = array('imageType'=>'png');
            Zend_Barcode::factory('code128', 'image', $barcodeOptions, $rendererOptions)->render();
    
        }