在Codeigniter中使用表格和条形码打印pdf

时间:2019-04-05 10:55:31

标签: codeigniter pdf tcpdf barcode-printing jsbarcode

我正在尝试以pdf格式打印表格,其中1列是条形码。 我可以在视图列表中设置条形码,但不能在PDF中设置条形码。

根据我的搜索,我在控制器中设置了2种方法TCPDF或在视图中设置了js ajax。目前,我已经尝试了两种方法,但是无法设置正确的代码以根据需要输出pdf。

方法1控制器中的TCPDF。在视图中,我有一个指向控制器的按钮,用于显示id_cod_obra =?的数据。 this is the view this is the pdf generated but no barcode print

 function createPDF(){





        if ($this->session->userdata('logged_in')) { // VALIDA USU�RIO LOGADO
            $this->load->model('model_perfil');
            $resultadoPerfil = $this->model_perfil->buscaPerfil();
            $dados ['resultadoPerfil'] = $resultadoPerfil;
            $this->load->model('htmltopdf_model');
            $this->load->library('pdf');


            /////////////////////////////////////


    $this->load->library('Pdf');


    // $this->pdf->setPaper('A4', 'landscape');
      $this->pdf->setPaper('A4', 'portrait');


            ////////////////////////////////////////

            $id_cod_obra = $this->input->get('id_cod_obra');
            //   $id = $this->uri->segment(2);
            $html_content= $this->htmltopdf_model->carregapecasfiltro($id_cod_obra);
            //$html_content ['html_content'] = $html_content;
            $html_content = '<h3 align="center">Convert HTML to PDF in CodeIgniter using Dompdf MERDA ISTO ESTA A DEMORAR 2</h3>';



    $template = array('table_open' => '<table border="1" cellpadding="2" cellspacing="1">');

    $this->table->set_template($template);

    $this->table->set_heading('ID peça', 'ID obra', 'nome ', 'Nr Subcj', 'mb ', 'ma ', 'Codigo Barrras ');

     $this->load->model('model_peca');


     // CODE 128 A
//$pdf->Cell(0, 0, 'CODE 128 A', 0, 1);
//$pdf->write1DBarcode('CODE 128 A', 'C128A', '', '', '', 18, 0.4, $style, 'N');
//$pdf->Ln();

//$params = $pdf->serializeTCPDFtagParameters(array('CODE 128', 'C128', '', '', 80, 30, 0.4, array('position'=>'S', 'border'=>true, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>4), 'N'));
//$html .= '<tcpdf method="write1DBarcode" params="'.$sf->codigoean.'" />';

    $html_content= $this->htmltopdf_model->carregapecasfiltro($id_cod_obra);
     $dados ['html_content'] = $html_content;

            foreach($html_content as $s){

        //  $codigoean=['codigoean'];

            //$codigoean=$row['codigoean'];
        //  $codigoean = $pdf->serializeTCPDFtagParameters(array($codigoean, 'C128', '', '', 80, 30, 0.4, array('position'=>'S', 'border'=>true, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>4), 'N'));
//



                                            ;}
//$codigoean = $pdf->serializeTCPDFtagParameters(array($codigoean, 'C128', '', '', 80, 30, 0.4, array('position'=>'S', 'border'=>true, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>4), 'N'));



                            //      var_dump( $this->table->add_row($s->codigoean()) );
                            //      die;


    // $html .= '<tcpdf method="write1DBarcode" params="'.$sf->codigoean.'" />'
    //$html .= '<tcpdf method="write1DBarcode" params="' . $params . '" />';
    //$codigoean = $pdf->serializeTCPDFtagParameters(array('CODE 128C+', 'C128C', '', '', 80, 30, 0.4, array('position' => 'S', 'border' => true, 'padding' => 4, 'fgcolor' => array(0, 0, 0), 'bgcolor' => array(255, 255, 255), 'text' => true, 'font' => 'helvetica', 'fontsize' => 8, 'stretchtext' => 4), 'N'));

    foreach ($html_content as $sf):

        $this->table->add_row($sf->id, $sf->id_cod_obra, $sf->nome , $sf->qtd, $sf->nr_de_peca, $sf->ma, $barcode );
    //  
        // $this->table->add_row($sf->codigoean );

    endforeach;
    $html_content = $this->table->generate();




   $this->pdf->loadHtml($html_content);
   $this->pdf->render();
   $this->pdf->stream("".$id_cod_obra.".pdf", array("Attachment"=>1));
   //  $this->pdf->stream('my.pdf', array("Attachment"=>0));
  // $dompdf->stream('my.pdf',array('Attachment'=>0));




     }}

