Print your web pages on the fly!

How to make your web pages print automatically.

A feature a lot of people always are asking me how they can print their web pages automaticaly with a button on their page. Here's the best way to achieve this:

<a href="Javascript:window.print();">Print This Page</a>

Now there's a couple of net things that you can also add to your pages, this is an example of just one of thos things. A page break, this will ensure that when a user prints the page, they receive the formatting you want.

<p STYLE="page-break-before: always">test text in the paragraph</p>

The best thing to do is to create a page called "print.cfm". I'll be making a fake query (You can replace this with your own query. The query will retrieve the data you want printed to the end user. Let's take a look at the code:

<!--- Here you retrieve the data that's going to be printed --->
<cfquery name="qGetRecords" datasource="YourDSN">
    SELECT     *
    FROM        TableName
    WHERE      fieldName = '#FieldVale#'
    ORDER       fieldName
</cfquery>

<!--- Now let's create the page that will be printed --->
<cfoutput>
    <html>
        <head>
            <title>
The site titles</title>
        </head>

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

        <!--- Display the users initial information --->
        <B>User Information:</B><BR>
        <BR>

        <cfloop query="qGetRecords">
            First name: #FirstName#<BR>
            Last Name: #LastName#<BR>
            Email: #Email#<BR>
            Biography: #Bio#<BR>
            <!-- now if you want each user to print in a different 
                   page then you would add this here, (else leave it out!) --->

            <p STYLE="page-break-before: always">&nbsp;</p>
        </cfloop>
    </body>
</html>

</cfoutput>

That's pretty much it!

Questions, Comments? Email Me
...

All ColdFusion Tutorials By Author: Pablo Varando
Download the EasyCFM.COM Browser Toolbar!