Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a print button to a web page

Tags:

css

php

On one of my pages in my website(coded in php), i'm trying to add 2 (even more) print buttons each one printing a portion of the webpage. For example, on a page there is a table with some values and 2 graphs underneath it. I would like to add 2 print buttons, one button printing the table and the other one printing both the graphs. I've found this Example but could not understand clearly. Any help or examples would help me.

Thanks in advance.

like image 256
Anil Avatar asked Jul 24 '12 15:07

Anil


Video Answer


2 Answers

This is html/javascript code that will launch the browser's Print dialog when clicked.

<button onClick="window.print()">Print this page</button>
like image 161
Ed Manet Avatar answered Sep 29 '22 20:09

Ed Manet


If your table is inside a div with id='printTable' use:

<a href="#null" onclick="printContent('printTable')">Click to print table</a>

EDIT: Here is the function "printContent()"

<script type="text/javascript">
<!--
function printContent(id){
str=document.getElementById(id).innerHTML
newwin=window.open('','printwin','left=100,top=100,width=400,height=400')
newwin.document.write('<HTML>\n<HEAD>\n')
newwin.document.write('<TITLE>Print Page</TITLE>\n')
newwin.document.write('<script>\n')
newwin.document.write('function chkstate(){\n')
newwin.document.write('if(document.readyState=="complete"){\n')
newwin.document.write('window.close()\n')
newwin.document.write('}\n')
newwin.document.write('else{\n')
newwin.document.write('setTimeout("chkstate()",2000)\n')
newwin.document.write('}\n')
newwin.document.write('}\n')
newwin.document.write('function print_win(){\n')
newwin.document.write('window.print();\n')
newwin.document.write('chkstate();\n')
newwin.document.write('}\n')
newwin.document.write('<\/script>\n')
newwin.document.write('</HEAD>\n')
newwin.document.write('<BODY onload="print_win()">\n')
newwin.document.write(str)
newwin.document.write('</BODY>\n')
newwin.document.write('</HTML>\n')
newwin.document.close()
}
//-->
</script>
like image 28
wbcrimson Avatar answered Sep 29 '22 19:09

wbcrimson