Search code examples
laravel-8mpdf

Can't load an image in laravel-mpdf with artisan serve


I am trying to load logo image in the pdf file using Laravel Mpdf in Laravel 8. Below is some code which I am using in img tag.

<img style="height: auto; width: 150px;" src="/storage/images/{{ $company_details->company_logo }}" />

The below error comes after a minute for timeout.

Symfony\Component\ErrorHandler\Error\FatalError
Maximum execution time of 60 seconds exceeded 

Without image tag everything is working fine. Pdf file is loading in 2 to 3 secs.

Please let me know if i am missing anything here for loading image file using Laravel Mpdf. Thank you.

Okay I am adding files to it for more clear view of problem.

routes/web.php

<?php
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PdfController;

Route::get('/invoices/pdf', [PdfController::class,'generate_pdf']);

PdfController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\CompanyDetails;
use PDF;

class PdfController extends Controller
{
    
    public function generate_pdf()
    {   
        $pdf = PDF::loadView('invoices.pdf', [
            'company_details' => CompanyDetails::orderBy('company_id', 'asc')->first()
            
        ], [
            'title' => 'PDF Title',
            'author' => 'PDF Author',
            'margin_left' => 20,
            'margin_right' => 20,
            'margin_top' => 40,
            'margin_bottom' => 20,
            'margin_header' => 10,
            'margin_footer' => 10,
            'showImageErrors' => true
        ]);

        return $pdf->stream('pdf-file.pdf');
    }
}

Only problem with it is without image its working fine but with image there is timeout error.


Solution

  • I have solved it by changing below line

    <img style="height: auto; width: 150px;" src="/storage/images/{{ $company_details->company_logo }}" />
    

    to

    <img style="height: auto; width: 150px;" src="storage/images/{{ $company_details->company_logo }}" />
    

    the slash(/) was the problem. I think it was considering it as a HTTP resource.