title: Print pdf with table with barcode in codeigniter
body: <p>I'm trying to print a table in pdf where 1 of the columns is a barcode.
  I'm able to set barcode in list in view but not in PDF.</p>
  
  <p>From what i have searched i have 2 methods TCPDF set in controller or js ajax set in view. At this moment i have tried both but unable to set the right code to output pdf as i want.</p>
  
  <p>method 1 TCPDF in controller. In view i have a button which directs to controller to display data where id_cod_obra=?
  <a href="https://i.stack.imgur.com/SimSO.png" rel="nofollow noreferrer">this is the view</a>
  <a href="https://i.stack.imgur.com/ubnpg.png" rel="nofollow noreferrer">this is the pdf generated but no barcode print</a></p>
  
  <pre><code> function createPDF(){
  
  
  
  
  
          if ($this-&gt;session-&gt;userdata('logged_in')) { // VALIDA USU�RIO LOGADO
              $this-&gt;load-&gt;model('model_perfil');
              $resultadoPerfil = $this-&gt;model_perfil-&gt;buscaPerfil();
              $dados ['resultadoPerfil'] = $resultadoPerfil;
              $this-&gt;load-&gt;model('htmltopdf_model');
              $this-&gt;load-&gt;library('pdf');
  
  
              /////////////////////////////////////
  
  
      $this-&gt;load-&gt;library('Pdf');
  
  
      // $this-&gt;pdf-&gt;setPaper('A4', 'landscape');
        $this-&gt;pdf-&gt;setPaper('A4', 'portrait');
  
  
              ////////////////////////////////////////
  
              $id_cod_obra = $this-&gt;input-&gt;get('id_cod_obra');
              //   $id = $this-&gt;uri-&gt;segment(2);
              $html_content= $this-&gt;htmltopdf_model-&gt;carregapecasfiltro($id_cod_obra);
              //$html_content ['html_content'] = $html_content;
              $html_content = '&lt;h3 align="center"&gt;Convert HTML to PDF in CodeIgniter using Dompdf MERDA ISTO ESTA A DEMORAR 2&lt;/h3&gt;';
  
  
  
      $template = array('table_open' =&gt; '&lt;table border="1" cellpadding="2" cellspacing="1"&gt;');
  
      $this-&gt;table-&gt;set_template($template);
  
      $this-&gt;table-&gt;set_heading('ID peça', 'ID obra', 'nome ', 'Nr Subcj', 'mb ', 'ma ', 'Codigo Barrras ');
  
       $this-&gt;load-&gt;model('model_peca');
  
  
       // CODE 128 A
  //$pdf-&gt;Cell(0, 0, 'CODE 128 A', 0, 1);
  //$pdf-&gt;write1DBarcode('CODE 128 A', 'C128A', '', '', '', 18, 0.4, $style, 'N');
  //$pdf-&gt;Ln();
  
  //$params = $pdf-&gt;serializeTCPDFtagParameters(array('CODE 128', 'C128', '', '', 80, 30, 0.4, array('position'=&gt;'S', 'border'=&gt;true, 'padding'=&gt;4, 'fgcolor'=&gt;array(0,0,0), 'bgcolor'=&gt;array(255,255,255), 'text'=&gt;true, 'font'=&gt;'helvetica', 'fontsize'=&gt;8, 'stretchtext'=&gt;4), 'N'));
  //$html .= '&lt;tcpdf method="write1DBarcode" params="'.$sf-&gt;codigoean.'" /&gt;';
  
      $html_content= $this-&gt;htmltopdf_model-&gt;carregapecasfiltro($id_cod_obra);
       $dados ['html_content'] = $html_content;
  
              foreach($html_content as $s){
  
          //  $codigoean=['codigoean'];
  
              //$codigoean=$row['codigoean'];
          //  $codigoean = $pdf-&gt;serializeTCPDFtagParameters(array($codigoean, 'C128', '', '', 80, 30, 0.4, array('position'=&gt;'S', 'border'=&gt;true, 'padding'=&gt;4, 'fgcolor'=&gt;array(0,0,0), 'bgcolor'=&gt;array(255,255,255), 'text'=&gt;true, 'font'=&gt;'helvetica', 'fontsize'=&gt;8, 'stretchtext'=&gt;4), 'N'));
  //
  
  
  
                                              ;}
  //$codigoean = $pdf-&gt;serializeTCPDFtagParameters(array($codigoean, 'C128', '', '', 80, 30, 0.4, array('position'=&gt;'S', 'border'=&gt;true, 'padding'=&gt;4, 'fgcolor'=&gt;array(0,0,0), 'bgcolor'=&gt;array(255,255,255), 'text'=&gt;true, 'font'=&gt;'helvetica', 'fontsize'=&gt;8, 'stretchtext'=&gt;4), 'N'));
  
  
  
                              //      var_dump( $this-&gt;table-&gt;add_row($s-&gt;codigoean()) );
                              //      die;
  
  
      // $html .= '&lt;tcpdf method="write1DBarcode" params="'.$sf-&gt;codigoean.'" /&gt;'
      //$html .= '&lt;tcpdf method="write1DBarcode" params="' . $params . '" /&gt;';
      //$codigoean = $pdf-&gt;serializeTCPDFtagParameters(array('CODE 128C+', 'C128C', '', '', 80, 30, 0.4, array('position' =&gt; 'S', 'border' =&gt; true, 'padding' =&gt; 4, 'fgcolor' =&gt; array(0, 0, 0), 'bgcolor' =&gt; array(255, 255, 255), 'text' =&gt; true, 'font' =&gt; 'helvetica', 'fontsize' =&gt; 8, 'stretchtext' =&gt; 4), 'N'));
  
      foreach ($html_content as $sf):
  
          $this-&gt;table-&gt;add_row($sf-&gt;id, $sf-&gt;id_cod_obra, $sf-&gt;nome , $sf-&gt;qtd, $sf-&gt;nr_de_peca, $sf-&gt;ma, $barcode );
      //  
          // $this-&gt;table-&gt;add_row($sf-&gt;codigoean );
  
      endforeach;
      $html_content = $this-&gt;table-&gt;generate();
  
  
  
  
     $this-&gt;pdf-&gt;loadHtml($html_content);
     $this-&gt;pdf-&gt;render();
     $this-&gt;pdf-&gt;stream("".$id_cod_obra.".pdf", array("Attachment"=&gt;1));
     //  $this-&gt;pdf-&gt;stream('my.pdf', array("Attachment"=&gt;0));
    // $dompdf-&gt;stream('my.pdf',array('Attachment'=&gt;0));
  
  
  
  
       }}
  
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/5EVAL.png" rel="nofollow noreferrer">This the datatable but unable to put barcodes inside table</a>
  2 method in view with AJAX JS</p>
  
  <pre><code>
  
  
        &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"&gt;&lt;/script&gt;
    &lt;link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.9,af-2.0.0,b-1.0.3,b-colvis-1.0.3,b-html5-1.0.3,b-print-1.0.3,se-1.0.1/datatables.min.css"/&gt;
    &lt;script type="text/javascript" src="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.9,af-2.0.0,b-1.0.3,b-colvis-1.0.3,b-html5-1.0.3,b-print-1.0.3,se-1.0.1/datatables.min.js"&gt;&lt;/script&gt;
    &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /&gt;
    &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"&gt;&lt;/script&gt;
   &lt;script src="&lt;?php echo site_url('assets/JsBarcode-3.11.0/dist/JsBarcode.all.js'); ?&gt;"&gt;&lt;/script&gt;
     &lt;script&gt;
      function textToBase64Barcode(text){
        var canvas = document.createElement("canvas");
        JsBarcode(canvas, text, {format: "CODE39"});
        return canvas.toDataURL("image/png");
      }
    &lt;/script&gt;
  
  
  &lt;div class="content-wrapper"&gt;
      &lt;section class="content-header"&gt;
  
          &lt;section class="content-header"&gt;
          &lt;h1&gt;
              Requisi&amp;ccedil;&amp;atilde;o AJAX
          &lt;/h1&gt;
          &lt;ol class="breadcrumb"&gt;
              &lt;li&gt;&lt;a href="#"&gt;&lt;i class="fa fa-dashboard"&gt;&lt;/i&gt; Home&lt;/a&gt;&lt;/li&gt;
              &lt;li class="active"&gt;RequisiÃ§ao AJAX&lt;/li&gt;
          &lt;/ol&gt;
      &lt;/section&gt;
  
  
  
  
   &lt;div class="box"&gt;
  
    &lt;html lang="en"&gt;
        &lt;head&gt;
          &lt;!-- Required meta tags --&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
  
  
  
  
  
  
  
        &lt;/head&gt;
  
  
        &lt;body style="background: #c9c3c3"&gt;
  
          &lt;div class="container"&gt;
              &lt;div class="table justify-content-center"&gt;
  
  
  
  
  ////////////JUST TO TEST IF BARCODE RENDER IN VIEW "TEST"///////
  
          &lt;svg id="example2"&gt;&lt;/svg&gt;
            &lt;a id="hello"&gt;Click Here&lt;/a&gt;
  
  
  
  
  
  
                  &lt;div class="col-10 mb-5"&gt;
                     &lt;div class="card mt-3"&gt;
                         &lt;div class="card-body"&gt;
  
  
  
                             &lt;h5 class="card-title text-center"&gt;---------------------------&lt;/h5&gt;
  
  
                                &lt;div class="table-responsive"&gt;
  
  
  
  
      &lt;table id="example" class="table  table-bordered table-striped"&gt;
       &lt;thead&gt;  
         &lt;tr&gt;  
          &lt;th&gt;#&lt;/th&gt;  
           &lt;th&gt;id&lt;/th&gt;  
           &lt;th&gt;obra&lt;/th&gt;  
           &lt;th&gt;nome&lt;/th&gt;  
           &lt;th&gt;qtd&lt;/th&gt;  
           &lt;th&gt;data&lt;/th&gt;  
           &lt;th&gt;Nr CJ&lt;/th&gt;  
           &lt;th&gt;NR Peça&lt;/th&gt;  
           &lt;th&gt;Prep&lt;/th&gt;
           &lt;th&gt;Doc&lt;/th&gt;
           &lt;th&gt;Tipo&lt;/th&gt;
           &lt;th&gt;Desc.&lt;/th&gt;
           &lt;th&gt;CB&lt;/th&gt;
           &lt;th&gt;area&lt;/th&gt;
           &lt;th&gt;peso&lt;/th&gt;
           &lt;th&gt;Kg MA&lt;/th&gt;
           &lt;th&gt;status&lt;/th&gt;
  
         &lt;/tr&gt;  
       &lt;/thead&gt;  
       &lt;tfoot&gt;  
  
          &lt;tr&gt;  
  
         &lt;/tr&gt;  
       &lt;/tfoot&gt;  
       &lt;tbody&gt;  
         &lt;tr&gt;  
                       &lt;?php if(!empty($peca)): ?&gt;
                                  &lt;?php foreach($peca as $pecas):                                 
                                  ?&gt;
                                  &lt;tr&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;id?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;id_cod_obra?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;nome?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;qtd?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;data_entrada?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;nr_subconjunto?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;nr_de_peca?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;preparou?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;doc_anexo?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;tipo_doc?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;descricao?&gt;&lt;/th&gt;
  
                                      &lt;!--            &lt;th&gt;&lt;/th&gt; --&gt;
  
  
                                      &lt;!--    &lt;th&gt;&lt;?//=   $pecas-&gt;codigoean?&gt;&lt;/th&gt;
  
                                      &lt;td&gt;&lt;?php// echo $pecas-&gt;codigoean;?&gt;&lt;svg id="hello"&gt;&lt;/svg&gt;&lt;/td&gt;
  
  
                          &lt;/th&gt;
                                      &lt;img src="&lt;?php //echo base_url('Barcode/barcode_generator')?&gt;/code128a/15/ ?&gt; &lt;th&gt;&lt;?//= $pecas-&gt;codigo_barras ?&gt; &lt;/th&gt; ;?&gt;/true"/&gt;
                                      &lt;/th&gt;
  
  
                              &lt;td&gt; 
  
                                              &lt;img src="&lt;?php// echo base_url('Barcode/barcode_generator')?&gt;/code128a/15/&lt;?php//  echo// $pecas-&gt;codigoean  ;?&gt;/true"/&gt;
  
  
  
  
  
                                                                          &lt;/td&gt;--&gt;
  
  
  
                                      &lt;th&gt;&lt;?= $pecas-&gt;area?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;peso?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;kg_solda?&gt;&lt;/th&gt;
                                      &lt;th&gt;&lt;?= $pecas-&gt;status?&gt;&lt;/th&gt;
  
  
  
  
                                  &lt;/tr&gt;
                                  &lt;?php endforeach ;?&gt;
                              &lt;?php endif;?&gt;
         &lt;/tr&gt;  
         &lt;tr&gt;  
  
       &lt;/tbody&gt;  
     &lt;/table&gt; 
                         &lt;/div&gt;
                     &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
  
        &lt;/body&gt;
      &lt;/html&gt;
  
              &lt;/div&gt;
  
          &lt;/div&gt;
  
  
  
      &lt;/section&gt;
  &lt;/div&gt;
  
  
  
  
  
   &lt;script src="&lt;?php echo site_url('assets/JsBarcode-3.11.0/dist/JsBarcode.all.js'); ?&gt;"&gt;&lt;/script&gt;
  
  
  
  &lt;script type="text/javascript" language="javascript" &gt;
  
  
  
  
    $(document).ready(function(){
  
    $('#example').DataTable({
        // Processing indicator
     "processing" : true,
     // DataTables server-side processing mode
     "serverSide" : true,
             // Initial no order.
      "order": [],
      // Load data from an Ajax source
     "ajax" : {
       "url": "getLists",
      type:"POST"
     },
     dom: 'lBfrtip',
     buttons: [
      'excel', 'csv', 'pdf', 'copy'
     ],
     "lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ],
          "aaSorting": [[0, 'desc']],
          "aoColumnDefs": [
              {"sType": "num-html", "aTargets": [0]}
  
          ],
  
  
    });
  
  
   });
  
    $("#example2").JsBarcode("TEST");
  
    document.querySelector("#hello").href = textToBase64Barcode("HELLO");
  
  
  
  
  &lt;/script&gt;
  </code></pre>
  
  <p>In 1 TCPDF i print the table with barcode in numbers.
  In 1 i am also able to insert in view a barcode column with this code</p>
  
  <pre><code>&lt;img src="&lt;?php echo base_url('Barcode/barcode_generator')?&gt;/code128a/15/&lt;?php  echo $pecas-&gt;codigoean  ;?&gt;/true"/&gt;
  </code></pre>
  
  <p>In 2 AJAX JS  i print the table with barcode in numbers. And all works excell/pdf/copy</p>
  
  <p>The JSBarcode just for test outputs correctly the barcode i put for "TEST"</p>