Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send php output to printer

Status
Not open for further replies.

DustDevil1980

Programmer
Oct 14, 2003
14
US
I have created a report in php that I want to automatically print every day.

<php?
$handle = printer_open("SAVIN4045ePCL6");
printer_set_option($handle, PRINTER_MODE, "raw");
printer_write($handle,"test.php");
printer_close($handle);
?>

The above code just gives me one line with ‘test.php’ printed on it. I want to send the output of test.php to the printer. Am I on the right track or is there a better way to do this?

I have spent days searching the web trying to figure this out and have come up dry. I have to imagine that this is a fairly common task, but I am unable to find any information on it.
 
printer_write takes a string to print, not a file. You need to open the file and then pass its contents to the printer_write function.

Try [blue]fopen()[/blue] and/or [blue]file()[/blue] to open the file, and then pass its contents to the printer_write function.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
How do I pass the contents to the printer?

I tried:

<?
$pHandle = fopen("test.php", "r");
$handle = printer_open("SAVIN4045ePCL6");
printer_set_option($handle, PRINTER_MODE, "raw");
printer_write($handle,$pHandle);
printer_close($handle);
?>

... all I got was 'Resource id # 1'
 
Read the links I provided for the functions.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I got it to work using fopen() and fread(), the problem is that it strips out all php and SQL code and just passes the html to the printer.

There has to be away to execute the code so I can get the actual data.
 
Try the eval() language construct. From reading the manual, it looks like you'll need to open the other script, read its contents into a string, then use eval() to execute the script and save the output to another variable. I think it would look like this, but I haven't tried it:

Code:
$Phandle = fopen(test.php);
$contents = fread($Phandle);
$eval("\$output = \"$contents\";");
$handle = printer_open("SAVIN4045ePCL6");
printer_set_option($handle, PRINTER_MODE, "raw"); 
printer_write($handle,$output);
printer_close($handle);
 
I don't think this is going to work:

Code:
[red]$[/red]eval([red]"\$output[/red] = \"$contents\";");


Eval() might be what you are looking for, but you would need to restructure your test.php code such that it doesn't echo out stuff on the spot, but returns a single sting with all the html code, and whatever data the php portion produced, at the end of the file. Also since you are sending it straight to the printer, the html will not get parsed by the browser, so your output would look like
Code:
<html>...<div>some text</div>...</html>
Not actual webpage layout and images.

I would think you need to call the printing function after the test page has rendered, or Make the output of the test.php text only so that it doesn't require a browser to interpret the html code.


Maybe someone here has some ideas of how to get it to work.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Oops, that was a typo on my part. Thanks for pointing it out vacunita.

Code:
$eval("\$output = \"$contents\";");

should be

Code:
[b]eval[/b]("\$output = \"$contents\";");
 
After playing with it some more today and reading about eval(), I think that vacunita is on the right path. There's a good chance that you could get this to work by adding some javascript or vbscript to the HTML output of the original PHP file.
 
I was able to run the php code and save the output as a plain html file. I then sent the html file to the printer. However, all my testing was done on screen to save paper. As mentioned above, it just sent the raw unrendered html source code to the printer.

I have spent way too much time on this... so for now the report is just going to have to be run manually. Thanks to all for your comments and input.
 
Well, I was finally able to get this to work. I don't know if it will do what you want, but here is the code that I used in my tests.

Code:
<?php

$fhandle = fopen("test.php","rb");
$contents = fread($fhandle, filesize("test.php"));
$output = eval($contents);

$handle = printer_open("Dell Laser Printer M5200");
printer_set_option($handle,PRINTER_MODE,"raw");
printer_write($handle,$output);
printer_close($handle);

?>

Code:
$now = time();
return $now;

Notice that test.php does not have open or closing tags. It also returns all it's output at once. I did not experiment with formating the output, but you might be able to include some non-printing control characters for your printer to do that. In all, it's probably more work than you want to put in.
 
That does work for simple text, but this report is sent to our customers, so it needs formating and grpahics included with it.

I will keep this on hand for other reports that do not require formating.
 
You may use file_get_contents()
for ex:
load a URL (full path) to the test.php anywhere on your server

then point file_get_contents to that URL
and it should properly parse your PHP
 
here is the way that I would do it. Using output buffering to store the evaluated code. I have found this technique very useful on a number of occasions

Code:
<?php
$filename = "test.php";
///////
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
///////
$handle = printer_open("SAVIN4045ePCL6");
printer_set_option($handle, PRINTER_MODE, "raw"); 
printer_write($handle,$contents);
printer_close($handle);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top