Print to Zebra printer in php - 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

Related

Is there a way in PHP to store printjobs that are sent via a Windows PC?

I am working on a project for school. I am wondering if there is a way in PHP to listen for print jobs and store them once received? I currently have a webserver that is accepting webhooks from other applications, parsing out the JSON and storing in a MsSQL. Is this the wrong path to take? thanks
Ok – I don’t know if this is a solution but here is my attempt at trying to help :)
Disclaimer: I am not affiliated to any of the links posted here and (or) make no recommendations.
General assumptions:
PHP is the server side language used in all instances (the theory should work with others as well – using PHP as the question was tagged as such)
Sender and receiver servers are different (it is more easier if both are on the same box – but not a show stopper)
Access to both sender and receiver webservers
Windows set up (WAMP or alternative)
You are securing your set up / directory accesses as you go along
Option 1 (Custom sender / receiver)
On the box that is sending the print request:
Create a PHP script to output the files to a temp directory
Create file with required extension (please see documentation if required)
Put contents
Close connection / file - https://www.php.net/manual/en/function.file-put-contents.php
Use powershell or alternative to put the files in the temp directory into a remote directory on the
https://blogs.msdn.microsoft.com/luisdem/2016/08/31/powershell-how-to-copy-a-local-file-to-remote-machines/
Run this at an interval of your choice
On the box that is receiving the file:
Create a PHP script to read files on remote
Show files to print in a list which can be downloaded to local terminal / device and printed
Option 2 (Leverage an existing print server)
In this option you would only need to amend the sender webserver config:
[inspired by: https://www.hashbangcode.com/article/printing-directly-php]
Download php_printer.dll for your version (https://windows.php.net/downloads/pecl/snaps/printer/0.1.0-dev/)
Enable in php.ini via: printer.default_printer=PHP_INI_ALL extension=php_printer.dll
Set the printer as your print server address and send your output to print directly from the Webserver as follows from your application:
`
$data= "Hello";
printer_set_option($ph, PRINTER_MODE, "RAW");
printer_write($ph, $data);
printer_close($ph);
}
else "Couldn't connect...";
?>'
Option 3 (Use a plugin):
https://www.neodynamic.com/products/printing/raw-data/php/

PHP cannot print to network printer

I have a server with ip 192.168.0.1, then I have a computer with ip 192.168.0.3 which access the server. I have a printer EPSON TM-U220 connected to 192.168.0.3, then I tried to print from the server to the printer using php command
copy($file, '//192.168.0.3/printername');
but it doesn't work, then I tried another function:
exec("print /d:\\\\192.168.0.3\\printername $file");
exec("copy $file \\\\192.168.0.3\\printername");
It says "unable to initialize device \\192.168.0.3\printername".
My computer and server run on windows7.
I already shared the printer to my server, I can test print page from the server.
how do I print to my network printer using PHP?
Thanks
After going some researches, I found a solution to my problem.
Run services.msc then double click Appache Service. At tab Log On, choose This account, then fill in the credential of the server.
After that, restart your appache..
My problem is solved after doing this..
Following erkape#s solution, I got auto printing for Laravel with Crystal Reports XI right:
$printname="\\\\SERVERBEKASI\\Canon iP2700 series (Copy 3)";
$driver='"winspool"';
$port='"Ne07:"';
$creport->SelectPrinter("winspool",$printname,$port);
$creport->PrintOut(fal`enter code here`se,1);
The solution on my system (Windows 7) looks like this:
create a new account as the administrator, set a password
Run services.msc, double-click Apache Service. At tab Log On, choose your new account, then fill in the credential of the server.
Restart Apache. Done

Print to network printer from PHP

What is the best approach for printing (an existing pdf, in my case) to a LAN printer directly from php? So far I have been unsuccessful in getting anything to work, but I'm not sure what direction to further pursue. I am running Apache on Windows SBS 2008, PHP 5.3.9.
Approaches I know of so far:
shell_exec()
phpprintipp - this seems like the best approach to me if I could get it to work
php_printer.dll - no current dll exists
It seems like this should be a simple task that has a widely accepted approach, but so far I'm not finding it. Thanks!
This is a tough nut to crack. I've had my own adventures in Windows printing from Ruby and came up with a few potential solutions that work by invoking an external command, which in PHP-land is system() or exec() (don't forget escapeshellcmd()/escapeshellarg()—they tend to make this stuff easier, especially on Windows). All of them assume Windows knows about the printer and it can be referenced by name.
You can literally just redirect the file to the networked printer, e.g.:
copy /b \path\to\filename.pdf > \\Printer_Machine\Printer_Queue
The /b switch specifies a binary file, but I'm 80% sure it's not strictly now,
in 2012.
You can try the print command:
print /d:\\Printer_Machine\Printer_Queue \path\to\filename.pdf
\d stands for "device." I haven't actually tried this one and I'm not sure if it
works with PDF or only, owing to its DOS origins, text files.
Install Adobe Reader and use its command line facilities:
AcroRd32.exe /t \path\to\filename.pdf "Printer Name" "Driver Name" "Port Name"
I'm not sure if your server environment can accommodate Reader but this is the
solution I've been most successful with. You can find
documentation here
(PDF, pg. 24). Printer Name and Driver Name should match exactly what you see in the
printer's properties in Control Panel. Port_Name can usually be omitted, I think.
Print using Ghostscript. I've never tried this on
Windows but the
documentation is here
and there's
more info here. The
command goes something like this:
gswin32.exe -sDEVICE=mswinpr2 -sOutputFile="%printer%Printer Name" \path\to\filename.pdf
mswinpr2 refers to Windows' own print drivers (see the second link above),
"%printer%" is literal and required and "Printer Name" should, again, match the
printer's name from Control Panel exactly. Ghostscript has many, many options and
you'll likely have to spend some time configuring them.
Finally, a general tip: You can register a network printer with a device name with the net use command e.g.:
C:\> net use LPT2 \\Printer_Machine\Printer_Queue /persistent:yes
This should let you use LPT2 or LPT2: in place of \\Printer_... with most commands.
I hope that's helpful!
Not sure if this works for all printers but this gets the job done sending ZPL files to a Zebra label printer:
<?php
if(($conn = fsockopen('192.168.10.112',9100,$errno,$errstr))===false){
echo 'Connection Failed' . $errno . $errstr;
}
$data = <<<HERE
^XA
^FT50,200
^A0N,200,200^FDTEST^FS
^FT50,500
^A0N,200,200^FDZebra Printer^FS
^XZ
HERE;
#send request
$fput = fputs($conn, $data, strlen($data));
#close the connection
fclose($conn);
?>

Print directly to network printer using 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.

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