# Print to LPR from Apache through Perl or PHP



## comptech54 (Apr 28, 2014)

Hi, 

I am running:

FreeBSD 10
Apache 2.4
PHP 5.3
CUPS
Samba
The server is in an intranet environment.  Trying to make a Perl or PHP script that can be called from a client web browser on another computer and activate and send a "doc" to the lpr daemon. 

The printer is in the printcap and in CUPS as posx1.

So far *I* can call either a PHP script and a Perl script each within the CLI and I can send print commands to the "networked" printer. However *I* can't get that same script to be called from the web browser and have it work. What permissions do *I* need to adjust to get this to work?

As a note when I try using the PHP script from the browser *I* get a 
	
	



```
command not found: lpr
```
 in the error logs, however when called from the command line it works fine. 

I would prefer to use Perl if at all possible.

I have the Perl modules install: Net::LPR, Socket::IO installed

Thanks for your help.


----------



## trh411 (Apr 29, 2014)

comptech54 said:
			
		

> So far *I* can call either a PHP script and a Perl script each within the CLI and I can send print commands to the "networked" printer. However *I* can't get that same script to be called from the web browser and have it work. What permissions do *I* need to adjust to get this to work?
> 
> As a note when I try using the PHP script from the browser *I* get a
> 
> ...


"command not found" is usually associated with an unqualified call to a command that cannot be satisfied by any path in PATH. Are you fully qualifying the /usr/bin/lpr command in your script? If not, what does $ENV{"PATH"} contain for the script?

Perhaps if you show us the actual code where you are making the call to lpr(1) it may help.


----------



## comptech54 (Apr 29, 2014)

Here is the PHP script:


```
<?php
echo exec("sudo printf 'hello there\n\nthis is a real test of the system $ENV{PATH}' | lpr -P posx1");
?>
```

Here is the PERL script:


```
#!/usr/bin/perl -w
use Net::LPR;
open(LPR, "| lpr -P posx1");

#open (FILE,"< $0 ") or die "Couldn't open: $!";
#my $text = do {local $/; <FILE>};
#close FILE;
$text = "this is a test \f \n \n <ESC> FF \n\n\n";
print LPR $text;
close LPR;
#printf "this is a printer test" | lpr -P posx1 -o raw;
use English qw( -no_match_vars );

my $username = getpwuid $UID;

print "Content-type: text/html\n\n";
print "$username user here";
print "Hello";
print "I worked";
exit;
```


Both scripts will execute properly when calling from a terminal or command line session.


----------



## trh411 (Apr 29, 2014)

Per my previous post, does it make a difference if you fully qualify the lpr(1) command, that is:
`echo exec("sudo printf 'hello there\n\nthis is a real test of the system $ENV{PATH}' | /usr/bin/lpr -P posx1");`
and
`open(LPR, "| /usr/bin/lpr -P posx1");`


----------



## wblock@ (Apr 29, 2014)

The Perl version loads the Net::LPR module but never uses it.

@trh411 is exactly right.  There is no reason to expect the path in the web server environment to be anything like that of an interactive login.  That's why it's standard practice to use full paths to commands.  And don't use the raw command name, define the whole thing including the path in a variable and then use that, so it only has to be changed or corrected once.


----------



## comptech54 (Apr 29, 2014)

Thanks so much. adding/changing the full path to: /usr/local/bin/lpr using the Perl script is working (PHP isn't but that is ok since I prefer it is Perl anyways).

i am printing to receipt printer, do you happen to know if there is any trick to sending command codes or can I just pass hex codes as: /0x00 /0xA0 (etc). Or should I post that under a different topic, either way thanks so much for helping with this... I was slowly going crazy trying to get it to work and you guys have been great. Thanks again.


----------



## wblock@ (Apr 29, 2014)

Please start a new thread for new, different questions.


----------

