Print directly to network printer using php - php

I am unable to print a page to a network printrer using php.
But this works if it is a local printer.
I have installed php_printer.dll and enabled in php.ini
The following is the code:
//$handle = printer_open("Send To OneNote 2007"); ///This Works
$handle = printer_open('\\\\192.168.0.8\\Canon MF4320-4350');
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_write($handle, "TEXT To print");
printer_close($handle);
It shows the error
Warning: printer_write() [function.printer-write]: couldn't allocate the
printerjob [5] in E:\Server\xampp\htdocs\Kiosk\Admin\print.php on line 16

If you used the command line PHP (CLI) the printing to network printers would work. Your $addr is correct by the way.
The issue lies with PHP when you combine it with Apache. In windows, your php scripts would run under the user SYSTEM. Because of security concerns, all network resources are not visible to SYSTEM.
To resolve this problem, create a new user with admin privileges (or at least with Network Resource visibility). In Windows, if you run Apache as a service, click on the SERVICE button in the Apache Service Monitor. Under Apache 2.2, right click on properties. Under the LOGIN tab, change the user from SYSTEM to your newly created user account. Restart Apache. You should be able to run your PHP script to print to network printers now.

Try using either "s with 4\ or 's with 3. eg:
$handle = printer_open("\\\\192.168.0.8\\Canon MF4320-4350");
// or
$handle = printer_open('\\\192.168.0.8\Canon MF4320-4350');
Also, try using a domain name rather than IP (eg. computer-name or full.address.example.com).

Just looking at that string, it feels very much as if it's likely to be caused by back-slash escaping going wrong.
Debugging tip: Set your network address in a variable rather than directly in printer_open(). Then use print() or similar to display the value.
<?php
$addr = '\\\\192.168.0.8\\Canon MF4320-4350';
print $addr;
printer_open($addr);
...
?>
This will allow you to see the value of the string that you're using, which might help you see what's going wrong, and more importantly, help you see how to fix it.
Hope that helps.

Related

How to execute xpdf (pdftotext.exe) on shared drive?

im trying to parse pdf to text via PHP and XPDF (pdftotext.exe). On my localhost everythings works well, but when im trying to move everything on server, im getting into troubles.
First of all i checked some settings on server and safe_mode is off, exec is not disabled and permissions are rwxrwxrwx.
Then im trying this
$command = "\\\\149.223.22.11\\cae\\04_Knowledge-base\\tools\\pdftotext.exe -enc UTF-8 ". $fileName . " \\\\149.223.22.11\\cae\\04_Knowledge-base\\output.txt";
$result = exec($command,$output,$args);
echo shell_exec($command);
which isnt working. When i look into $result, $output, are empty, but $args returns 1 which coresponds to Incorrect function by this document windows system error codes
Whole command looks like \\149.223.22.11\cae\04_Knowledge-base\tools\pdftotext.exe -enc UTF-8 \\149.223.22.11\cae\04_Knowledge-base\testpdf\04_egerland_final_paper.pdf \\149.223.22.11\cae\04_Knowledge-base\output.txt and when is dirrectly inputed into commandline, its working.
So im a bit out of ideas. Have someone any hint?
edit 20160201 - aditional trying
So i made aditional tests and when im trying to run similar command with exec from localhost (target .exe file, input and output file is in same location, only im using localhost not server) its working. Im now checking differences in server settings. So can here be problem, that localhosts Server Api is Apache 2.0 Handler and server is CGI/FASTCGI?
So it was all mistake on my side. I badly checked IIS permissions and forgot to assign user to virtual directory which i wana to access. So my only advice and more wisdom from this is to double (maybe triple) check if you have all permissions set correctly.

PHP under IIS6 can't open/read file on network

