如何将网页干净地打印到通用/文本打印机?

时间:2013-03-05 12:05:16

标签: php web-applications printing

我有一个生成小收据格式的Web应用程序。
当打印到通用/ txt打印机时。没有任何内容打印到纸张上

以下是印刷品的样本.. 的 删除

请问有什么方法可以将此网页打印到通用/ txt打印机

这是html

        <style>
    body{
        font-family:"Courier New", Courier, monospace;  
        font-size:13px;
    }
    .f1{
        text-transform:uppercase;
        display:block;
        text-align:center;
    }

    .f2{
        text-transform:uppercase;
        display:block;
        margin-left:10px;
    }

    .border_bottom{
        border-bottom:2px dashed #000;
    }
    .border_top{
        border-top:2px dashed #000;
    }

    .body_table{
        border:1px dashed #CCCCCC;
        padding:.5em;
    }
    </style>
    </head>

    <body onload="window.print();">
    <table width="300" cellpadding="0" cellspacing="0" class="body_table">
        <tr>
            <td><span class="f1">Official Receipt</span></td>
        </tr>
        <tr>
            <td><span class="f1">DreamWorks Pharmaceutical Industries Limited GH.</span></td>
        </tr>
        <tr>
            <td><span class="f1">Plot 14 Blk 8A</span></td>
        </tr> 
    <tr>
            <td class="border_bottom"><span class="f1">0241093621</span></td>
        </tr>   


    <tr>
            <td class=""><span class="f2">RECEIPT NO.: 0241093621</span></td>
        </tr>     

    <tr>
            <td class=""><span class="f2">VAT REG #.: </span></td>
        </tr>

    <tr>
            <td class=""><span class="f2">cashier.: </span></td>
        </tr> 

    <tr>
            <td class=""><span class="f2">date.:  time: </span></td>
        </tr>

    <tr>
            <td class="border_bottom"><span class="f2">customer.:  time: </span></td>
        </tr>

    <tr>
        <td>
            <table width="98%" align="center" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="5%"><span class="f2">QTY</span></td>
                    <td width="60%"><span class="f2">item</span></td>
                    <td width="15"><span class="f2">price</span></td>
                    <td width="15"><span class="f2">amt.</span></td>                
                </tr> 


                        <tr>
                        <td class="f2">52</td>
                        <td>MALAREX TABS</td>
                        <td>2.6</td>
                        <td>135.20</td>
                        </tr>
                                </table>
        </td>
    </tr> 
    <tr>
        <td class="border_top border_bottom">
        <table width="100%" cellpadding="0" cellspacing="0" align="left" id="print_table7"> 
                    <tr>
                        <Td class="ash_backg" width="53%"><span>Subtotal</span></Td>
                        <td width="47%" align="right">135.20</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>Discount</span></Td>
                        <td align="right">0.00</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>VAT</span></Td>
                        <td align="right">0.00</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>Total Due</span></Td>
                        <td align="right">135.20</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>Paid</span></Td>
                        <td align="right">135.20</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>Change</span></Td>
                        <td align="right">0.00</td>
                    </tr>                                                
                 </table>
        </td>
    </tr>  

    <tr>
        <td>Thank You dear customer</td>
    </tr>

    <tr>
        <td>VAT Inclusive Where Applicable</td>
    </tr>                           
    </table>
    </body>

