Page 1 of 1

PHP/Javascript: print preview selected page

Posted: Wed Jan 11, 2012 10:08 am
by cjkeane
hi,

i need to open a print preview page and only display the contents of a defined page. Currently i'm using the following code, but it opens a print preview window of the current page, not the page defined in the href. Does anyone have any ideas on how to resolve this?

Code: Select all

<?php
    echo '<td nowrap><a href="print_preview_invoice.php?ID_Number=' . $ID_Number . '&inv_num=' .$result['Inv_Num'].'" target="_blank" onclick="print();">'.$invoice.'</a>' . " / " . '<a href="print_preview_timesheet.php?ID_Number=' . $ID_Number . '&inv_num=' .$result['Inv_Num'].'" target="_blank" onclick="print();">'.$timesheet.'</a></td>';
?>
Thanks

Re: PHP/Javascript: print preview selected page

Posted: Wed Jan 11, 2012 5:24 pm
by Christopher
Perhaps you could open a new window containing the page you want to print and call the Javascript print() function from within that page.

Re: PHP/Javascript: print preview selected page

Posted: Wed Jan 11, 2012 5:40 pm
by cjkeane
is it possible to call print() upon opening the page e.g. on load?

i just tried :

Code: Select all

<?php
echo '<td nowrap><a href="print_preview_invoice.php?ID_Number=' . $ID_Number . '&inv_num=' .$result['Inv_Num1'].'"  >'.$invoice.'</a>' . " / " . '<a href="print_preview_timesheet.php?ID_Number=' . $ID_Number . '&inv_num=' .$result['Inv_Num1'].'" >'.$timesheet.'</a></td>';
>?
when print_preview_invoice.php opens, i have <body onload="print()">

This opens the invoice, but also opens the print preview window, so it in fact opens two windows. is there any way to resolve this? i just want/need the print preview window to open.

Re: PHP/Javascript: print preview selected page

Posted: Fri Jan 13, 2012 9:58 am
by Tiancris
Instead of calling print() in body onload, add a button and call print() in onclick event. So you can see the preview and then print only if you want.

[text]<input type="button" onclick="print()" class="hidden" />[/text]

And set this style to avoid printing the button:

[text]<style type="text/css" media="print">
.hidden {display:none}
</style>[/text]

Re: PHP/Javascript: print preview selected page

Posted: Fri Jan 13, 2012 12:05 pm
by cjkeane
yes i understood. It works perfectly thanks! now the button is hidden when the button is clicked.

Re: PHP/Javascript: print preview selected page

Posted: Fri Jan 13, 2012 12:19 pm
by Tiancris
:D Exactly. And the same is applicable to any visible object that you don't want to be printed.

Re: PHP/Javascript: print preview selected page

Posted: Fri Jan 13, 2012 12:55 pm
by cjkeane
yes, i realized that as well! perfect! thanks again.