Server side printing in PHP 5 - php

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.

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.

PHP print files in directory to printer

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.

Download file to server using API (it triggers prompt)

I want to store some data retrieved using an API on my server. Specifically, these are .mp3 files of (free) learning tracks. I'm running into a problem though. The mp3 link returned from the request isn't to a straight .mp3 file, but rather makes an ADDITIONAL API call which normally would prompt you to download the mp3 file.
file_put_contents doesn't seem to like that. The mp3 file is empty.
Here's the code:
$id = $_POST['cid'];
$title = $_POST['title'];
if (!file_exists("tags/".$id."_".$title))
{
mkdir("tags/".$id."_".$title);
}
else
echo "Dir already exists";
file_put_contents("tags/{$id}_{$title}/all.mp3", fopen($_POST['all'], 'r'));
And here is an example of the second API I mentioned earlier:
http://www.barbershoptags.com/dbaction.php?action=DownloadFile&dbase=tags&id=31&fldname=AllParts
Is there some way to bypass this intermediate step? If there's no way to access the direct URL of the mp3, is there a way to redirect the file download prompt to my server?
Thank you in advance for your help!
EDIT
Here is the current snippet. I should be echoing something, correct?
$handle = fopen("http://www.barbershoptags.com/dbaction.php?action=DownloadFile&dbase=tags&id=31&fldname=AllParts", 'rb');
$contents = stream_get_contents($handle);
echo $contents;
Because this echos nothing.
SOLUTION
Ok, I guess file_get_contents is supposed to handle redirects just fine, but this wasn't happening. So I found this function: https://stackoverflow.com/a/4102293/2723783 to return the final redirect of the API. I plugged that URL into file_get_contents and volia!
You seem to be just opening the file handler and not getting the contents using fread() or another similar function:
http://www.php.net/manual/en/function.fread.php
$handle = fopen($_POST['all'], 'rb')
file_put_contents("tags/{$id}_{$title}/all.mp3", stream_get_contents($handle));

Download a dynamically generated file using PHP

This might sound really "nooby" but I need to find a way for PHP to download an XLS file to a server folder. This file is not stored in another server, it is dynamically generated with another PHP script.
This is what I got from browsing the web but it's not working:
<?php
$url = "http://localhost/ProyectoAdmin/admin/export_to_excel.php?id=1&searchtype_id=2";
$local_file_path = './xls_tmp/Report.xls';
$xlsFile = file_get_contents($url);
file_put_contents($file_path,$xlsFile);
?>
I'd really appreciate any hint.
You're missing an end quote on your second line.
It should be: $local_file_path = './xls_tmp/Report.xls';

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...";
?>

Categories