简体   繁体   English

在 Laravel 中用 DOMPDF 将 PDF 文件附加到 email

[英]Attach PDF file to email with DOMPDF in Laravel

I'm using my MyEmail model to attach a PDF file to it.我正在使用我的 MyEmail model 将一个 PDF 文件附加到它。 The mail gets sent with all the data I want and a PDF gets attached but it is empty, how can I import my layout and data into the PDF if I attach?邮件中包含我想要的所有数据,并附加了一个 PDF,但它是空的,如果我附加,如何将我的布局和数据导入 PDF?

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Course;
use Barryvdh\DomPDF\PDF;

class MyEmail extends Mailable {
    use Queueable, SerializesModels;
    public $data;
    public $course;
    private $pdf;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data, Course $course) {
        $this->course = $course;
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build() {
        return $this->subject('Thanks')->view('emails.my_email')->attachData($this->pdf, 'my_pdf.pdf')->with(['data' => $this->data, 'course' => $this->course]);
    }
}

Here's the function to send the email my controller:这是 function 发送 email 我的 controller:

public function sendMail(Request $request) {

    $data = array(
        'first_name'        => $request->first_name,
        'last_name'         => $request->last_name,
        'id'                => $request->id,
        'course_id'         => $request->course_id,
    );

    $resultJson = $this->validateRecaptcha($request);

    $course= Course::where('id', $data['course_id'])->first();

    if($resultJson->success != true) :
        return redirect()->back()->with(['success' => 'success']);
    else :
        \Mail::to($request->email)
             ->cc('my@*******')
             ->queue(
            new Mail\MyEmail($data, $course)
        );
        return redirect()->back()->with(['success' => 'Success!']);
    endif;
    
}

The PDF also requires a view, like so: PDF 也需要一个视图,如下所示:

// creating the pfd:
$pdf = PDF::loadView('pdf.multiple_pages', [
            'pages' => $this->pages,
            'title' => $this->options->get('documentName'),
        ]);

Where the view looks like this:视图看起来像这样:

<?php

$imgPath = public_path('store/assets/images/new_logo.png');
$img = base64_encode(file_get_contents($imgPath));
?>
<html lang="{{ app()->getLocale() }}">
<head>
    <title>{{ $title ?? 'document' }}</title>

</head>
<body>
<style>
    @php
        include(public_path('/css/pdf/multiple_pages.css'))
    @endphp

    div.page {
        background-image: url("data:image/png;base64, {{ $img }}");
    }
</style>
@foreach($pages as $page)
    <div class="page">
        {!! html_entity_decode($page) !!}
    </div>
@endforeach
</body>
</html>

So i recommend first creating the PDF, and only then attaching it to the email所以我建议首先创建 PDF,然后才将其附加到 email

Keep in mind this is very specific to our own use case and you will need to modify this.请记住,这是非常针对我们自己的用例的,您将需要对其进行修改。

If you are using barryvdh/laravel-dompdf package, you can simply use loadView() function, that takes your .blade file and the data you want.如果您使用的是barryvdh/laravel- dompdf package,您可以简单地使用loadView() function,它获取您的.blade文件和您想要的数据。 Inside that blade, you will generate PDF layout and inject your variables.在该刀片内,您将生成 PDF 布局并注入您的变量。 Simple example would be something like this:简单的例子是这样的:

$name = 'John';
$pdf = PDF::loadView('your_view', $name);

You can now use this $name variable in your pdf.blade file:您现在可以在 pdf.blade 文件中使用此$name变量:

<h1>Hi {{name}}</h1>

After you generate the pdf with desired layout and data, you can attach it to Mail class like this:生成具有所需布局和数据的 pdf 后,您可以将其附加到邮件 class,如下所示:

->attachData($pdf->output(), 'your_file_name');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM