3

Possible Duplicate:
Show progress for PHP long script

I have a PHP script that generates large PDF document on request. This usually takes about 1-2 minutes, and the only way it lets the user know that it is finished is by sending an email when it the whole process completes.

I would like to have some kind of a progress bar displayed to the user while the file is generated. What is the best way to approach this task? The only thing that comes to my mind is that the PDF-generating script should save data in the database (I already use MySQL in the project), and on the client-side I could include Ajax request that would poll for this progress data say every 5 seconds and display it to the user. So probably there would be two Ajax requests: one to launch the PDF-generating script and another one for the user to see the progress.

I think I can do it this way, but is there any standard way that I maybe don't know of?

Community
  • 1
  • 1
parrker9
  • 958
  • 14
  • 23

3 Answers3

4

Your proposed approach:

...is probably the easiest and it also walks on known grounds so it's something you'll handle and debug easily.

To it i can only add that you could use a fork/exec/passthrough instead of the ajax call that initiates the PDF generation.

A more elegant (to be read: complicated) approach:

PHP Gearman is a stand alone server you can run on the machine which has a PHP communication library.

You can have the Gearman daemon running, give it a task to work on and write data to the database for the progress bar. The advantage of this setup is that fork/exec/passthrough are expensive operations - Gearman being a daemon is already on so it handles the exec call to the PHP worker and your triggering script will need to work for a shorter time.

I'm not sure if it has a communication API to ask the Gearman about the state of a certain worker and thus avoid adding a database to the cocktail.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
  • This is a really interesting way to try solving this problem. I will definitely look into it, thank you! – parrker9 Aug 02 '12 at 16:52
  • I have the same problem as @parrker9 , but most hosts don't allow fork/exec/passthrough/system.. and it's not possible to install Gearman.. is there another way? or am I wrong about hosts? – iguider Jun 12 '13 at 21:39
  • If you're in a shared hosting then fork/exec/passthrough won't be available for security reasons. Gearman is a "system wide" service, they can't install it just for you because they can't divide the service it provides and allow others to use it equally. Get a VPS for that. – Mihai Stancu Jun 13 '13 at 10:57
2

With a process that takes so long to complete, I'd say your suggestion using ajax to poll the database is probably the simplest way to get the job done.

The only other alternative I see is if your initial request (the actual PDF generation script) generates unbuffered output that you can pipe directly back into the browser. But this would be limited by what you can echo out from PHP, and you'd also have to worry about browser/webserver timeouts.

Chris Tonkinson
  • 13,823
  • 14
  • 58
  • 90
  • Nicely put. Can you please explain some of the downsides of Long Polling? (the second solution) – Adi Aug 02 '12 at 16:10
  • Long-polling without declaring the size of the "finished" file makes the default progress bar of the browser-download not work. Knowing/calculating the size of the finished file according to the raw data you put into it is inexact. – Mihai Stancu Aug 02 '12 at 16:18
  • @MihaiStancu I don't believe the goal here is to make use of the browsers download progress bar, unless I've misunderstood OP - but you're right to warn against downloads of "unknown" size. – Chris Tonkinson Aug 02 '12 at 19:27
0

Without saving the content of pdf in database you can't show a progress bar. I suggest why not use a processing image and tell user processing.

ARIF MAHMUD RANA
  • 5,026
  • 3
  • 31
  • 58
  • You don't need to save the content of the PDF to database, just the current size of the file for example or if you're iterating through a list of values to be pushed into the PDF you can just write the current iteration number divided by the count of total iterations and voila percent of completeness. – Mihai Stancu Aug 02 '12 at 16:16
  • @MihaiStancu yes, I am iterating through a list of items, generating several charts for each of them as well as showing a several tables for each. To be honest, it took some time to get that work okay, now the time to get it more user friendly. – parrker9 Aug 02 '12 at 16:50