Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Print Direct PHP <or> Other ways of printing, examples within.

Posted on 2011-02-18
Medium Priority
Last Modified: 2013-12-13
My client needs to print php generated pages after an order is taken to the dot matrix printer on the network
while leaving the client default printer setting alone. So with a good amount
of research I found the following....

<script language='VBScript'>
Sub Print()
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"

<script language="javascript">

bypasses printing prompt window, this works great if the default is the printer that we want to print too. however the clients computers each have their own and then the network. default to network could be great, but we don't want accidental printing on our invoices of say an email or private info due to a bad click by the user! OPS!

What I want to do is again print directly to the network printer, every client comp has the driver so if there was also maybe a way to change the default to another with a prompt? IE client doesn't pick from default printer window but I give them the option in the browser with a pop-up, I have thought about doing this with changing the default printer as well doing the following....

function addPrinter25()
var x=confirm("You are about to add printer LON-691-08-P206. Would you like to make this your default printer?")
if (x==true)
WshNetwork = new ActiveXObject("WScript.Network");
var PrinterPath = "\\\\emalonptr00\\lon-691-08-p206";
alert("Printer LON-691-08-P206 has been added successfully and set as default")
WshNetwork = new ActiveXObject("WScript.Network");
var PrinterPath = "\\\\emalonptr00\\lon-691-08-p206";
alert("Printer LON-691-08-P206 has been added successfully")

I found the excample on the web, it's a good way of doing it, but having issue implmenting it. So I looked into direct printing and added the print.dll to my server and tested that with a text command. Which printed fine however doing the below to attempt to access a generated file is having a bad result. Converting to pdf is not possible btw, that is being used in other applications atm.

$filename = '"order_items_print.php?all=".$oldvalues["Order_num"]?>"';
include $filename;
$contents = ob_get_contents();
$handle = printer_open();
printer_set_option($handle, PRINTER_MODE, "raw");
printer_write($handle,$contents, "test");

will not work, or at least, I can't get a hold of the generated page. (error)

Error description include("order_items_print.php?all=".$oldvalues["Order_num"]?>") [function.include]: failed to open stream: No such file or directory
URL *******************/*******************/order_items_print.php?all=".$oldvalues["Order_num"]?>
Error file C:\vhosts\*******************\output\include\order_items_events.php
Error line 2153
SQL query SELECT `ItemName` FROM `items` WHERE `ItemID` = 6

which I am running around in circles, I have no problem accessing my network printer as I was able to do text testing through the php code. Just
getting a rendered page is the issue here and putting that to the network printer.

This is again an intranet application, I have full access to the apache 2.2.6 , php5, mysql , win2008 server and all clients and all code
as I am writing it myself. I am just stumped, I know I am missing something here.

Getting this done will finally finish this 3 year project in the making! So to me worth everypoint for the right answer...
I do request that if you know a soultion, please provide relative example code. IE like I posted here, I can then read it
undestand what you are talking about and go from there.

This question is defintily one needed to be answered by an expert in the field, I have been in this industry 20+ years and
sadly I can't figure this one out for the life of me ever sense the changes to IE 4 and above.

Finally I know about the activex printing out there http://www.zeepe.com/zeepeinfo/default.asp which paying a yearly fee is just not worth it to the client.  I am thinking maybe even a keylogger at this point, but rather attempt to keep it all in one application.  Another thougth I was having is capturing the URL then using a keylogger to say "ok this is the printer to goto" but no luck finding anything out there yet. I am open for discussion of course, however again if your going to realted directly to code please provide examples, it's easy to talk to the talk, but to walk it is a whole other story.

Noel B.
Question by:chaintm
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
  • 4
  • 3
LVL 84

Accepted Solution

Dave Baldwin earned 2000 total points
ID: 34931907
PHP running thru Apache doesn't normally have access to anything outside of the web root.  However, you can setup a 'CGI' directory in Apache where you can place programs that can run under just about any permissions.  You could place a program there that has permission to access your network printer and call or link to it to do your printing.

I can't give you code but that is the method in Apache to get access to the machine.

Author Comment

ID: 34937758
Thank you, I will look into apache once I get back to work on monday and see if this is viable.

Noel B.

Author Comment

ID: 34937936
Follow up, Eh I couldn't wait :) So I did some searching, well now I know why people don't CGI anymore eeegads! hehe, reminds me of DOS days. Anyways, I will need to find an alternative solution to this or ask here for CGI assistance. I again am a coder for many years, so I can figure anything out given enough time, thou I need a place to start.

Setting up CGI, simple enough, getting it to access my printer and be called from a client hitting a certain invoice print page I will get lost. Being that CGI is a script language, it shouldn't be too rough, always starting out in something can be somewhat of a nightmare. Oh and yes, I know, been doing this 20+ years, How don't I know about CGI by now? Eh, lucky I guess ;P hehe..
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Closing Comment

ID: 34937950
It leads off to a "possble" solution but doesn't help with someone not in the know... something like "try looking here" or a lead would have been better. Other then that, at least I have a point in a direction I can work with.
LVL 84

Expert Comment

by:Dave Baldwin
ID: 34938031
Sorry I couldn't help more but CGI is something I know about but I haven't done anything like what you asking.  Note that CGI is not limited to Perl, any language or executable can be used if you set it up for it.

In general, printing on paper from browsers is a pain.  Browsers are not formatted for paper printing which is why a lot of people turn to PDF for it.  Thanks for the points though.
LVL 84

Expert Comment

by:Dave Baldwin
ID: 34938144
I found an example that actually works on my networked LJ4 printer.  I'm not sure how to get it to work with your PHP text though.
use IO::Socket;
#!$host=shift @ARGV;
#!$port=shift @ARGV;
$socket=IO::Socket::INET->new(PeerAddr=> $host, PeerPort=> $port, Proto=> 'tcp',
Type=> SOCK_STREAM) or die "Can't talk to $host at $port";

while (<>) {
print $socket $_;
close $socket;

Open in new window

LVL 84

Expert Comment

by:Dave Baldwin
ID: 34938168
That example works to print a file passed on the command line.  I don't know what to do to get it to accept text passed on the command line or thru 'stdin'.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

618 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