Solved

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

Posted on 2011-02-18
7
3,136 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
[X]
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
7 Comments
 
LVL 83

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 83

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 83

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 83

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP 5.6 and 7.x 4 40
Wordpress and Wufoo 1 39
assigning javascript variable to php variable 8 43
How do I Check for duplicate entries in mysql 15 26
This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 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