This the datatable but unable to put barcodes inside table 结合AJAX JS的2种方法




      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <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"/>
  <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"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
 <script src="<?php echo site_url('assets/JsBarcode-3.11.0/dist/JsBarcode.all.js'); ?>"></script>
   <script>
    function textToBase64Barcode(text){
      var canvas = document.createElement("canvas");
      JsBarcode(canvas, text, {format: "CODE39"});
      return canvas.toDataURL("image/png");
    }
  </script>


<div class="content-wrapper">
    <section class="content-header">

        <section class="content-header">
        <h1>
            Requisi&ccedil;&atilde;o AJAX
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li class="active">Requisiçao AJAX</li>
        </ol>
    </section>




 <div class="box">

  <html lang="en">
      <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">







      </head>


      <body style="background: #c9c3c3">

        <div class="container">
            <div class="table justify-content-center">




////////////JUST TO TEST IF BARCODE RENDER IN VIEW "TEST"///////

        <svg id="example2"></svg>
          <a id="hello">Click Here</a>






                <div class="col-10 mb-5">
                   <div class="card mt-3">
                       <div class="card-body">



                           <h5 class="card-title text-center">---------------------------</h5>


                              <div class="table-responsive">




    <table id="example" class="table  table-bordered table-striped">
     <thead>  
       <tr>  
        <th>#</th>  
         <th>id</th>  
         <th>obra</th>  
         <th>nome</th>  
         <th>qtd</th>  
         <th>data</th>  
         <th>Nr CJ</th>  
         <th>NR Peça</th>  
         <th>Prep</th>
         <th>Doc</th>
         <th>Tipo</th>
         <th>Desc.</th>
         <th>CB</th>
         <th>area</th>
         <th>peso</th>
         <th>Kg MA</th>
         <th>status</th>

       </tr>  
     </thead>  
     <tfoot>  

        <tr>  

       </tr>  
     </tfoot>  
     <tbody>  
       <tr>  
                     <?php if(!empty($peca)): ?>
                                <?php foreach($peca as $pecas):                                 
                                ?>
                                <tr>
                                    <th><?= $pecas->id?></th>
                                    <th><?= $pecas->id_cod_obra?></th>
                                    <th><?= $pecas->nome?></th>
                                    <th><?= $pecas->qtd?></th>
                                    <th><?= $pecas->data_entrada?></th>
                                    <th><?= $pecas->nr_subconjunto?></th>
                                    <th><?= $pecas->nr_de_peca?></th>
                                    <th><?= $pecas->preparou?></th>
                                    <th><?= $pecas->doc_anexo?></th>
                                    <th><?= $pecas->tipo_doc?></th>
                                    <th><?= $pecas->descricao?></th>

                                    <!--            <th></th> -->


                                    <!--    <th><?//=   $pecas->codigoean?></th>

                                    <td><?php// echo $pecas->codigoean;?><svg id="hello"></svg></td>


                        </th>
                                    <img src="<?php //echo base_url('Barcode/barcode_generator')?>/code128a/15/ ?> <th><?//= $pecas->codigo_barras ?> </th> ;?>/true"/>
                                    </th>


                            <td> 

                                            <img src="<?php// echo base_url('Barcode/barcode_generator')?>/code128a/15/<?php//  echo// $pecas->codigoean  ;?>/true"/>





                                                                        </td>-->



                                    <th><?= $pecas->area?></th>
                                    <th><?= $pecas->peso?></th>
                                    <th><?= $pecas->kg_solda?></th>
                                    <th><?= $pecas->status?></th>




                                </tr>
                                <?php endforeach ;?>
                            <?php endif;?>
       </tr>  
       <tr>  

     </tbody>  
   </table> 
                       </div>
                   </div>
                </div>
            </div>
        </div>


      </body>
    </html>

            </div>

        </div>



    </section>
</div>





 <script src="<?php echo site_url('assets/JsBarcode-3.11.0/dist/JsBarcode.all.js'); ?>"></script>