I have a simple php script that runs on an IIS 6 2003 server. All the script does is try to open a file on a shared network network location and display its content to the screen. Try as I might I cannot get that to work. I keep getting a "failed to open stream error message". After a lot of reading (I'm on my second week of working at this) I narrow the problem down to being a server configuration problem. I can run the script through the command prompt and it works fine. If I run var_dump(shell_exec('whoami')) It returns "NULL". If I run that same command on the command prompt it returns the current user that is logged in (i.e. me). The task manager reports that the user for w3wp.exe is "NETWORK SERVICE". I'm including the code below although I'm 100% sure the code is not the problem but, some people like to look at code so there it is. How do you configure or make changes on the server so that it allows reading from a network location? Also, the network location I'm trying to access have been setup with all permissions for everybody so that we can solve this one issue.
<?php
$theFile="\\\\192.168.0.16\\geo\\junk.txt"; #network file does not works
#$theFile="junk.txt"; #local file works fine
$handle = fopen($theFile, "r");
if($handle){
while (!feof($handle)){
$buffer = fgets($handle);
echo $buffer."<br />";
}
}
?>

Print to Zebra printer in php

Looking for the proper code to print from php web page to a zebra IP printer using RAW port 9100. Does anyone know if this is possible? I need to send a string in ZPL formatted output direct to ZM400 label printer. I've searched high and low, the closest I've found is this: Print directly to network printer using php
It seems very close to what I need, but when my php page hits that code, it doesn't do anything. Here's the code I used:
<?php
$handle = printer_open('\\\\192.168.2.206:9100\\');
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_write($handle, "TEXT To print");
printer_close($handle);
?>
I realise this question is a little old but I recently had to perform this exact task and here is how I did it.
Main server is a Cloud-Based PHP server which is not on the local network. On the local network we have another machine which is simply running WAMP and this script, the Zebra printer itself is also on the local network at IP 192.168.1.201:
<?php
/*
* File Allows printing from web interface, simply connects to the Zebra Printer and then pumps data
* into it which gets printed out.
*/
$print_data = $_POST['zpl_data'];
// Open a telnet connection to the printer, then push all the data into it.
try
{
$fp=pfsockopen("192.168.1.201",9100);
fputs($fp,$print_data);
fclose($fp);
echo 'Successfully Printed';
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Then, on the webpage generated by the cloud server we have some code which simply does an Ajax POST to the server on the local network, posting in the zpl_data to be printed.
Edit 2017
We've now moved things over to run through PrintNode (https://www.printnode.com/). We've found it to be really good so far, and allows us to print all kinds of documents without needing to use our own proxies, and also provide a whitelabelled installer so it looks like our own product. I'm not affiliated with PrintNode.
If you're looking to send ZPL to the printer, you don't necessarily need a dedicated printing library. You just need to open up a socket to that printer and send your ZPL directly. This is more of a general PHP socket-communication question as opposed to a printer-specific question.
If the server hosting your web app and the printers are on the same network, then you will be able to open the socket and send the ZPL. If, however, your printers and web app server are on different networks, you will not be able to print over sockets, on that model printer, without additional browser plugins or add-ons. Generally speaking, accessing a remote printer (or any device) via a web-site is a security risk.
The printer_open() and related functions are not part of the standard PHP language; they are part of an extension.
If you want to use them, you'll need to install the extension: See here for info on the printer extension.
However, please note that this extension is only available for PHP running on Windows.
If your server is not Windows, you will need to use an external program to send data to the printer. An example might look like this:
exec("lpr -P 'printer' -r 'filename.txt');
This info, and more can be found elsewhere on SO -- eg here: printing over network from PHP app
Hope that helps.
After hours of searchings I get the solutions :
After you install the printer on the needed IP:
exec('lp -d printer file');
In my case the command was:
exec('lp -d Epson-Cofetarie /home/clara/Desktop/txt.txt');
Where: printer = Epson-Cofetarie
file = /home/clara/Desktop/txt.txt
file need Apsolute path
I hope, this simple code will resolve the problem,
Put your ZPL code in a file e.g "barcode.zpl"
Share your Zebra Printer, so that it can be accessed In Windows Explorer by typing
e.g "\192.168.1.113[YOUR PRINTER NAME]";
Create PHP File, and write code:
<?php
$file="barcode.zpl";
copy($file, "//192.168.1.113/ZDesigner GK420t");
?>
Thank You

How to view PHP or Apache error log online in a browser?

Is there a way to view the PHP error logs or Apache error logs in a web browser?
I find it inconvenient to ssh into multiple servers and run a "tail" command to follow the error logs. Is there some tool (preferably open source) that shows me the error logs online (streaming or non-streaming?
Thanks
A simple php code to read log and print:
<?php
exec('tail /var/log/apache2/error.log', $error_logs);
foreach($error_logs as $error_log) {
echo "<br />".$error_log;
}
?>
You can embed error_log php variable in html as per your requirement. The best part is tail command will load the latest errors which wont make too load on your server.
You can change tail to give output as you want
Ex. tail myfile.txt -n 100 // it will give last 100 lines
See What commercial and open source competitors are there to Splunk? and I would recommend https://github.com/tobi/clarity
Simple and easy tool.
Since everyone is suggesting clarity, I would also like to mention tailon. I wrote tailon as a more modern and secure alternative to clarity. It's still in its early stages of development, but the functionality you need is there. You may also use wtee, if you're only interested in following a single log file.
You good make a script that reads the error logs from apache2..
$apache_errorlog = file_get_contents('/var/log/apache2/error.log');
if its not working.. trying to get it with the php functions exec or shell_exec and the command 'cat /var/log/apache2/error.log'
EDIT: If you have multi servers(i quess with webservers on it) you can create a file on the machine, when you make a request to that script(hashed connection) you get the logs from that server
I recommend LogHappens: https://loghappens.com, it allows you to view the error log in web, and this is what it looks like:
LogHappens supports kinds of web server log format, it comes with parses for Apache and CakePHP, and you can write your own.
You can find it here: https://github.com/qijianjun/logHappens
It's open source and free, I forked it and do some work to make it work better in dev env or in public env. That is:
Support token for security, one can't access the site without the token in config.php
Support IP whitelists for security and privacy
Sopport config the interval between ajax requests
Support load static files from local (for local dev env)
I've found this solution https://code.google.com/p/php-tail/
It's working perfectly. I only needed to change the filesize, because I was getting an error first.
56 if($maxLength > $this->maxSizeToLoad) {
57 $maxLength = $this->maxSizeToLoad;
58 // return json_encode(array("size" => $fsize, "data" => array("ERROR: PHPTail attempted to load more (".round(($maxLength / 1048576), 2)."MB) then the maximum size (".round(($this->maxSizeToLoad / 1048576), 2) ."MB) of bytes into memory. You should lower the defaultUpdateTime to prevent this from happening. ")));
59 }
And I've added default size, but it's not needed
125 lastSize = <?php echo filesize($this->log) || 1000; ?>;
I know this question is a bit old, but (along with the lack of good choices) it gave me the idea to create this tiny (open source) web app. https://github.com/ToX82/logHappens. It can be used online, but I'd use an .htpasswd as a basic login system. I hope it helps.

Apache: reverse proxy to process PHP from another server

I have the following setup:
Plain-Server: Delivering php-files as plain text
Proxy-Server: Asking the Plain-Server for the php file and parsing it.
Now my question: How do I configure the Proxy-Server (a fully configurable apache 2.2 with PHP 5.3) to interpret the plain php files from Plain-Server?
Example: Given a small php script "hello.php" on Plain-Server (accessible throw http://plainserver/hello.php):
<?php
echo "Hello World";
?>
Plain-Server only outputs it as plain text, no parsing of php-code.
On the Proxy-Server the file "hello.php" does not exist. But when requesting hello.php from Proxy-Server it should get the hello.php from Plain-Server with mod_proxy (Reverse Proxy). It should also parse and execute the php, saying only "Hello World".
The Reverse Proxy is already running, but the execution of php code not working. I tried mod_filter, but couldn't is work. Any ideas how to that?
You may consider instead sharing the php files from your source server via an nfs mount or something similar to your target server. Tricking the proxy server into doing what you ask seems like the long way around the barn?
I totally agree with jskaggz,
you could build some awfull tricks building some apps that fetch the remote page ,
dowload it into a local file and then redirect the user to that page that could be executed...
but there is a milion security issues and things that might go wrong with that...
Can't you just convert the 'plain server' to a php excuting server and do some traditional reverse proxying
on your 'proxy server'
maybe using mod_proxy:
http://www.apachetutor.org/admin/reverseproxies ?
Answered this on the ServerFault version of this thread: https://serverfault.com/a/399671/48061

Categories