Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theblack68's avatar

Laravel Dompdf

Hi!

I have set a jobs for save 600 pdf. In a foreach I generate a pdf and I save it in storage. All the procedure works fine but some times I get the error "allowed memory size..."

This is my code:

Job


class ProcessPdf implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $associate;

    /**
     * Create a new job instance.
     *
     * @param Associate $associate
     */
    public function __construct(Associate $associate)
    {
        $this->associate = $associate;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $path = storage_path('app/downloads/pdf/' . $this->associate->ragione_sociale . '_' . $this->associate->codice_contribuente . '.pdf');
        PDF::loadView('prints.print', ['associate' => $this->associate])->save($path);
    }
}

This is the controller:


ini_set('memory_limit', '300M');
        foreach($associates as $associate) {
            ProcessPdf::dispatch($associate);
        }
        return redirect('associates')->with('ok_message', 'Il processo di Generazione PDF è iniziato.');

Someone can give me some advise for a best pratic?

Thanks

0 likes
11 replies
DDSameera's avatar

@theblack68 , if you are using WAMP ,please follow up this steps

(left click ) wampmanager icon -> PHP -> php.ini

you can change it without having any issues

laracoft's avatar

@theblack68

  1. There are only 2 ways out of this, optimize the code to use less memory or increase the memory
  2. A better place to put such long running tasks is a command/job, a web controller's method is not a good place
  3. Once in a command/job, I will increase the memory limit higher, either 512 or unlimited ini_set('memory_limit', -1);.
theblack68's avatar

Hi laracroft ...

A better place to put such long running tasks is a command/job, a web controller's method is not a good place...

Would you mind helping me with some advice for this task?

Thanks a lot

theblack68's avatar

I didn't understand how to do without the controller.

I think you intend to set a cron at a time of day?

In case what changes in terms of memory limits?

...because I have set ini_set('memory_limit', -1) but I get the same error.

After about 170 record processed I get the error ;(

theblack68's avatar

Thanks laracroft ...I try now.

I hope don't receive the error anyway ;(

laracoft's avatar
laracoft
Best Answer
Level 27

@theblack68

If you still get memory issues, you have to increase the memory in the job

    public function handle()
    {
        ini_set('memory_limit', -1);
        $path = storage_path('app/downloads/pdf/' . $this->associate->ragione_sociale . '_' . $this->associate->codice_contribuente . '.pdf');
        PDF::loadView('prints.print', ['associate' => $this->associate])->save($path);
    }
theblack68's avatar

Thanks laracroft ...I have finish now to test it ;)

All works right with the memory_limit in the job.

Great ;)

Please or to participate in this conversation.