webapp中生成pdf、xls、png文件的正确方法
我必须根据 Web 应用程序的输入生成一些各种文件类型(excel、pdf、png)的报告。该应用程序是在 Apache Web 服务器上用 PHP 编写的。
ATM,当用户访问报告部分时,他们会触发事件,检查数据库中是否有提交的新数据,并根据该信息生成新的报告文件。这使得用户需要等待文件生成(3-10 秒),这根本不是一个好方法,所以我需要您能给我的任何建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,这实际上取决于您的应用程序和系统,但通常我所做的是有一个 cron 作业,它执行 PHP 脚本来定期生成报告。输出可以保存到文件系统或数据库中。这样,我的报告仅生成一次(而不是每次用户尝试下载它们时),并且仅在需要时生成它们(您的脚本可以检查是否需要根据您拥有的任何更改标准生成报告,或者您可以只需将其设置为定期生成全新的报告即可)。为了保持简单,只需使用 wget 或 curl 调用当前网页,并在网络服务器上创建一个新页面以下载保存的报告。
这种方法的唯一问题是用户可能会下载“过时的”报告(数据已更改但报告尚未更新)。如果这确实是一个问题,另一种方法是让 PHP 脚本在后台运行,检查修改的数据并根据需要生成报告。
另一种方法是,当用户尝试下载报告时,您可以继续生成报告,但缓存结果。再次使用您自己的特定于应用程序的标准,您将检查缓存的报告是否足以立即下载,或生成新报告并缓存它。您甚至可以将其与第一种方法结合起来,定期生成新报告,但如果用户尝试下载过期的报告,则立即生成新报告(使用户等待 3-10 秒)。
最终这取决于您的系统和应用程序。
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.