Page 1 of 1

Print a Dynamic php page without displaying it

Posted: Mon Apr 21, 2008 9:06 am
by francisjeffy
In this example i will show you how to print a dynamic php page with dynamic contents without showing up the page itself.

This can be useful in situations where you need to print a dynamic page but you should not display it.

I had a scenario like this, where i use a form to gather information, and these information are not saved up in any database, and it is printed in a different pattern with images etc, and i should not display the printing page.

so got up with this mix of js and php code which does it,

hope it might be useful for some..

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<script language="javascript">
    function beforeSubmit(){
        document.getElementById("one").style.display = "block";
        document.getElementById("two").style.display = "none";
    }
    function afterSubmit(){
        document.getElementById("one").style.display = "none";
        document.getElementById("two").style.display = "block";
        window.print();
        document.getElementById("one").style.display = "block";
        document.getElementById("two").style.display = "none";
    }
</script>
<body <?php if(isset($_REQUEST[btn_submit])){ ?> onLoad="javascript&#058; return afterSubmit();" <?php }else{ ?> onLoad="javascript&#058; return beforeSubmit();" <?php } ?>>
<div id="one">
    <form name="form1" method="post" action="./test21.php">
    <table width="600" border="0" align="left">
        <tr>
            <td width="300" align="right">name:</td>
            <td width="300" align="left"><input type="text" name="str_name" size="50"></td>
        </tr>
        <tr>
            <td width="300" align="right">email:</td>
            <td width="300" align="left"><input type="text" name="str_email" size="50"></td>
        </tr>
        <tr>
            <td width="300" align="right" colspan="2"><input type="submit" value="print" name="btn_submit"></td>
        </tr>
    </table>
    </form>
</div>
 
<div id="two">
    <table width="600" border="0" align="left">
        <tr>
            <td width="300" align="right" colspan="2"><?php echo $_REQUEST[str_name]; ?>' mail id is <?php echo $_REQUEST[str_email]; ?></td>
        </tr>
    </table>
</div>
 
</body>
</html>

Line by line explanation of the code

this function diplays div "one" and hides div "two"

Code: Select all

function beforeSubmit(){
        document.getElementById("one").style.display = "block";
        document.getElementById("two").style.display = "none";
    }
this function diplay div "two" and hides div "one" and then prints div "two" and then displays div "one" again

Code: Select all

function afterSubmit(){
        document.getElementById("one").style.display = "none";
        document.getElementById("two").style.display = "block";
        window.print();
        document.getElementById("one").style.display = "block";
        document.getElementById("two").style.display = "none";
    }

Body tag display div "one" before submit and div "two" after submit, the condition checks it with submit button, js body onload event is used to call the function

Code: Select all

<body <?php if(isset($_REQUEST[btn_submit])){ ?> onLoad="javascript&#058; return afterSubmit();" <?php }else{ ?> onLoad="javascript&#058; return beforeSubmit();" <?php } ?>>

the form which takes the input values is in div "one"

Code: Select all

<div id="one">
    <form name="form1" method="post" action="./test21.php">
    <table width="600" border="0" align="left">
        <tr>
            <td width="300" align="right">name:</td>
            <td width="300" align="left"><input type="text" name="str_name" size="50"></td>
        </tr>
        <tr>
            <td width="300" align="right">email:</td>
            <td width="300" align="left"><input type="text" name="str_email" size="50"></td>
        </tr>
        <tr>
            <td width="300" align="right" colspan="2"><input type="submit" value="print" name="btn_submit"></td>
        </tr>
    </table>
    </form>
</div>

the section which displays the dynamic content from the form is in div "two"

Code: Select all

<div id="two">
    <table width="600" border="0" align="left">
        <tr>
            <td width="300" align="right" colspan="2"><?php echo $_REQUEST[str_name]; ?>' mail id is <?php echo $_REQUEST[str_email]; ?></td>
        </tr>
    </table>
</div>

Re: Print a Dynamic php page without displaying it

Posted: Mon Apr 21, 2008 9:53 am
by JayBird
Couldn't this have been done simpler with print specific CSS rules?

Re: Print a Dynamic php page without displaying it

Posted: Mon Apr 21, 2008 2:03 pm
by francisjeffy
Hi JayBird,

I tried the css, but to me it seems like you can use css print style if u r printing the same page, like one style for screen and another for print, using media="screen" and media="print". Did you mean that or is there any other way to do this, If you know some please share, it will be helpful for me.

Thanks in advance
Jeffy

Re: Print a Dynamic php page without displaying it

Posted: Mon Apr 21, 2008 3:52 pm
by onion2k
The user would still get the print dialogue, and while it's open the page will display the print version in the browser window behind it. I agree with Jaybird... use a print stylesheet.

(Or use a js enabled PDF to print without the print dialogue... :twisted: )

Re: Print a Dynamic php page without displaying it

Posted: Tue Apr 22, 2008 5:06 pm
by francisjeffy
What i've got is a huge four page doc to print, so style sheet will be a real pain, and this wont show the print page as you suggested, it will only show the form

jeF