Jump to content

Problem with window.print()


jeffg

Recommended Posts

Here's the scenario: I collect some information in various forms in journey.php. The user then clicks a 'Print' button which loads journey_print.php to format and print the information. After printing I return to journey.php.In Firefox it all goes according to plan - the print dialog is displayed, the document can be printed and it returns to journey.php. In Internet Explorer, the print dialog never appears and journey.php is immediately re-displayed.This is my test version of journey_print.php:

<?php// journey_print.phpsession_cache_limiter("nocache");session_start();require ('connect.inc');require ('db_error.inc');$journey_id	= $_SESSION['journey_id'];?><!-- Start of page body --><?phpecho <<<END<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Some title</TITLE><STYLE TYPE="text/css">@media print{BODY	{font-family: Verdana, Arial, helvetica, sans-serif; font-size: 10pt}H1		{text-align: center; font-size: 18pt}H2		{text-align: left; font-size: 14pt}H3		{text-align: left; font-size: 12pt}}</STYLE></HEAD><BODY><H1>This is the heading</H1>Here is some body text.journey_id is $journey_id.END;echo "<script TYPE=\"text/javascript\">window.print();//alert('printing');</SCRIPT>";echo "<script TYPE=\"text/javascript\">window.location.href=\"journey.php?".SID."\";</SCRIPT>";?><!-- End of page body --></BODY></HTML>

If I comment out the code to return to journey.php, both browsers work ok (print dialog appears), but then I am left with the print page on the screen and nowhere to go.Any ideas, please? I really want to avoid having a return button on the printed page (always assuming that would fix things).

Link to comment
Share on other sites

Have you tried delaying the call to print() with setTimeout()? Something like...

?><script type="text/javascript">function return(){window.location.href='journey.php?<?php echo SID; ?>';}setTimeout('return();', 1000);//in 1 second</script><?php

(BTW, notice how I exited PHP to print instead of using echo for such a long string. It's less resource-intensive.)

Link to comment
Share on other sites

Have you tried delaying the call to print() with setTimeout()?
Well, I probably would have done, but it's knowing, or finding out about, these functions when you're not a JS expert :).I tried PHP sleep(2) instead between the two JS calls, but that didn't help. But I will try your suggestion anyway. With the sleep, it seemed to wait before executing the print statement instead of afterwards. Anyway, I got a weird effect.And before you craftily snuck in an edit at the instant I started composing my reply, yes, I am trying to sort out my use of single and double quotes, since I hate escaped double-quotes, too. It's unavoidable in one or two places however (I could bore you with an example).And OK about dropping out of PHP - point taken. (Actually most of the stuff before that could be straight on the page, I realized, but I just learned about heredocs and was playing with that :)
Link to comment
Share on other sites

Lol, yeah I barely realized that it would be better to leave PHP altogether than just use different quotes.I think delaying the PHP wouldn't help much because print() can't be called until the page is loaded (or at least loading) in the browser, it can't load in the browser until the server is done serving it, and the server isn't done serving it until all the PHP is executed. (At least I don't think the browser would load and the server serve simultaneously... Could be wrong.)Tell us how it goes. :)RE the below: I'm not leaving...

Link to comment
Share on other sites

Don't go away! I will edit this post in a moment - there is a syntax problem now after using your code....
I have an IE error dialog now: "Line: 24 Char: 10 Expected '('This is the page source (as generated):
<!-- Start of page body --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Some title</TITLE><STYLE TYPE="text/css">@media print{BODY	{font-family: Verdana, Arial, helvetica, sans-serif; font-size: 10pt}H1		{text-align: center; font-size: 18pt}H2		{text-align: left; font-size: 14pt}H3		{text-align: left; font-size: 12pt}}</STYLE></HEAD><BODY><H1>This is the heading</H1>Here is some body text.journey_id is 1.<script type="text/javascript">window.print();//alert('printing');</script><script type="text/javascript">function return(){window.location.href='journey.php?';}setTimeout('return();', 1000);//in 1 second</script><!-- End of page body --></BODY></HTML>

This is my modified source:

<?php// journey_print.phpsession_cache_limiter("nocache");session_start();require ('connect.inc');require ('db_error.inc');$journey_id	= $_SESSION['journey_id'];?><!-- Start of page body --><?phpecho <<<END<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Some title</TITLE><STYLE TYPE="text/css">@media print{BODY	{font-family: Verdana, Arial, helvetica, sans-serif; font-size: 10pt}H1		{text-align: center; font-size: 18pt}H2		{text-align: left; font-size: 14pt}H3		{text-align: left; font-size: 12pt}}</STYLE></HEAD><BODY><H1>This is the heading</H1>Here is some body text.journey_id is $journey_id.END;?><script type="text/javascript">window.print();//alert('printing');</script><script type="text/javascript">function return(){window.location.href='journey.php?<?php echo SID; ?>';}setTimeout('return();', 1000);//in 1 second</script><!-- End of page body --></BODY></HTML>

What have I done?? :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...