Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Symfony, Is there any way to create pdf file pages at a time?

I have Symfony framework with TWIG template engine. I am making 1000 page pdf within

<dynamic-page>...content in for-loop...</dynamic-page>`

However while writing onto disk with ps_facade, it gives me memory exhausted fatal error. So is there any ways I can create this pdf by 5 pages a chunk?

After research I found that using template with 5 page worth of data and then write it on file should work. But in that way I won't be able to add page numbers, as page number should be 1-1000. My footer code looks like this,

<placeholders>
    <footer>
        <div height="30px" width="100%">
        <hr/>
        <div float="left">Blah Blah</div>
        <div float="left" margin-left="350px"><page-info format="Page %s of %s"></div>
        </div>
    </footer>
</placeholders>
like image 495
TeaCupApp Avatar asked Oct 08 '12 22:10

TeaCupApp


1 Answers

You should use a messaging system for long processes like this. RabbitMQ could do the job.

  1. Your user requests for the pdf
  2. You tell him instantly that his request has been taken into account, and that he will receive an email when the job is done, or that he can come back later
  3. A consumer sees the job, and launches the pdf generation (does not have to be in php).

Pros: Since the process is not run by the web version of php, it does not have the memory_limit and max_execution time limitation If the job is already running, you can tell your user so instead of launching another generation for the same pdf. And since people often refresh the page when they get impatient, this can really be a huge Pro for your server (less CPU usage).

Cons: You'll have to setup a messaging server and learn a few things. But is this really a Con?

like image 175
greg0ire Avatar answered Oct 22 '22 11:10

greg0ire