打印div中生成的条形码

时间:2019-06-04 22:44:21

标签: html image codeigniter barcode

我有一个屏幕,我从数据库中加载值,然后加载条形码控制器并生成图像。我要打印生成的条形码图片。

这是条形码控制器 https://github.com/jahangiralammahi/codeigniter_barcode_master 我相信也有机会保存到.png文件和打印文件。但也失败了。 因此,我们将不胜感激

<td>
<div id="printableArea">
<img src="<?php echo base_url('Barcode/barcode_generator')?>/code128a/30/<?php echo $operarios->codigoean?>/true"/>
<!-- <img src="<?php// echo base_url('Barcode/barcode_generator')?>/Code39/40/EDT45SGHJKLE/true"/> -->  

</div>  <input type="button" onclick="printDiv('printableArea')" value="print" />
</td>
<script type="text/javascript">
function printDiv(printableArea)
    {
        Popup($(printableArea).html());
    }

    function Popup(data) 
    {
        var mywindow = window.open('', 'my div', 'height=100,width=100');
        mywindow.document.write('<img src="<?php echo base_url('Barcode/barcode_generator')?>/code128a/30/<?php echo $operarios->codigoean?>/true"/>');
     //   mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
    //    mywindow.document.write('</head><body >');
        mywindow.document.write(data);
    //    mywindow.document.write('</body></html>');

        mywindow.print();
        mywindow.close();

        return true;
    }


</script>

我可以查看最后一行条形码的打印视图,因为我猜它抓住了上次使用的ID参考。因此,我按下的任何打印按钮都将获得相同的最后一个条形码。


MY VIEW PAGE

<link rel="stylesheet" href="assets/css/bootstrap/bootstrap.min.css">
<link rel="stylesheet" href="assets/datatables/dataTables.bootstrap.css">

<div class="content-wrapper">
      <section class="content-header">
            <h1>Lista Operários</h1>
            <ol class="breadcrumb">
                  <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                  <li>Operários</li>
                  <li class="active">Lista operários</li>
            </ol>


      </section>

      <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>';
                              }
                              ?>
                              <div class="box">
