Welcome Guest, Not a member yet? Register   Sign In
Error in DOWNLOAD HELPER
#1

[eluser]rochellecanale[/eluser]
Hello guys I have a little problem here about download helper. My problem is after i created a file that can generate a POS receipt. And I used a download helper. The sequence is:

1. Get details about purchased products (done automatically)
2. Save it in notepad inside my receipt folder (done automatically)
3. After saving there is a button named "PRINT RECEIPT" (need to click the button for printing)
4. Click the button and it will automatically call the
Code:
file_get_contents()
function for downloading.
5. After downloading the next line in my code is an exec() function that calls the printer
6. After the exec() function the printer should print the desired file.

Here's my sample code:
Code:
//receipt.php

$date = date('YmdHis');
        
        $myFile = "c:/receipt/receipt".$date.".txt";
        $sec_file = "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);
.
.
.
.
fclose($fh);
        
        echo "<tr>";
            echo "<td colspan='2'>";
                echo form_open('sales_controller/testprint');
                    echo "&lt;input type='hidden' name='url' value = {$sec_file}&gt;";
                    echo "&lt;input type='submit' value='PRINT RECEIPT' /&gt;";    // the button
                echo form_close();
            echo "</td>";
        echo "</tr>";


//my controller function
public function testprint(){
            $data['url'] = $this->input->post('url');
            $this->load->view('sites/testprint123',$data);
}

//testprint123.php

$code = "PRINT-".date('YmdhisA').".txt";
    
    $data2 = file_get_contents("c:/receipt/".$url); // Read the file's contents
    $name2 = $code;
    
    force_download($name2, $data2);  //after this function it can't proceed to the next line

   //not running
    $directory = "C:/Documents and Settings/New user/My Documents/Downloads/".$code;
    $printer = "CSO_PRINTER";
    exec('NOTEPAD.EXE /PT "'.$directory.'" "'.$printer.'"');

My problem is in testprint123.php, after the download it can't proceed to the next line or next code. That's why I can't print the receipt.
#2

[eluser]TheFuzzy0ne[/eluser]
That's not a bug. force_download() should be the last thing called in your script, as the function end with exit(). This is to prevent any further output (from the profiler, or from an error/warning), as it will mess up the downloaded file.
#3

[eluser]rochellecanale[/eluser]
So how can i execute my last code?
#4

[eluser]TheFuzzy0ne[/eluser]
I'm not entirely sure, to be honest. This might work.

Code:
$directory = 'C:/Documents and Settings/New user/My Documents/Downloads/'.$code;
$printer = 'CSO_PRINTER';
exec("NOTEPAD.EXE /PT $directory $printer &"); // Note the ampersand.

force_download($name2, $data2);

Alternatively, you might want to override the force_download() function, so it echoes, rather than exits, but you might run into problems.
#5

[eluser]WanWizard[/eluser]
It would probably be easier to generate the receipt in HTML, and have the browser print it? Or do you need to print it server side?
#6

[eluser]rochellecanale[/eluser]
What do you mean print in browser? You mean that save as PDF and then print?
#7

[eluser]rochellecanale[/eluser]
Mr. TheFuzzy0ne,

I also tried that but there's an error. It couldn't find the directory. Here's the pop up message alert.
Code:
cannot find the c:/documents and settings/new user/my documents/downloads/print-20130403103510AM.txt file.
After you click the button it will download the file.
#8

[eluser]TheFuzzy0ne[/eluser]
I wonder if that's because the file is in use? It might be worth copying it to a temporary directory (C:\Temp) and printing from that, then you can force_download() the one in C:\documents and settings\new user\my documents\downloads\




Theme © iAndrew 2016 - Forum software by © MyBB