<script type="text/javascript" language="javascript" >




  $(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");




</script>

在1 TCPDF中,我用数字条形码打印表格。 在1中,我还可以在视图中插入带有此代码的条形码列

<img src="<?php echo base_url('Barcode/barcode_generator')?>/code128a/15/<?php  echo $pecas->codigoean  ;?>/true"/>

在2 AJAX JS中,我用数字条形码打印表格。和所有的作品excell / pdf / copy

仅用于测试的JSBarcode可以正确输出我为“ TEST”放置的条形码

3 个答案:

答案 0 :(得分:0)

您必须使用服务器端条形码生成器来处理流文件,例如生成PDF。我建议使用此php-barcode-generator

答案 1 :(得分:0)

简单地使用这个,对我有用。

$obj_pdf->Cell(0, 0, 'CODE 128 C', 0, 1);
$obj_pdf->write1DBarcode('0123456789', 'C128C', '', '', '', 18, 0.4, $style, 'N');

引用:TCPDF example

答案 2 :(得分:0)

this is how the print looks like这将允许使用条形码图像打印不是我一直在寻找的解决方案,但至少可以使用

      <link href="assets/impecran/style.css" media="screen" rel="stylesheet" type="text/css" />
      <script language="javascript">
        function Clickheretoprint()
        { 
          var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
              disp_setting+="scrollbars=yes,width=1000, height=500, left=100, top=25"; 
          var content_vlue = document.getElementById("tablecon").innerHTML; 

          var docprint=window.open("","",disp_setting); 
           docprint.document.open(); 
           docprint.document.write('</head><body onLoad="self.print()" style="width: 1000px; font-size:11px; font-family:arial; font-weight:normal;">');          
           docprint.document.write(content_vlue); 
           docprint.document.close(); 
           docprint.focus(); 
        }
        </script>

      <section class="content">
            <div class="row">
                  <div class="col-xs-12 col-sm-12 col-lg-12">
                        <div class="box box-warning">




                              <?php
                              if (isset($msg)) {
                                    echo '<div class="box-header with-border">' . $msg . '</div>';


                              }
                    //    echo anchor('Home/createPDF', 'Generate PDF Report');



                              ?>



                                  <?php
                                                      if ((isset($resultadoPeca)) && (!empty($resultadoPeca))) {
                                                      foreach ($resultadoPeca as $pecas) {  $pecas->id_cod_obra;}} //vai retornar o id_cod_obra n vezes igual por isso escolho o ultimo da lista
                                                                  ?>                                                          

                                                                  <a href="createPDF?id_cod_obra=<?php echo $pecas->id_cod_obra;  ?>" class="btn btn-default"><i  class="fa fa-plus"></i> PDF</a>
                                                          <?php        ?>
                                                            <tr>


                              <div class="box">
<!--                                    <div class="box-header with-border">
                                          <h3 class="box-title">Lista de pecas</h3>
                                    </div>-->
                                    <!-- /.box-header -->




                                    <div class="box-body">
                                          <form role="form" name="formulariopeca" id="formulariopeca" action="alterapeca" method="post" class="form-horizontal">
                                                <input name="idpeca" id="idpeca" type="hidden" value="" readonly="readonly">
                                          </form>

                                          <span id="title">Products Inventory</span><a id="addd" href="javascript:Clickheretoprint()">Print</a>
                                          <div id="tablecon">
                                       <table id="example1" class="table table-bordered table-striped">
                                                <thead>
                                                      <tr>

                                                            <th>id_cod_obra</th>
                                                            <th>nome</th>
                                                            <th>qtd</th>

                                                            <th>nr_subconjunto</th>
                                                            <th>nr_de_peca</th>


                                                            <th>descricao</th>

                                                            <th>codigoean</th>
                                                            <th>Status</th>
                                                            <th style="width:1px;">&nbsp;</th>

                                                      </tr>
                                                </thead>
                                                <tbody>

                                  <?php
                                                      if ((isset($resultadoPeca)) && (!empty($resultadoPeca))) {
                                                            foreach ($resultadoPeca as $pecas) {
                                                                  ?>                                                          



                                                                  <tr>


                                                                         <td><?php echo $pecas->id_cod_obra; ?></td>
                                                                        <td><?php echo $pecas->nome; ?></td>
                                                                        <td><?php echo $pecas->qtd; ?></td>

                                                                        <td><?php echo $pecas->nr_subconjunto; ?></td>
                                                                        <td><?php echo $pecas->nr_de_peca; ?></td>


                                                                        <td><?php echo $pecas->descricao; ?></td>

.........