Print document in PHP


Is it possible to print a document i.e webpage using PHP. I’m looking for something similiar to the javascript function

<input type=“button” value=“Print This Page” onClick=“window.print()” />

but want to use php ?

thanks for you help


PHP resides on the server, so you cant use PHP to initiate a print command directly, only to output something to the browser, the browser uses JS to start an automatic print. Why cant you use the javascript command ??

That’s correct. PHP does things only to the server, and then sends output to the user. The only way I can think of to make that output make the browser print is using the Javascript function. Here’s a code sample plucked off the Internet:

<input type="button" value="Print this page" onClick="window.print()">

I think the staff in the server room would be slightly annoyed if users all over the world started printing stuff on the host’s printers. :wink:

yeah thought so…was just wondering if i could do it in case the client had javascript turned off

thanks for the replies…

Client can go to File->Print themselves if they don’t have javascript. It’s not that hard.

You could always check if javascript is turned off, if so just dont show the print button, this saves people hitting buttons that apparently, to them, dont work.

A button (or any other element) that depends on JavaScript should, of course, be created by JavaScript in the first place.

Instead of calling the window.print using button you can try this as the last line in your php

echo "<script>window.print();</script>

Also try <noscript> tag for the non js support browser.

@keviin6: please read my post immediately preceding yours. :slight_smile:

You can print documents in PHP… (in linux)…

just include this syntax…


system(“lpr [document_filename]”);



Wont that print the file at the server end ???

The OP wanted the users browser to print the page.

I personally find the JS print dialog annoying, most of the time I just want a printable version displayed so I can choose what I want to do with it :wink: