Printing url content - javascript

I have a php page that has a chart, date picker (calendar) and a few buttons.

I want to add another "Print Chart" button, which ONLY prints the chart, not the entire page, in the local printer.

I am trying to do this with another script (which only displays a chart) and using the javascript function 'window.print'

HTML

<input type="button" onClick="printChart()" value="Print Chart"> 

Javascript

 function printChart(){ var myParameters = window.location.search;// Get the parameters from the current page var URL = "http://my_server/my_folder/my_script_that_outputs_only_my_chart.php"+myParameters; var W = window.open(URL); W.window.print(); // Is this the right syntax ? This prints a blank page and not the above URL } 

I tried the above code - it does not work. A blank page is printed.

Is there a way to print the destination URL? If so, is it possible to print it without opening a new window?

thank you for your time

+4
source share
3 answers

You can use the print style sheet ...

 <link rel="stylesheet" type="text/css" media="print" href="print.css" /> 

... in addition to your regular style sheet. In print.css, just do everything "display: none"; besides what you want to print.

+5
source

Try removing the call to W.window.print () and add

 <body onload="window.print();"> ... </body> 

into your php document to make sure your document is ready to print, and just print this page.

Regarding printing on this page, adding

  <input type="button" value="Print" onclick="window.print();" /> 

must work.

+3
source

Try using CSS @media print

"I want to add another Print Chart button that ONLY prints the chart, not the entire page, on the local printer."

Just try javascript print and then create a css class like

 `@media print` { .onprinthide { dispaly: none; } } 

and applies to all those items that are not required for printing. A.

0
source

All Articles