• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text align when creating fwrite()

#1
[eluser]rochellecanale[/eluser]
Hello guys.. actually this is not a CI problem but I am using CI as my framework in building my system. I created a POS system that generates a receipt using a line printer. But when I printed my result all the text are all right align. My problem is how can I align my text? I used fwrite function in PHP and after that i put the result in notepad. Then after that i have a code that calls the last added file (receipt.txt) in the folder named receipt. Here's my sample code I hope you can help me.

Code:
//CREATE TXT FILE FOR RECEIPT PRINTING
        
        $date = date('YmdHis');
        
        $myFile = "c:/receipt/receipt".$date.".txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        
        $stringData = "Thank you for transacting at ".PHP_EOL."High5 Cellular Tech Solutions".PHP_EOL;
        fwrite($fh, $stringData);
        
        $stringData = date('M d, Y H:i:s A').PHP_EOL;
        fwrite($fh, $stringData);
        
        $stringData = "Cashier on duty: ".$this->session->userdata('username').PHP_EOL;
        fwrite($fh, $stringData);
        
        $stringData = "".PHP_EOL;
        fwrite($fh, $stringData);
        
        $stringData = "RECEIPT NO. ".$this->input->post('code').PHP_EOL;
        fwrite($fh, $stringData);
        
        $stringData = "".PHP_EOL;
        fwrite($fh, $stringData);
        
        $stringData = "MEMBER'S ID: 00000001".PHP_EOL;
        fwrite($fh, $stringData);
        
        $stringData = "ACCOUNT TYPE: WALK-IN".PHP_EOL;
        fwrite($fh, $stringData);
        
        $stringData = "".PHP_EOL;
        fwrite($fh, $stringData);
        
        $stringData = "****************************************".PHP_EOL;
        fwrite($fh, $stringData);
        
        foreach($products as $p){
            
            $stringData = "SKU ID: ".$p['id'].PHP_EOL;
            fwrite($fh, $stringData);
            
            $stringData = $p['product_name'].PHP_EOL;
            fwrite($fh, $stringData);
            
            $stringData = "SKU Price: ".$p['product_price'].PHP_EOL;
            fwrite($fh, $stringData);
            
            $stringData = "Quantity: ".$p['product_qty'].PHP_EOL;
            fwrite($fh, $stringData);
            
            $stringData = "Total Purchased: ".number_format($p['product_amount'],2).PHP_EOL;
            fwrite($fh, $stringData);
            
            $stringData = "Payment Method: ".$payment_method.PHP_EOL;
            fwrite($fh, $stringData);
            
            $stringData = "Cash Received: ".number_format($payment,2).PHP_EOL;
            fwrite($fh, $stringData);
            
            $stringData = "Change: ".number_format($change,2).PHP_EOL;
            fwrite($fh, $stringData);
            
            $stringData = "Subtotal: ".number_format($this->session->userdata('subtotal'),2).PHP_EOL;
            fwrite($fh, $stringData);
            
            $stringData = "****************************************".PHP_EOL;
            fwrite($fh, $stringData);

        }
        
        fclose($fh);

#2
[eluser]boltsabre[/eluser]
I don't really have any experience on this, just throwing out some ideas.

What happens when you open that notepad file, rather than printing it, is it left or right aligned?

It sounds more like an OS or notepad or printer setting rather than a php thing. What happens when you print a different notepad file not created by your script, does it print right or left?

On another note, you're doing a huge bunch of fwrites, can't you just create one $stringData, and once it's fully populated fwrite it just the once?

Also, perhaps try creating your string at the start of a new line
Code:
$stringData = "
this is my first line of code, starting at the very left
";

No idea if any of this will help in the slightest, but it's some things to look into.

#3
[eluser]stuartr[/eluser]
[quote author="boltsabre" date="1363866214"]I don't really have any experience on this, just throwing out some ideas.

What happens when you open that notepad file, rather than printing it, is it left or right aligned?

It sounds more like an OS or notepad or printer setting rather than a php thing. What happens when you print a different notepad file not created by your script, does it print right or left?

On another note, you're doing a huge bunch of fwrites, can't you just create one $stringData, and once it's fully populated fwrite it just the once?

Also, perhaps try creating your string at the start of a new line
Code:
$stringData = "
this is my first line of code, starting at the very left
";

No idea if any of this will help in the slightest, but it's some things to look into.[/quote]

I would agree with boltsabre that it is unlikely to be an issue with fwrite.

How and on what are you printing the receipt?

#4
[eluser]InsiteFX[/eluser]
you need to send it the printer command codes to format your print out. They should be in your printer manual.

#5
[eluser]rochellecanale[/eluser]
In notepad the text are all left align but when i print this on my line printer all the text are all right align. But when i decreases the font size of the text manually (in notepad) it centered the text. I used the exec() function to send the line of text in the printer. Here's my sample exec() code.

Code:
$printer = "CSO_PRINTER";  //printer name
exec('NOTEPAD.EXE /PT "'.$myFile.'" "'.$printer.'"');

#6
[eluser]stuartr[/eluser]
[quote author="rochellecanale" date="1363919576"]In notepad the text are all left align but when i print this on my line printer all the text are all right align. But when i decreases the font size of the text manually (in notepad) it centered the text. I used the exec() function to send the line of text in the printer. Here's my sample exec() code.

Code:
$printer = "CSO_PRINTER";  //printer name
exec('NOTEPAD.EXE /PT "'.$myFile.'" "'.$printer.'"');
[/quote]

Line printers are notorious for printing issues - as a previous poster has mentioned you need to find the printer control codes for the device in question, and send these to the printer at the start of the print run.

Depending what else the printer is used for, it may be necessary to send an end of job set of codes as well to return it to its previous state. So once you have got your receipt printing correctly, double check any other prints that use it. I have seen many occasions where sending control codes sort out one problem and cause another.

#7
[eluser]TheFuzzy0ne[/eluser]
I know this isn't necessarily the way you want to do things, but can't you just render the page in HTML, and have a print button you can click? Then you can use CSS to format the data for the printer, and print it out as you want. You wouldn't need to use php_printer.dll, and you have a lot more control over how things are formatted.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.