如何使用qz print打印带有html标签的php页面?

时间:2016-04-21 04:17:05

标签: php html printing

我有一个带有html标签的php页面

<?php
$arr_bill_rows =  count($sale_item);
?>

<html lang="en">

        <head>
            <title>Invoice Print</title>
            <meta charset="UTF-8">
            <style type="text/css">
 body   {font-family:Arial,sans-serif; margin:0px; padding:0px ; font-size:12px;} 
                 address { width: 200px; height:auto; float:right; font-size:12px; font-style:normal; margin:15px 0 0 0; text-align:right;}
                .page   { width:320px; background-color:#FFF; margin: 0px auto; overflow:hidden; padding:5px 10px; display: none; height: <?php echo (10 + ($arr_bill_rows * 0.5)); ?>cm; }  
                .header { width:100%; height:80px; float:left;}
                .header h1, .header p { margin:0; font-size: 14px; }
        .header h2 { margin:0px 0 15px; font-size: 18px}
                 .header h1 { margin:0; font-size: 20px;   text-align:center}   
                .text-left { text-align:left;}
                .text-right { text-align:right;}
                .text-center { text-align:center}

                .logo   { /* width:100px; height: 60px; */ float:left  }  
                .logo img   { width:100%; } 
                .heading    { width:100%; height:18px; float:left;  font-size:12px; font-weight:bold; text-align:center; margin:5px 0 5px 0; line-height:18px;}
                .name       { width:100%; height:19px; float:left; border-bottom:1px #ddd dashed; font-size:12px; font-weight:bold; text-align:center;  }
                .footer     { width:100%; height:auto; float:left; border-top:1px #ddd dashed; text-align:right; margin:5px 0 0 0; line-height:25px; position:relative; bottom:0;}
                .inv-row    { width:100%; height:15px; float:left; font-size:12px; }
                .inv-box    { width:75px; height:15px; float:left;margin:5px 0 0 0;  }
                .content-box-out{ width:100%; float:left; }
                .content-box{ width:100%;  float:left;margin:25px 0 0 0;  }
                .content-box td{ height:23px;}
                .total { border-bottom:1px solid #999;}

                .content-box td { font-size:14px;}

                @media print {@page { margin: 0; padding: 0; visibility: hidden;  }.page{ display: block; } .page-break { display: block; page-break-before: always;}}            
                @media screen{
                     body { background: #fff; margin:0px; padding:0px ;  } 
                     .page:nth-child(1){ display: block; margin: 0 auto 0 auto; border:1px #ddd solid; -webkit-box-shadow: 2px 5px 5px 1px rgba(0, 0, 0, .2); box-shadow: 2px 5px 5px 1px rgba(0, 0, 0, .2); } 
                } 


            </style>
<!--        <script type="text/javascript" src="assets/js/jquery-1.8.3.min.js"></script>-->
            <script type="text/javascript" charset="UTF-8" src="<?php echo $this->get_base_url(); ?>assets/lib/jquery/jquery-1.7.1.min.js"></script>
            <script type="text/javascript" charset="UTF-8" src="<?php echo $this->get_base_url(); ?>assets/lib/jquery/jquery-barcode.js"></script>


        </head>
        <body onLoad="">

            <!--Page -->
            <div class="page" id="invoice-print">

                <div class="header">
                    <h1 class="text-center"><?php echo INVOICE_HEAD ; ?></h1>
                    <p class="text-right"><?php echo INVOICE_ADDRESS1 ; ?> </p>
                    <p class="text-right"><?php echo INVOICE_ADDRESS2; ?></p>
                    <h2 class="text-center"><?php echo INVOICE_HEAD2 ; ?></h2>
                    <br/><br/>
        </div>
<!--                <div class="heading">INVOICE</div>-->


                <div class="name"><?php echo strtoupper($customer_name); ?></div>

                <div class="inv-row">
                    <div class="inv-box"> <?php echo $sale_invoice_no; ?></div>
                    <div class="inv-box" style=" width:180px; float:right">Date : <?php echo $inovice_date; ?>
                    </div>
                </div>

                <div class="content-box-out" style="position: relative; min-height: 70%">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="content-box">
                        <tr>
                            <td style="border-bottom:1px dashed #e3e3e3;"  width="5%">#</td>
                            <td style="border-bottom:1px dashed #e3e3e3;"  width="50%">Items </td>
                            <td style="border-bottom:1px dashed #e3e3e3;"  width="5%"  align="center">Qty</td>
                            <td style="border-bottom:1px dashed #e3e3e3;"  width="20%"  align="right">Rate</td>
                            <td style="border-bottom:1px dashed #e3e3e3;"  width="20%"  align="right">Total</td>
                        </tr>

                        <?php
                        $i = 1;$grosstotal = 0;
                       foreach ($sale_item as $row):
                        ?>

                            <tr>
                                <td width="3%"><?php echo $i; ?></td>
                                <td><?php echo $row->product_name; ?> </td>
                                <td width="5%" class="pull-left" align="left"><?php echo $row->sale_item_qty; ?></td>
                                <td class="pull-right" align="right"><?php echo round($row->sale_item_amount,2); ?></td>
                                <td class="pull-right" align="right"><?php echo (round($row->sale_item_qty*$row->sale_item_amount,2)); ?></td>
                            </tr>

                        <?php
                        //$grosstotal += round(($row->sale_item_qty*$row->sale_item_amount),2);
                        $i ++;
                        endforeach;

                        ?>                       

                    </table>
                    <div class="footer" > 
                       <?php if($sale_service_charge>0 || $sale_discount_total>0 || $sale_other_amount>0) :?>
                       <div class="pull-right">Net Total : <?php echo number_format($sale_invoice_total, 2);?></div> 
                       <?php endif;?>
                       <?php if($sale_service_charge>0) :?>
                       <div class="pull-right">Service Charge :<?php echo number_format($sale_service_charge,2);?></div>
                        <?php endif; if($sale_discount_total>0) :?>
                        <div class="pull-right">Discount : <?php echo number_format($sale_discount_total,2);?></div>
                        <?php endif; if($sale_other_amount>0) :?>
                        <div class="pull-right">Extra Charge : <?php echo number_format($sale_other_amount,2);?></div>
                          <?php endif;?>
                       <div class="pull-right total">Total :<?php echo number_format($sale_invoice_total_rounding, 2);?></div>
                    </div>


                </div>


            </div>  
            <!--Page End -->   
            <div class="page-break"></div>

        </body>
    </html>

我必须使用qz打印来打印,如何做到这一点?现在我正在使用html打印。

Onclick打印按钮

doprint_html('sales/print_invoice/" . $aRow->sale_id . "/duplicate-copy/')

调用并执行html打印,但我要使用qz

改变

print_qz_invoice('sales/print_invoice/" . $aRow->sale_id . "/duplicate-copy/')

qz建立了联系。

 function print_qz_invoice(url){ 
   if($('#printer').val()!=''&&$('#printer_type').val()==1 ){ 
      var printer = $('#printer').val();  
                                    printer = printer.replace(/%20/g, " ");  
                                    findPrinter(printer);  

                                       window['qzDoneFinding'] = function() {  
                                        doprint_qz_sale21(url+'/'+$('#printer').val()+'/'+$('#printer_type').val()+'/');  //,printer

                                       };
   }else{ 
        doprint_html(url);
   }
        }



  function doprint_qz_sale21(url){ 

        var base_url  = $("meta[name=baseurl]").attr("content");
        $.ajax(
        {
            url: base_url + url,
            type: "get",
            dataType: "json",
            error: function(){
               alert("Unable process print please try again");
            },              
            beforeSend: function(){
              $("#print-page").html("");
            },                  
            success: function(json){ 
               // printEPL_barcode(json,show_price);
                dos_print_qzs(json);
            }
        });
    }

通过ajax调用我接受所有变量,并在qz.js

function dos_print_qzs(data) { 

    if (notReady()) { return; }
      $(json).each(function(index,data) { 
    qz.appendHTML(data.replace("<strong>","\x1B\x21\x38").replace("</strong>","\x1B\x21\x08")

    );
      });
    qz.printHTML();
}

但这不起作用,这是正确的方法吗?

1 个答案:

答案 0 :(得分:2)

在最终的appendHTML语句中,您似乎混合了HTML和原始(ESC / POS)命令。你需要选择其中一个。这里将详细介绍:https://stackoverflow.com/a/38965573/3196753

此外,QZ Tray 1.9中的HTML渲染器非常有限,无法处理大部分内容。此后,QZ Tray 2.0及更高版本已大大改善。

var config = qz.configs.create("Printer Name");
var data = [{
  type: 'html',
  format: 'plain', // or 'file' if using a URL
  data: '<html><body><h1>Hello world</h1></body></html>'
}];
qz.print(config, data).catch(function(e) { console.error(e); });