PHP print files in directory to printer - php

I'm trying to print files directly from html button called print and this is my code:
if($handle = printer_open("\\\\servername\\printername")){
printer_set_option($handle, PRINTER_MODE, "raw");
$output = "file.pdf";
printer_write($handle,$output);
printer_close($handle);
}
But, the code doesn't work, did I miss something?
If I put echo "test" inside the if statement, it echoed, and it means that my printer path is correct, right?

The function printer_open is not a native PHP call. It exists in specific packages available online, but does not work without recompiling PHP with the package installed. A useful script others, including myself, have used in the past is to hand it directly to the system call.
system("lp $filename");
There are many options that you can add in the system call; one option needed would be destination printer. You can check the man page for lp for details.

Related

Save the console text into a txt file? (PHP)

actual I finished writing my program. Because it is only a plugin and it runs on a external server I still want to see if I get some errors or something else in the console.
I wrote every console input with echo ...;. My question now is if it is possible to get the text of the console?
Because then I could easily safe it in a .txt file and could get access to it from the web :) - Or is there another way to get the console text?
I could probably just say fwrite(...) instand of echo ...;. But this will cost a lot of time...
Greetings and Thank You!
An alternative that could be usefull on windows would be to save all the output buffer to a txt, first check your php configuration for the console app implicit_flush must be off then
<?php
ob_start(); //before any echo
/** YOUR CODE HERE **/
$output = ob_get_contents(); //this variable has all the echoes
file_put_contents('c:\whatever.txt',$output);
ob_flush(); //shows the echoes on console
?>
If your goal is to create a text file to access, then you should create a text file directly.
(do this instead of echoing to console)
$output = $consoleData . "\n";
$output .= $moreConsoleData . "\n";
(Once you've completed that, just create the file:)
$file = fopen('output.txt', 'a');
fwrite($file, $output);
fclose($file);
Of course, this is sparse - you should also check that the file exists, create it if necessary, etc.
For console (commando line interface) you can redirect the output of your script:
php yourscript.php > path-of-your-file.txt
If you haven't access to a command line interface or to edit the cronjob line, you can duplicate the starndar output at the begining of the script:
$fdout = fopen('path-to-your-script.txt', 'wb');
eio_dup2($fdout, STDOUT);
eio_event_loop();
fclose($fdout);
(eio is an pecl extension)
If you are running the script using the console (i.e. php yourscript.php), you can easily save the output my modifying your command to:
php yourscript.php > path/to/log.txt
The above command will capture all output by the script and save it to log.txt. Change the paths for your script / log as required.

Server side printing in PHP 5

How can i print my html file via php script? I just want to run it in background without any prompt. I have read other posts regarding this but still didnt find anything working.
I tried this one :
<?php
$dir = "temp"; // the folder that you are storing the file to be printed
$file = "file.html"; //change to proper file name
$file_dir = $dir.$file;
$server = "home_computer"; //name of the computer you are printing to on your network
$printer = "HP"; //printers shared name
$command = "print $file_dir /d:\\$server\\$printer";
exec($command) or die("File failed to print");
?>
got this example here http://www.phpfreaks.com/forums/index.php/topic,207946.0.html
here is what i got working :
$html = "testing print";
$handle = printer_open();
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_write($handle, $html);
printer_close($handle);
We require php_printer.dll php extension to make this work in php5. :)
You can't print html pages with php. Php is a server-side language, it runs on the server.
The printer is on the client's machine. Meaning you'll need a client-side language to accomplish this.
If you want to print the source code then it should be be doable by writing a program that prints the passed string and then calling it via a system call. On windows it appears to have an extension for that.
If you want to print a rendered version, then you have to know that for that you need some kind of a rendering engine. While not impossible its probably more work than what you want to get into.

printing over network from PHP app

