Hi there, was wondering if somebody could help me.
I currently have a project to create a php/mysql system for a local pizza company, a touch screen system. Cut a long story short, I need to be able to print multiple lines from a txt file. I have got the php to output to txt file from mysqlusing variables and I can get the php script to print 1 line. As I am new I would imagine I would need some kind of loop maybe.
would be greatfull if somebody could help. (i am using a thermal receipt till printer)

for example

receipt.txt has content

receipt number: 24
time: 22:32
date: 22/09/2008

Delivery address
Name: joe blogs
house number:
street:
town/city:
Postcode:
Tel:

Order total
15" pizza £12
order total £12

So you want to physically print the contents of receipt.txt? What method are you using to print the first line?

i am using:

$file = 'receipt.txt';
$handle = printer_open('HP LaserJet 4000 Series PCL6');
printer_set_option($handle, PRINTER_SCALE, 75);
printer_start_doc($handle, "My Document");
printer_start_page($handle);
printer_write($handle, "$file");
printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);

If I replace printer_write($handle, "$file"); with printer_write($handle, "TEXT TO PRINT"); this then prints the single line of text. I am not too sure how to print all the lines in the text file

Check out this and look into getting the entire file into a variable with "\n\r" (linefeed and carriage return) separating each line of text. Alternatively write each line to the printer with it's own call of printer_write() . The latter may be easier as you usually read data from a file line by line. It may also be torture for the printer and rather slow, I don't know how printer_write() works.

For this same reason I don't know how a new line command should be issued. Try the following and see what happens: printer_write($handle, "Text \nTo print"); This is the most likely solution and sould print:

Text 
To print

printer_write($handle, "Text \n\rTo print"); This may be needed instead to give the above result.

Also try:

printer_write($handle, "Line 1");
printer_write($handle, "Line 2");
printer_write($handle, "Line 3");
printer_write($handle, "Line 4");

And see how it reacts and if it works. If your still can't get around it then tell me how each of those tests went.

Also check that out. Someone said that PRINTER_MODE must be set to "RAW" for printing to work... don't quite know what was meant but have a look anyway.

Hi
I have tryed

printer_write($handle, "Line 1");
printer_write($handle, "Line 2");

This seems to print 2 seperate pages
line 1 is one page
line 2 is the other page

Ok, that makes sense I guess.

What about the other tests? There has to be a character that will mean "new line" and I'm guessing that if "\n" won't work then "\r\n" will.

Thank you very much for your help so far :o)

ok i have completed the other test too.

It looks like that the

printer_write($handle, "Text \n\rTo print");

did write 2 seporate lines. without using the \r switch the output was:

Text

To print

if i included the \r the output was:
Text
To Print

My next step would have the php code to send the text file to an array splitting the text file for each line and sending it to a loop to print to the printer. which im not really sure how to do?

Having studyed lots of code I have found the following code will output text file to an array

<?php
$file_handle = fopen("receipt.txt", "rb");

while (!feof($file_handle) ) {

$line_of_text = fgets($file_handle);
$parts = explode(':', $line_of_text);

print $parts[0] . $parts[1]. "<BR>";
}

fclose($file_handle);

?>

Which outputs sucessfully to the screen. But it looks like that when it gets to the end of the file having no more text to print it displays the following errors

PHP Notice: Undefined offset: 1 in C:\Inetpub\wwwroot\takeaway\receipt.php on line 25 PHP Notice: Undefined offset: 1 in C:\Inetpub\wwwroot\takeaway\receipt.php on line 25 PHP Notice: Undefined offset: 1 in C:\Inetpub\wwwroot\takeaway\receipt.php on line 25 PHP Notice: Undefined offset: 1 in C:\Inetpub\wwwroot\takeaway\receipt.php on line 25 PHP Notice: Undefined offset: 1 in C:\Inetpub\wwwroot\takeaway\receipt.php on line 25 PHP Notice: Undefined offset: 1 in C:\Inetpub\wwwroot\takeaway\receipt.php on line 25

sorry i forgot to mension that in my last comment any lines displaying : are not shown in the output to screen

changing the print function to RAW data seems to slow the printing speed
setting the printer function to TEXT seems to increase the speed

I guess that because your printing a receipt and you don't need an images, TEXT would probably be the best option (If it works just as well). Faster printing would indicate it's doing less work which can't be a bad thing.

You could use the function file_get_contents($filename) to get the contents of the file as a string. Try something like the following code:

$text_to_print = file_get_contents('receipt.txt');
//Use this next line only if it doesn't work without it, 
//ie prints "Text \n\nTo print" instead of "Text \nTo print"
$text_to_print = str_replace('\n', '\r\n', $text_to_print);

//yada yada, printer stuff
printer_write($handle, $text_to_print);
//yada yada, more printer stuff

That should all work I reckon, I reckon.

BTW, when do you write the contents to the file receipt.txt ? If it's earlier on in the script and you don't need to keep receipt.txt then you may as well skip that process. For example:

$text_to_print = '';
//load mysql info into an array using:
$data = mysql_fetch_array($result, MYSQL_ASSOC);

$text_to_print .= "Name: $data['name']\r\n";
$text_to_print .= "Date: $data['date']\r\n";
//etc.

php.net is the best place to get help on any specific function in my humble opinion.

hi all

having spent a large amount of time studing the ways to print using php I have finaly figured out the best solution. I have decided NOT to use the text file, but to print directly from a query to the mysql database. the function printer_draw_text is the way forward which can be used multiple times in one page.

This question has already been answered. Start a new discussion instead.