最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to take a print out of a html table using PHP - Stack Overflow

programmeradmin1浏览0评论

I have a table in a PHP page. It is a invoice. So I want to print it after adding items to it, and I had already done the coding part and all the things are working properly.

Now I just want to print that invoice table to a paper by pressing a button. I searched on Google but no clue is found to do that.

Can any one help me?

This is my table on right hand side. It is populated by the form in left hand side so I just want to print that right hand side table:

I have a table in a PHP page. It is a invoice. So I want to print it after adding items to it, and I had already done the coding part and all the things are working properly.

Now I just want to print that invoice table to a paper by pressing a button. I searched on Google but no clue is found to do that.

Can any one help me?

This is my table on right hand side. It is populated by the form in left hand side so I just want to print that right hand side table:

Share Improve this question edited Jul 11, 2018 at 9:44 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 22, 2012 at 4:40 YasithaYasitha 9112 gold badges17 silver badges43 bronze badges 3
  • print where? user computer? server printer? – user557846 Commented Jul 22, 2012 at 4:46
  • 1 There is nothing to do on server side so no need of php. You can do with JS. if you need more details in JS, add JS in tags. – Riz Commented Jul 22, 2012 at 4:50
  • @Dagon-yep i want to print in user computer. – Yasitha Commented Jul 22, 2012 at 5:00
Add a comment  | 

4 Answers 4

Reset to default 6

Try this one:

Add an attribute id to your table your_content

Place an anchor tag :

   <a href="javascript:void(0);" id="printPage">Print</a> 

And add script:

This script will initially show print preview of the content that is inside your table.

 <script lang='javascript'>
 $(document).ready(function(){
  $('#printPage').click(function(){
        var data = '<input type="button" value="Print this page" onClick="window.print()">';           
        data += '<div id="div_print">';
        data += $('#your_content').html();
        data += '</div>';

        myWindow=window.open('','','width=200,height=100');
        myWindow.innerWidth = screen.width;
        myWindow.innerHeight = screen.height;
        myWindow.screenX = 0;
        myWindow.screenY = 0;
        myWindow.document.write(data);
        myWindow.focus();
    });
 });

Copy the script part and put it on the top of your page. And put the print link, [above anchor tag] wherever you want. And make sure you have included the jquery script file.

This would be too trivial of a situation for something like jQuery so you should use plain Javascript to do it. Place this somewhere on your page and see if it works:

<a href="javascript:void(0);" onclick="printPage();">Print</a> 

<script type="text/javascript">
 function printPage(){
        var tableData = '<table border="1">'+document.getElementsByTagName('table')[0].innerHTML+'</table>';
        var data = '<button onclick="window.print()">Print this page</button>'+tableData;       
        myWindow=window.open('','','width=800,height=600');
        myWindow.innerWidth = screen.width;
        myWindow.innerHeight = screen.height;
        myWindow.screenX = 0;
        myWindow.screenY = 0;
        myWindow.document.write(data);
        myWindow.focus();
    };
 </script>​​​​​​

You can see the demo here: http://jsfiddle.net/Pxqtb/

i used this code and it solved my problem. thank you all for helping me.....

function printlayout() {
     var data = '<input type="button" value="Print this page" onClick="window.print()">';           
       var DocumentContainer = document.getElementById('printlayout');
        //data += $('printlayoutable').html();
        //data += '</div>';
var WindowObject = window.open('', "TrackHistoryData", 
                              "width=740,height=325,top=200,left=250,toolbars=no,scrollbars=yes,status=no,resizable=no");           
 WindowObject.document.writeln(DocumentContainer.innerHTML);
  //WindowObject.document.writeln(DocumentContainer.innerHTML);
        WindowObject.document.close();
        WindowObject.focus();
        WindowObject.print();
        WindowObject.close();

You have a few options. (1) If you create a new HTML page with just the invoice, the user can print that page using the browser's print capacity. (2) Alternatively, you can allow the user to download an invoice document which you create and print that. (3) You can use the JavaScript command window.print(); in conjunction with #1. Have a look at this.

发布评论

评论列表(0)

  1. 暂无评论