I have a set of printers connect over a network with Static IP assigned to each printer.
Now i have a PHP web application running on a linux server which needs to send print jobs, to these printer over the network.
Is this possible using lpr or cups and how do i go about it.
You could use the LPR Printer class from here:
http://www.phpclasses.org/package/2540-PHP-Abstraction-for-printing-documents.html
Example:
<?php
include("PrintSend.php");
include("PrintSendLPR.php");
$lpr = new PrintSendLPR();
$lpr->setHost("10.0.0.17"); //Put your printer IP here
$lpr->setData("C:\\wampp2\\htdocs\\print\\test.txt"); //Path to file, OR string to print.
$lpr->printJob("someQueue"); //If your printer has a built-in printserver, it might just accept anything as a queue name.
?>
This question has been asked before. See print to a network printer using PHP
The answer given that time was exec("lpr -P 'printer' -r 'filename.txt');
However, the answer was never accepted so not sure whether the OP found it helpful; it certainly looks like it ought to do the trick, but it's not quite a direct and easy method of doing it from within PHP.
A number of other resources I found were also recommending variations on this approach.
Digging a bit deeper, I see PHP has got a Printer module in PECL. However it's only for Windows, and looks like it's not well maintained. But in case it helps, the link it here: http://www.php.net/manual/en/intro.printer.php
I think the answer ultimately is that PHP isn't really designed for this kind of thing, and doesn't have built-in functionality to do it. But since you can shell out to external commands using exec() and similar, it shouldn't be too hard to get it working, albeit not quite ideal.
Try PHP::PRINT::IPP
It worked perfectly for me.
Basic Usage
<?php
require_once(PrintIPP.php);
$ipp = new PrintIPP();
$ipp->setHost("localhost");
$ipp->setPrinterURI("/printers/epson");
$ipp->setData("./testfiles/test-utf8.txt"); // Path to file.
$ipp->printJob();
?>
Reference
i was also doing research on this...and i think the below written code can help you in handling printer in linux
<?php
$printer = "\\\\Pserver.php.net\\printername");
if($ph = printer_open($printer))
{
// Get file contents
$fh = fopen("filename.ext", "rb");
$content = fread($fh, filesize("filename.ext"));
fclose($fh);
// Set print mode to RAW and send PDF to printer
printer_set_option($ph, PRINTER_MODE, "RAW");
printer_write($ph, $content);
printer_close($ph);
}
else "Couldn't connect...";
?>

Email piping with php script

Hi' I want to forward all the emails(which are come to my inbox) to php script and retrieve email content and save it in a file. So do that I was add email forwarder with piping path correctly.
Address to Forward :tickets#ana.stage.centuryware.org
Pipe to a Program : /home/centuryw/public_html/stage/ana/osticket/upload/api/pipe.php
I have used following script as pipe.php
#!/usr/bin/php –q
<?
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("mail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
/* Script End */
But there was no output file and all email are bounced to my inbox again. Can anyone help me please?
Make sure the PHP file has the execute bit set (i.e. chmod +x pipe.php).
I know this question is 2 years old but hopefully this will help anybody who stumbles across it as I did.
I had exactly the same issue and I spent ages trying to log errors etc. I then noticed that my script (and this one) had PHP short tags (i.e. <?) and my PHP config file had these turned off. I changed the tag to <?php and the script worked! Obvious but easy to miss...
try the following 2 options to check:
First, your php file must need to have execute permission(from owner group at least), otherwise it won't work.
What did you used when you were using forwarder? You need to mention php compiler path at the beginnig also.
I have recently written an article regarding the full detail process of piping email content to php program , you will may like to have a look. Let me know if you have any more question. Thanks.
Chanaka -
This doesn't address why there is no output file, but...
Don't you want to use a+ in your fopen() call? The w+ argument will delete any content that already exists in your output file.
PS: Have you tried doing a simple test which writes to the output file using dummy text (not the input from an e-mail) as the contents?
Jeff Cohan
I encountered the same problem. However I solved it by specifying the output file with full path name. Instead of just 'mail.text', i entered '/home/username/public_html/mail.txt'.
If this is actually an email box, why not use IMAP (PHP)? There are lots of classes to read the mail with imap # phpclasses.org

PHP - Will fwrite() save the file immediately?

This is my code:
$zplHandle = fopen($target_file,'w');
fwrite($zplHandle, $zplBlock01);
fwrite($zplHandle, $zplBlock02);
fwrite($zplHandle, $zplBlock03);
fclose($zplHandle);
When will the file be saved? Is it immediately after writing to it or after closing it?
I am asking this because I have Printfil listening to files in a folder and prints any file that is newly created. If PHP commits a save immediately after fwrite, I may run into issues of Printfil not capturing the subsequent writes.
Thank you for the help.
PHP may or may not write the content immediately. There is a caching layer in between. You can force it to write using fflush(), but you can't force it to wait unless you use only one fwrite().
I made a tiny piece of code to test it and it seems that after fwrite the new content will be detected immediately,not after fclose.
Here's my test on Linux.
#!/usr/bin/env php
<?php
$f = fopen("file.txt","a+");
for($i=0;$i<10;$i++)
{
sleep(1);
fwrite($f,"something\n");
echo $i," write ...\n";
}
fclose($f);
echo "stop write";
?>
After running the PHP script ,I use tail -f file.txt to detect the new content.And It shows new contents the same time as php's output tag.
the file will be saved on fclose. if you want to put the content to the file before, use fflush().
Assuming your working in PHP 5.x, try file_put_contents() instead, as it wraps the open/write/close into one call.
http://us3.php.net/manual/en/function.file-put-contents.php

Categories