0

Hie everyone,

My scenario : I have a job which sends an email along with a pdf attached. The pdf is generated in the job handle() and attached with the email. To make user experience more friendly i used queue job for sending email. So, all the other emails works fine except when I generate a PDF:loadView. Additionally, the PDF works perfectly fine on the localhost (WAMP) However, it throws the above exception and sits in the failed_jobs table in the database.

Example of my job Class::EmailTestPdf

class EmailTestPdf implements ShouldQueue
{ 
    protected $data;
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(array $data)
{
    $this->data = $data;
}

public function handle()
{
    $d=$this->data;
    $d['doc'] = PDF::loadView('email.test',compact('data','survey')); 
    Mail::send('email.testpdf', $d, function ($message) use ($d)
    {
        $message->from(abc@gmail.com, 'Tester');
        $message->subject('Subject Title');
        $message->to('me@gmail.com');
        $message->attachData($d['doc']->output(), 'test.pdf', ['mime' => 'application/pdf']);
    });
}
}

On the controller method i used :

 EmailTestPdf::dispatch($data)->delay(now()->addSeconds(5));

The above works fine on localhost. However, it dosent work on the live server. Additionally, On the centOS server i implemented supervisor as below:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/coreso5/ptesting/artisan queue:work database --sleep=3 --
tries=3
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/home/coreso5/ptesting/storage/logs/worker.log

After running a test on sending a pdf it will throw an error which I found on the failed_jobs table

ErrorException: Undefined index: SCRIPT_FILENAME in /ptesting/vendor/dompdf/dompdf/src/Css/Stylesheet.php:175

...............

Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined index...', '/ptesting/v...', 175, Array)

miken32
  • 42,008
  • 16
  • 111
  • 154
Deepesh Thapa
  • 1,721
  • 3
  • 19
  • 29

1 Answers1

0

If your job is only sending an email and you are not going to use it elsewhere, you don't need to create a dedicated job, laravel can queue your email.

Queueing-mail

Mail::to(abc@gmail.com)
  ->queue(new EmailTestPdf($data));

Regarding your error it looks like the problem is happening when creating the pdf. Are you sure that it works outside this job? what happens when you do;

$d['doc']->output();
Diogo Gomes
  • 2,135
  • 16
  • 13
  • I mean If is the way domPDF has been implemented. It is looking for a server url of somekind. and I cannot think of getting it right – Deepesh Thapa Dec 16 '18 at 23:07
  • Can you paste the error trace? it should be at `/home/coreso5/ptesting/storage/logs/worker.log` Also, which version of dompdf are you using? can you show the code for this line: `/ptesting/vendor/dompdf/dompdf/src/Css/Stylesheet.php:175` – Diogo Gomes Dec 17 '18 at 09:40
  • Status: 500 Internal Server Error Cache-Control: no-cache, private date: Sun, 16 Dec 2018 12:33:25 GMT Content-type: text/html; charset=UTF-8 – Deepesh Thapa Dec 18 '18 at 14:02
  • `Maximum execution time of 30 seconds` you need to increase the max_execution time for that only for that script add this at the top of the handle method `set_time_limit(0);` or globally https://stackoverflow.com/questions/16171132/how-to-increase-maximum-execution-time-in-php – Diogo Gomes Dec 19 '18 at 12:10
  • Thank you for the reply. But for some reason it was due to the fault in the server handling supervisor. I have a highest execution time. – Deepesh Thapa Dec 19 '18 at 15:51