使用laravel-dompdf在邮件上发送pdf而不保存在服务器上

时间:2018-01-18 08:21:45

标签: php laravel email pdf

我想使用laravel-dompdf在邮件上发送pdf而不保存在服务器上

我有下一个代码:

$pdf = PDF::loadView('admin.costs.pdf',array('costscategories' => $costscategories,'marketcosts' => $marketcosts,'bonuscosts' => $bonuscosts));
Mail::send('admin.costs.test', $pdf, function($message)
{
    $message->to('test@test.test', 'Jon Doe')->subject('Welcome!');
    $message->attachData($pdf, 'invoice.pdf');
});
}

但我得到了下一个错误:

Type error: Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, object given, called in /var/www/html/miltonia-update/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 221

如何解决?

3 个答案:

答案 0 :(得分:3)

enter image description here接受second argument作为您希望传递给array的{​​{1}}数据。将您的view包裹为$pdf数据。

array

答案 1 :(得分:1)

如错误所示,您需要将array传递给Mail::send函数,但您只是传递$pdf这是一个对象而不是数组。

这应该有效:

$data = [];

$pdf = PDF::loadView('admin.costs.pdf',array('costscategories' => $costscategories,'marketcosts' => $marketcosts,'bonuscosts' => $bonuscosts));

$data['pdf'] = $pdf;

Mail::send('admin.costs.test', $data, function($message)
{
    $message->to('test@test.test', 'Jon Doe')->subject('Welcome!');
    $message->attachData($data['pdf'], 'invoice.pdf');
});
}

答案 2 :(得分:0)

谢谢大家!我用下面的想法解决了这个问题:

$pdf = PDF::loadView('admin.costs.pdf',array('costscategories' => $costscategories,'marketcosts' => $marketcosts,'bonuscosts' => $bonuscosts))->stream();

Mail::send('admin.costs.test', ['pdf'=>$pdf], function($message) use ($pdf)
{
   $message->to('test@test.test', 'Jon Doe')->subject('Welcome!');
   $message->attachData($pdf, 'invoice.pdf');
});