Solved

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

Posted on 2011-02-18
7
3,046 Views
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()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>
 
&&&&

<script language="javascript">
window.onload=window.print();
</script>
===================================================

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....

==============================================
<script>
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";
WshNetwork.AddWindowsPrinterConnection(PrinterPath);
WshNetwork.SetDefaultPrinter(PrinterPath);
alert("Printer LON-691-08-P206 has been added successfully and set as default")
}
else
{
WshNetwork = new ActiveXObject("WScript.Network");
var PrinterPath = "\\\\emalonptr00\\lon-691-08-p206";
WshNetwork.AddWindowsPrinterConnection(PrinterPath);
alert("Printer LON-691-08-P206 has been added successfully")
}
}
</script>
============================================

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"]?>"';
///////
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
///////
$handle = printer_open();
printer_set_option($handle, PRINTER_MODE, "raw");
printer_write($handle,$contents, "test");
printer_close($handle);
///////
==============================================

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.
0
Comment
Question by:chaintm
  • 4
  • 3
7 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 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.
0
 

Author Comment

by:chaintm
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.
0
 

Author Comment

by:chaintm
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..
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:chaintm
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.
0
 
LVL 82

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.
0
 
LVL 82

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.
#!/usr/bin/perl
use IO::Socket;
#!$host=shift @ARGV;
#!$port=shift @ARGV;
$host="10.202.46.31";
$port="9100";
$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

0
 
LVL 82

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'.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now