<!--                                    <div class="box-header with-border">
                                          <h3 class="box-title">Lista de operarios</h3>
                                    </div>-->
                                    <!-- /.box-header -->
                                    <div class="box-body">
                                          <form role="form" name="formulariooperario" id="formulariooperario" action="alteraoperario" method="post" class="form-horizontal">
                                                <input name="idoperario" id="idoperario" type="hidden" value="" readonly="readonly">
                                          </form>
                                          <table id="example1" class="table table-bordered table-striped">
                                                <thead>
                                                      <tr>
                                                             <th style="width:24px">    </th>
                                                            <th>id</th>
                                                            <th>nome</th>
                                                            <th>Profissão</th>

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

                                                      </tr>
                                                </thead>
                                                <tbody>
                                                      <?php
                                                      if ((isset($resultadoOperario)) && (!empty($resultadoOperario))) {
                                                            foreach ($resultadoOperario as $operarios) {
                                                                  ?>
                                                                  <tr>
                                                                        <td>

                                                                        <?php  if($edit_oper ==='1' ) {  ?>
                                                                        <a href="alteraoperario?id=<?php echo $operarios->id; ?>"><i class="fa fa-pencil-square-o"></i> </a> 
                                                                        <?php       }  ;?>
                                                                        <?php  if($del_oper ==='1' ) {  ?>
                                                                        <a href="<?php echo base_url().'home/deleteoperario/'.$operarios->id; ?>" class="btn btn-danger btn-xs"><span class="fa fa-trash" title="!!!Elimina todas as informações do colaborador"></a>
                                                                        <?php       }  ;?>

                                                                        <a href="alteraoperario2?id=<?php echo $operarios->id;  ?>" class="btn btn-default"><i  class="fa fa-plus"></i> Documentação operários</a>








                                                                        </td>
                                                                         <td><?php echo $operarios->id; ?></td>
                                                                        <td><?php echo $operarios->nomefantasia; ?></td>
                                                                        <td>


                                                                                                                                                    <?php
                                                                              if ((int)$operarios->profissao === 1) {
                                                                                    echo "soldador";                                                                                

                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 2) {                                                                                   
                                                                                          echo "tubista";                                                                               
                                                                              } else {
                                                                                     if ((int)$operarios->profissao === 3) {                                                                                   
                                                                                          echo "serralheiro";
                                                                             } else {
                                                                                     if ((int)$operarios->profissao === 4) {
                                                                                          echo "pintor";
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 5) {
                                                                                          echo "motorista"; 
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 6) {
                                                                                          echo "operador";  
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 7) {
                                                                                          echo "encarregado";   
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 8) {
                                                                                          echo "eletromecânico";    
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 9) {
                                                                                          echo "armador";   
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 10) {
                                                                                          echo "indiferenciado";    
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 11) {
                                                                                          echo "outro"; 
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 12) {
                                                                                          echo "resp armazém";  
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 13) {
                                                                                          echo "ferramenteiro"; 
                                                                            } else {
                                                                                     if ((int)$operarios->profissao === 14) {
                                                                                          echo "ajudante";  


                                                                                   } else {
                                                                                          echo '<i class="fa fa-circle 2x" style="color: red;">';
                                                                                    }
                                                                            }}}}}}}}}}}}}
                                                                              ?>










                                                                        </td>

                                                                        <td><?php echo $operarios->email; ?></td>
                                                                        <td>
                                                                        <div id="printableArea">

                                                                        <img src="<?php echo base_url('Barcode/barcode_generator')?>/code128a/30/<?php echo $operarios->codigoean?>/true"/>
                                                                    <!--        <img src="<?php// echo base_url('Barcode/barcode_generator')?>/Code39/40/EDT45SGHJKLE/true"/> -->   

                                                                        </div>
                                                                        <input type="button" onclick="printDiv('printableArea')" value="print" />


                                                                        </td>



                                                                             <td> <?php

                                                                             if ((int)$operarios->status === 1) {
                                                                                    echo '<i class="fa fa-circle 2x" style="color: green;">';
                                                                              } else {
                                                                                     if ((int)$operarios->status === 2) {
                                                                                          echo '<i class="fa fa-circle 2x" style="color: red;">';
                                                                              } else {
                                                                                        if ((int)$operarios->status === 3) {
                                                                                          echo '<i class="fa fa-circle 2x" style="color: yellow;">';
                                                                              } else {
                                                                                        if ((int)$operarios->status === 4) {
                                                                                          echo '<i class="fa fa-circle 2x" style="color: blue;">';
                                                                              } else {
                                                                                  echo '<i class="fa fa-circle 2x" style="color: black;">';
                                                                                    }
                                                                            }}}
                                                                              ?>
                                                                        </td>
                                                                  </tr>
                                                                  <?php
                                                            }
                                                      }
                                                      ?>

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

<script src="assets/js/jquery/jquery-2.2.3.min.js"></script>


<script src="assets/js/bootstrap/bootstrap.min.js"></script>
<script src="assets/datatables/jquery.dataTables.min.js"></script>
<script src="assets/datatables/dataTables.bootstrap.min.js"></script>

<!-- page script -->
<script>
      var base_url = '<?php echo base_url() ?>';
      $(document).ready(function () {

      });
      $(function () {
            $('#example1').DataTable({
                  "paging": true,
                  "lengthChange": false,
                  "searching": false,
                  "ordering": true,
                  "info": true,
                  "autoWidth": false
            });
      });
      function consultapeca(id){
            $('#idoperario').val(id);
            $('#formulariooperario').submit();
      }
</script>

<script type="text/javascript">


  function printDiv(printableArea)
    {
        Popup($(printableArea).html());
    }

    function Popup(data) 
    {
        var mywindow = window.open('', 'my div', 'height=100,width=100');
        mywindow.document.write('<img src="<?php echo base_url('Barcode/barcode_generator')?>/code128a/30/<?php echo $operarios->codigoean?>/true"/>');
     //   mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
    //    mywindow.document.write('</head><body >');
        mywindow.document.write(data);
    //    mywindow.document.write('</body></html>');

        mywindow.print();
        mywindow.close();

        return true;
    }


</script>


