phpphpdocx

phpdocx + ob_clean + download = error


I use phpdocx library to make reports in Word. By default, download function contains these lines:

$this->save($tmp_filename);
... many headers
ob_clean();
flush();
readfile($tmp_filename);            
exit;

With ob_clean function nothing happens - nothing is loaded. If I comment out this line, then the file starts to load. But it gets loaded with an error, which appears when I try to open the file. The error message says that the file is corrupt etc. However, if I open the temporary file itself - it is ok. So, what I want to know is what can be wrong with ob_clean function and with the download function on the whole.

EDIT

I replaced ob_clean with ob_end_clean and the problem has gone away.


Solution

  • You don't need to use ob clean or flush; there is a built in function that does that for phpdocx:

    For example:

    require_once '../../classes/CreateDocx.inc';
    $docx = new CreateDocx();
    $text = 'Lorem ipsum dolor sit amet.';
    $docx->addText($text, $paramsText);
    $docx->createDocxAndDownload('example_text');
    

    @ref: http://www.phpdocx.com/api-documentation/layout-and-general/create-and-download-docx-with-PHP