Question

I have to generate some reports of various file type(excel, pdf, png), based on the inputs of a web application. The application is written in PHP on a apache webserver.

ATM, when users visit the reports section, they trigger the event which checks if there are submitted new data in the database and based on that info, the new report files are generated. This makes the user to wait until the files are generated (3-10seconds), what is not a good approach at all so I need any advice you can give me.

Was it helpful?

Solution

Of course it really depends on your application and your system, but typically what I do is I have a cron job that executes a PHP script to generate reports periodically. The output could be saved to the filesystem or in a database. This way my reports are only generated once (rather than each time a user tries to download them) AND they are only generated when needed (your script could check if it needs to generate a report based on whatever change criteria you have, or you could just set it up to generate whole new reports periodically). To keep it really simple just call your current web page with wget or curl, and create a new page on your webserver for downloading the saved reports.

The only problem with this approach is users might download "stale" reports (data has changed but the reports haven't been updated). If this is really an issue another approach would be to keep a PHP script running in the background checking for modified data and generating reports as needed.

Another approach is you could just continue generating reports when your users attempt to download them, but cache the results. Again using your own application-specific criteria you would check if the cached report is good enough for immediate download, or generate a new report and cache it. You could even combine this with the first approach by periodically generating new reports, but if a user attempts to download a report that is out of date immediately generate a new one (making the user wait 3-10 seconds).

Ultimately it depends on your system and your application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top