将通过Laravel生成的PDF附加到Mailable

时间:2017-12-19 06:16:11

标签: laravel pdf laravel-5 email-attachments

目前我正在使用laravel-snappy从页面生成我的PDF,我之所以这样做主要是因为它对我生成的一些页面上的一些JavaScript脚本起到了很好的作用。

我知道可以将附件附加到mailable上,这是我在其他人的建议中使用的内容,直到知识分子得到更多的东西与他们合作。

但我不知道是否可以将生成的PDF附加到mailable。

目前,我发送PDF以通过路线生成:

Route::get('/shipments/download/{shipment}','ShipmentController@downloadPDF');

然后在控制器中将其发送到此处:

  public function downloadPDF(Shipment $shipment){
            $shipment_details = $shipment->shipment_details;

            $pdf = PDF::loadView('shipments.pdf', compact('shipment','shipment_details'))
                    ->setOption('images', true)
                    ->setOption('enable-javascript', true)
                    ->setOption('javascript-delay', 100);
            return $pdf->download('shipment'.$shipment->url_string.'.pdf');
        }

我的mailable定义如下:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

use App\Shipment;

class newFreightBill extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $shipment;

    public function __construct(Shipment $shipment)
    {
        $this->shipment = $shipment;
        $this->attachmentURL = $shipment->url_string;
        $this->proNumber = $shipment->pro_number;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('billing@cmxtrucking.com')
                    ->subject('New Freight Bill Created - '. $this->proNumber)
                    ->view('emails.shipments.created');
    }
}

那么,是否可以附加生成的PDF?

2 个答案:

答案 0 :(得分:1)

您可以使用附件通过邮件发送文件。例如: -

About to start

这在我努力的时候适合我。希望它也适合你。

答案 1 :(得分:0)

我在这里使用laravel package&#34; vsmoraes / laravel-pdf&#34;用于pdf生成

步骤:

1)使用动态数据渲染视图

2)将此视图加载到pdf

3)将pdf数据附加到邮件

$html = view('pdf',compact('result','score'))->render();
$pdf =  PDF::Load($html);

\Mail::send('mail.analysis',['user'=>$data],function($message) use($pdf,$user){
   $message->to($user->email)->subject("Page Speed Aanlysis Generated");
   $message->attachData($pdf->output(),'pdf_name.pdf');
});