duanlei8119 2016-08-25 06:08
浏览 74
已采纳

在窗口加载时使用JS打印html表

I need to print the contents of a table element to a POS receipt printer, I am doing it like this:

    <?php
    private function message(){
    return '<!DOCTYPE html><html moznomarginboxes mozdisallowselectionprint><body style="padding:30px;">'
.'<table id="print-area" style="height: 501px; border:1px solid red;padding:1em;margin:0 0 1em" width="364" ><tr><td>example</td></tr></table></table>'
.'<p>&nbsp;</p></body> </html>'}
    ?>

What I am doing is that I am passing the return of that PHP function to a new function. Please ignore the php functions structure and logic, these are just an example of how I am doing this, I know that this might not be the best way to do this, but on what I really need help is on the JavaScript part. Why is it not printing and closing the new window?.

I need to print just the table and make it fit to the POS receipt page, without printing a whole blank space.... I have already tried with this version of the PrintReceipt function but it prints a lot of white space on the receipt.

    public function PrintReceipt($content) {

    echo"<!DOCTYPE html><script type='application/javascript'>
                    window.onload=function(){          
                        var myWindow = window.open('', 'MsgWindow', 'width=200,height=100');
                        myWindow.document.write('" . $content . "');
                        myWindow.print();
                        myWindow.close();              
                    }
    </script>";
}
  • 写回答

1条回答 默认 最新

  • dongshi1207 2016-08-25 06:51
    关注

    If you aren't using frames the there is no point in declaring the following lines:

    myWindow.frames['print_frame'].document.title = document.title; 
    myWindow.document.getElementById('print-area').contentWindow.print();   
    

    Which means that your 2nd attempt in OP is correct. If there's empty space then that's absolutely a css problem. If you want the table to occupy 100% of the page width do:

    return '<table id="print-area" style="height: 100%; border:1px solid red;padding:1em;margin:0 0 1em" width="100%" ><tr><td>example</td></tr></table></table>';

    instead of

    return '<!DOCTYPE html><html moznomarginboxes mozdisallowselectionprint><body style="padding:30px;">' .'<table id="print-area" style="height: 501px; border:1px solid red;padding:1em;margin:0 0 1em" width="364" ><tr><td>example</td></tr></table></table>' .'<p>&nbsp;</p></body> </html>'}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测