You can let the printer do all the stuff, but I think it is a better idea to implement some knowledge about the printer characteristics into the output routine. You should know how many characters per line can be printed.
You could then use sprintf()
to format the entire line, with the product info aligned to the left and with a maximum field size, and the price aligned to the right.
$texttoprint .= sprintf('%s %f', $article, $price); // very basic start: A string and a float
$texttoprint .= sprintf('%-30.30s %8.2f', $article, $price);
// 30 chars for the string, will not exceed this length, and 8 chars for the price, with decimal dot and 2 decimal digits.
Please note that you shouldn't use stripslashes()
on the final result. If you have an issue with slashes being present, they are introduced by "magic quotes" and should be eliminated right at the start, not at the end.
A different solution would be to switch the printer to "Windows" mode and make him need CR/LF for a complete line printout. This way you could print the whole line without moving the paper by printing CR only, which moves the printer head to the start of the line again. You can then print on top of the already printed line again, until you print a LF. Note that if there is an actual printer head involved, this might make things worse, as it might trigger additional head movement just for printing one single line. Printers usually are able to optimize head movement for one single line after another.