这是收据格式HTML

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/plain; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
    @media screen{
        body{
            font-family:"Courier New", Courier, monospace;  
            font-size:13px;
        }
        .f1{
            text-transform:uppercase;
            display:block;
            text-align:center;
        }

        .f2{
            text-transform:uppercase;
            display:block;
            margin-left:10px;
        }

        .border_bottom{
            border-bottom:2px dashed #000;
        }
        .border_top{
            border-top:2px dashed #000;
        }

        .body_table{
            border:1px dashed #CCCCCC;
            padding:.5em;
        }   
    }

    @media print{
        body{
            font-family:"Courier New", Courier, monospace;  
            font-size:8px;
            width:250px;
        }
    }
    </style>
    </head>

    <body onload="window.print();">
    <table width="300" cellpadding="0" cellspacing="0" class="body_table">
        <tr>
            <td><span class="f1">Official Receipt</span></td>
        </tr>
        <tr>
            <td><span class="f1">DreamWorks Pharmaceutical Industries Limited GH.</span></td>
        </tr>
        <tr>
            <td><span class="f1">Plot 14 Blk 8A</span></td>
        </tr> 
    <tr>
            <td class="border_bottom"><span class="f1">0241093621</span></td>
        </tr>   


    <tr>
            <td class=""><span class="f2">RECEIPT NO.: 0241093621</span></td>
        </tr>     

    <tr>
            <td class=""><span class="f2">VAT REG #.: </span></td>
        </tr>

    <tr>
            <td class=""><span class="f2">cashier.: </span></td>
        </tr> 

    <tr>
            <td class=""><span class="f2">date.:  time: </span></td>
        </tr>

    <tr>
            <td class="border_bottom"><span class="f2">customer.:  time: </span></td>
        </tr>

    <tr>
        <td>
            <table width="98%" align="center" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="5%"><span class="f2">QTY</span></td>
                    <td width="60%"><span class="f2">item</span></td>
                    <td width="15"><span class="f2">price</span></td>
                    <td width="15"><span class="f2">amt.</span></td>                
                </tr> 


                        <tr>
                        <td class="f2">6</td>
                        <td>MALAREX TABS</td>
                        <td>0</td>
                        <td>0</td>
                        </tr>

                        <tr>
                        <td class="f2">1</td>
                        <td>PARAFEN PLUS TAB.</td>
                        <td>0</td>
                        <td>0</td>
                        </tr>

                        <tr>
                        <td class="f2">5</td>
                        <td>ASTHAX INHALER</td>
                        <td>5</td>
                        <td>25</td>
                        </tr>
                                </table>
        </td>
    </tr> 
    <tr>
        <td class="border_top border_bottom">
        <table width="100%" cellpadding="0" cellspacing="0" align="left" id="print_table7"> 
                    <tr>
                        <Td class="ash_backg" width="53%"><span>Subtotal</span></Td>
                        <td width="47%" align="right">25.00</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>Discount</span></Td>
                        <td align="right">0.00</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>VAT</span></Td>
                        <td align="right">0.00</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>Total Due</span></Td>
                        <td align="right">25.00</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>Paid</span></Td>
                        <td align="right">25.00</td>
                    </tr>
                    <tr>
                        <Td class="ash_backg"><span>Change</span></Td>
                        <td align="right">0.00</td>
                    </tr>                                                
                 </table>
        </td>
    </tr>  

    <tr>
        <td>Thank You dear customer</td>
    </tr>

    <tr>
        <td>VAT Inclusive Where Applicable</td>
    </tr>                           
    </table>
    </body>
    </html>

2 个答案:

答案 0 :(得分:1)

您可以使用CSS媒体类型。使用一个用于屏幕使其看起来很漂亮,一个用于打印,以使其清晰。

例如:

@media screen
  {
  p.test {font-family:verdana,sans-serif;font-size:14px;}
  }
@media print
  {
  p.test {font-family:times,serif;font-size:10px;}
  }

答案 1 :(得分:1)

是的,您可以按上述方式打印文档,并且必须使用单独的样式表进行屏幕显示和打印。 CSS规则是:

@media screen
  {
  //Css
  }

@media print{
    @page { size:250px 375px; margin: 4px }
    body{font-family:"Courier New", Courier, monospace;  font-size:8px;}
    .f1{text-transform:uppercase; display:block; text-align:center;}
    .f2{text-transform:uppercase; display:block; margin-left:10px;}
    .border_bottom{ border-bottom:2px dashed #000;}
    .border_top{border-top:2px dashed #000;}
    .body_table{border:1px dashed #CCCCCC; padding:.5em;}
}

在这些内容中,您可以描述页面大小,颜色以及更多内容,只需谷歌CSS打印规范,您就会知道更多。

但是,我认为您使用POS打印机打印它所涉及的另一个问题。为此,我想建议的方式是:

两种简单的方法,取决于您愿意在界面中接受多少干扰。

首先,您可以使用JavaScript来触发您的收据页面/ iframe进行打印。这个选项有点笨拙,因为它要求收银员在系统对话中确认打印。

但其次,您始终可以选择在网络上共享每个电台的打印机。您的服务器端组件可以直接打印到它。如果它是一个* nix服务器,一旦每个工作站的打印机添加到服务器/与服务器共享,它就很容易做到。在PHP中(安装了CUP,我想):

// attempt to pipe to 'lp -d printername'
if (`echo "{$text}" | lp -d {$printer_name}`) {
    // print successful
    return true;
} else {
    // print failed
    return false;
}

对于第二种方法,如果您正在运行Windows服务器,则可能要复杂得多。