MY controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Barcode extends CI_Controller {

    public function barcode_print(){
        $this->load->view('print_page');
    }

    //Barcode Generator
    public function barcode_generator($code_type,$size,$text,$print){

        $filepath = "";
        $orientation= "horizontal";
        $sizefactor = "1";

        // This function call can be copied into your project and can be made from anywhere in your code
        $this->barcode_create( $filepath, $text, $size, $orientation, $code_type, $print, $sizefactor );
    }

    //Barcode create
    public function barcode_create( $filepath="", $text="0", $size="20", $orientation="horizontal", $code_type="code128", $print=false, $SizeFactor=1 ) {
        $code_string = "";
        // Translate the $text into barcode the correct $code_type
        if ( in_array(strtolower($code_type), array("code128", "code128b")) ) {
            $chksum = 104;
            // Must not change order of array elements as the checksum depends on the array's key to validate final code
            $code_array = array(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","@"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","\`"=>"111422","a"=>"121124","b"=>"121421","c"=>"141122","d"=>"141221","e"=>"112214","f"=>"112412","g"=>"122114","h"=>"122411","i"=>"142112","j"=>"142211","k"=>"241211","l"=>"221114","m"=>"413111","n"=>"241112","o"=>"134111","p"=>"111242","q"=>"121142","r"=>"121241","s"=>"114212","t"=>"124112","u"=>"124211","v"=>"411212","w"=>"421112","x"=>"421211","y"=>"212141","z"=>"214121","{"=>"412121","|"=>"111143","}"=>"111341","~"=>"131141","DEL"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","FNC 4"=>"114131","CODE A"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
            $code_keys = array_keys($code_array);
            $code_values = array_flip($code_keys);
            for ( $X = 1; $X <= strlen($text); $X++ ) {
                $activeKey = substr( $text, ($X-1), 1);
                $code_string .= $code_array[$activeKey];
                $chksum=($chksum + ($code_values[$activeKey] * $X));
            }
            $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];

            $code_string = "211214" . $code_string . "2331112";
        } elseif ( strtolower($code_type) == "code128a" ) {
            $chksum = 103;
            $text = strtoupper($text); // Code 128A doesn't support lower case
            // Must not change order of array elements as the checksum depends on the array's key to validate final code
            $code_array = array(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","@"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","NUL"=>"111422","SOH"=>"121124","STX"=>"121421","ETX"=>"141122","EOT"=>"141221","ENQ"=>"112214","ACK"=>"112412","BEL"=>"122114","BS"=>"122411","HT"=>"142112","LF"=>"142211","VT"=>"241211","FF"=>"221114","CR"=>"413111","SO"=>"241112","SI"=>"134111","DLE"=>"111242","DC1"=>"121142","DC2"=>"121241","DC3"=>"114212","DC4"=>"124112","NAK"=>"124211","SYN"=>"411212","ETB"=>"421112","CAN"=>"421211","EM"=>"212141","SUB"=>"214121","ESC"=>"412121","FS"=>"111143","GS"=>"111341","RS"=>"131141","US"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","CODE B"=>"114131","FNC 4"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
            $code_keys = array_keys($code_array);
            $code_values = array_flip($code_keys);
            for ( $X = 1; $X <= strlen($text); $X++ ) {
                $activeKey = substr( $text, ($X-1), 1);
                $code_string .= $code_array[$activeKey];
                $chksum=($chksum + ($code_values[$activeKey] * $X));
            }
            $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];

            $code_string = "211412" . $code_string . "2331112";
        } elseif ( strtolower($code_type) == "code39" ) {
            $code_array = array("0"=>"111221211","1"=>"211211112","2"=>"112211112","3"=>"212211111","4"=>"111221112","5"=>"211221111","6"=>"112221111","7"=>"111211212","8"=>"211211211","9"=>"112211211","A"=>"211112112","B"=>"112112112","C"=>"212112111","D"=>"111122112","E"=>"211122111","F"=>"112122111","G"=>"111112212","H"=>"211112211","I"=>"112112211","J"=>"111122211","K"=>"211111122","L"=>"112111122","M"=>"212111121","N"=>"111121122","O"=>"211121121","P"=>"112121121","Q"=>"111111222","R"=>"211111221","S"=>"112111221","T"=>"111121221","U"=>"221111112","V"=>"122111112","W"=>"222111111","X"=>"121121112","Y"=>"221121111","Z"=>"122121111","-"=>"121111212","."=>"221111211"," "=>"122111211","$"=>"121212111","/"=>"121211121","+"=>"121112121","%"=>"111212121","*"=>"121121211");

            // Convert to uppercase
            $upper_text = strtoupper($text);

            for ( $X = 1; $X<=strlen($upper_text); $X++ ) {
                $code_string .= $code_array[substr( $upper_text, ($X-1), 1)] . "1";
            }

            $code_string = "1211212111" . $code_string . "121121211";
        } elseif ( strtolower($code_type) == "code25" ) {
            $code_array1 = array("1","2","3","4","5","6","7","8","9","0");
            $code_array2 = array("3-1-1-1-3","1-3-1-1-3","3-3-1-1-1","1-1-3-1-3","3-1-3-1-1","1-3-3-1-1","1-1-1-3-3","3-1-1-3-1","1-3-1-3-1","1-1-3-3-1");

            for ( $X = 1; $X <= strlen($text); $X++ ) {
                for ( $Y = 0; $Y < count($code_array1); $Y++ ) {
                    if ( substr($text, ($X-1), 1) == $code_array1[$Y] )
                        $temp[$X] = $code_array2[$Y];
                }
            }

            for ( $X=1; $X<=strlen($text); $X+=2 ) {
                if ( isset($temp[$X]) && isset($temp[($X + 1)]) ) {
                    $temp1 = explode( "-", $temp[$X] );
                    $temp2 = explode( "-", $temp[($X + 1)] );
                    for ( $Y = 0; $Y < count($temp1); $Y++ )
                        $code_string .= $temp1[$Y] . $temp2[$Y];
                }
            }

            $code_string = "1111" . $code_string . "311";
        } elseif ( strtolower($code_type) == "codabar" ) {
            $code_array1 = array("1","2","3","4","5","6","7","8","9","0","-","$",":","/",".","+","A","B","C","D");
            $code_array2 = array("1111221","1112112","2211111","1121121","2111121","1211112","1211211","1221111","2112111","1111122","1112211","1122111","2111212","2121112","2121211","1121212","1122121","1212112","1112122","1112221");

            // Convert to uppercase
            $upper_text = strtoupper($text);

            for ( $X = 1; $X<=strlen($upper_text); $X++ ) {
                for ( $Y = 0; $Y<count($code_array1); $Y++ ) {
                    if ( substr($upper_text, ($X-1), 1) == $code_array1[$Y] )
                        $code_string .= $code_array2[$Y] . "1";
                }
            }
            $code_string = "11221211" . $code_string . "1122121";
        }

        // Pad the edges of the barcode
        $code_length = 20;
        if ($print) {
            $text_height = 30;
        } else {
            $text_height = 0;
        }

        for ( $i=1; $i <= strlen($code_string); $i++ ){
            $code_length = $code_length + (integer)(substr($code_string,($i-1),1));
            }

        if ( strtolower($orientation) == "horizontal" ) {
            $img_width = $code_length*$SizeFactor;
            $img_height = $size;
        } else {
            $img_width = $size;
            $img_height = $code_length*$SizeFactor;
        }

        $image = imagecreate($img_width, $img_height + $text_height);
        $black = imagecolorallocate ($image, 0, 0, 0);
        $white = imagecolorallocate ($image, 255, 255, 255);

        imagefill( $image, 0, 0, $white );
        if ( $print ) {
            imagestring($image, 5, 31, $img_height, $text, $black );
        }

        $location = 10;
        for ( $position = 1 ; $position <= strlen($code_string); $position++ ) {
            $cur_size = $location + ( substr($code_string, ($position-1), 1) );
            if ( strtolower($orientation) == "horizontal" )
                imagefilledrectangle( $image, $location*$SizeFactor, 0, $cur_size*$SizeFactor, $img_height, ($position % 2 == 0 ? $white : $black) );
            else
                imagefilledrectangle( $image, 0, $location*$SizeFactor, $img_width, $cur_size*$SizeFactor, ($position % 2 == 0 ? $white : $black) );
            $location = $cur_size;
        }

        // Draw barcode to the screen or save in a file
        if ( $filepath=="" ) {
            header ('Content-type: image/png');
            imagepng($image);
            imagedestroy($image);
        } else {
            imagepng($image,$filepath);
            imagedestroy($image);       
        }
    }
}

    ```

2 个答案:

答案 0 :(得分:0)

<script>
function ImagetoPrint(source) {
    return "<html><head><script>function step1(){\n" +
            "setTimeout('step2()', 10);}\n" +
            "function step2(){window.print();window.close()}\n" +
            "</scri" + "pt></head><body onload='step1()'>\n" +
            "<img src='" + source + "' /></body></html>";
}
function PrintImage(source) {
    Pagelink = "about:blank";
    var pwa = window.open(Pagelink, "_new");
    pwa.document.open();
    pwa.document.write(ImagetoPrint(source));
    pwa.document.close();
}
    </script>
<td>    
<img  src="<?php echo base_url('Barcode/barcode_generator')?>/code128a/30/<?php echo $operarios->codigoean?>/true"/>

<a href="#" onclick="PrintImage('<?php echo base_url('Barcode/barcode_generator')?>/code128a/30/<?php echo $operarios->codigoean?>/true'); return false;"><button id="printImage">Print this Image</button></a>
</td>

答案 1 :(得分:0)