V
V
Vadim2017-02-06 19:25:40
symfony
Vadim, 2017-02-06 19:25:40

How to generate a relatively long report?

Good afternoon.
So - I form an Excel file on the server and immediately send it to the browser. Now the requests are not heavy, the file is small and it works. But sooner or later the wait will exceed the timeout.
This is the question - how to generate such reports correctly?
Now the project is on a symphony, but I have already done this before - as I understood it myself. That's how it was.
By clicking on the link, he sent a request for formation, where the path was written to the session where the generated file would be saved and sent requests until such a file appeared, as it appeared, he gave the file.

<a href="/" class="download-report">
    <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>
    Загрузить таблицу
</a>

$('.download-report').click(function (e) {
    e.preventDefault();
    waitingDialog.show();
    $.get('start_gnerate_programs_info_report');
    var download_interval = setInterval(function () {
        $.get('is_programs_info_report_ready', function (is_programs_info_report_ready) {
            if (is_programs_info_report_ready == 'yes') {
                waitingDialog.hide();
                clearInterval(download_interval);
                location.href = 'get_programs_info_report';
            } else if (is_programs_info_report_ready != 'no') {
                clearInterval(download_interval);
                waitingDialog.hide();
            }
        });
    }, 3000);
});

class ProgramsInfoReportController
{
    /**
     * @param PDO $PDO
     */
    public function startGenerateProgramsInfoReportAction(PDO $PDO)
    {
        $generation_start_time = time();
        $file_location = $_SERVER['DOCUMENT_ROOT'] . '/tmp/' . $generation_start_time . '.xlsx';
        $_SESSION['programs_info_report'] = $file_location;
        session_write_close();
...
        $this->generateExcelFile($programs_info, $file_location);
    }

    private function generateExcelFile($programs_info, $file_location)
    {
...
        $writer->save($file_location);
    }

    public function isProgramsInfoReportReadyAction()
    {
        if (isset($_SESSION['programs_info_report'])) {
            if (file_exists($_SESSION['programs_info_report'])) {
                echo 'yes';
            } else {
                echo 'no';
            }
        } else {
            echo 'error';
        }
    }

    public function getProgramsInfoReportAction()
    {
        $file_name = 'programs_info.xlsx';

        if (isset($_SESSION['programs_info_report']) && (file_exists($_SESSION['programs_info_report']))) {
            $file_location = $_SESSION['programs_info_report'];
            header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            header("Content-Length:" . filesize($file_location));
            header("Content-Disposition: attachment; filename=$file_name");
            header('Pragma: no-cache');
            readfile($file_location);
            unlink($file_location);
            unset($_SESSION['programs_info_report']);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2017-02-06
@kylt_lichnosti

everything is as it was done + a link to the mail / to the memerzone
or pre-generation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question