What is the simplest way to print in php?

Posted on 2004-04-13
Last Modified: 2006-11-17
I would like to have a formatted report print on a user's default printer when prompted.  I don't have any prepared file, it would have to be created from mysql.
Question by:RUSS_EMI
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Expert Comment

ID: 10820501
Easiest is to use some sort of a pdf-generator:

whereas I have used the fpdf myself with very good results... =o)


Expert Comment

ID: 10825577
PHP can't print on it's own, however if you're outputting HTML then you can use JavaScript to bring up the print menu, by replacing the line <body> with <body onload="window.print();">,or, to display a question first: <body onload="if(confirm("Would you like to print this page?"))window.print;">.

Hope that helps.

Author Comment

ID: 10825809
Is it possible to create a text file (with php) and ask the user if they wish to print it (java script)?  That way they wouldn't get all the extra stuff that may be on the page.
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


Accepted Solution

Cijori earned 50 total points
ID: 10825867
No, JavaScript only works on an HTML page. However, if you create a text file and then add:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>Your HTML File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<body onload="if(confirm("Would you like to print this page?"))window.print;">

to the top of it and:


to the bottom then it will be an HTML file, but the content that they see when they print and on screen will be the same.


Expert Comment

ID: 10826049
cijori: "PHP can't print on it's own" it isn't really true...

you can take a look on the following page:


Expert Comment

ID: 10826189
That's printing on a printer connected to the server not the client though. Not really any use until PHP can also pack it up and post it...

Expert Comment

ID: 10826463
That's true but I don't really agree with you :)

I have used it a couple of times in my intranet php-applications. Then it's pretty useful, but as you say it isn't used very often though... =o)

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why is PHP Storm giving me an undefined constant error? 4 40
Wordpress and Wufoo 1 40
How to use session variables in php? 22 52
How to remove  "0":  in a json string 4 19
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question