将PDF文件附加到Laravel中的DOMPDF的电子邮件

发布于 2025-01-22 12:42:36 字数 1815 浏览 3 评论 0 原文

我正在使用我的肌邮件模型将PDF文件附加到它。邮件随我想要的所有数据发送,并且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]);
    }
}

这是向我的控制器发送电子邮件的功能:

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;
    
}

I'm using my MyEmail model to attach a PDF file to it. 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?

<?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:

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;
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2025-01-29 12:42:36

PDF还需要一个视图,这样的视图:

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

在此视图看起来像这样:

<?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>

因此,我建议先创建PDF,然后将其附加到电子邮件中,

请记住这对我们自己的用例非常具体,您将需要修改此。

The PDF also requires a view, like so:

// 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

Keep in mind this is very specific to our own use case and you will need to modify this.

沙沙粒小 2025-01-29 12:42:36

如果您使用 barryvdh/larryvdh/laravel-dompdf 软件包,您可以简单地使用 loadview()函数,它会 .blade> .blade file and you想要的数据。在该刀片内部,您将生成PDF布局并注入变量。简单的示例就是这样:

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

您现在可以在pdf .blade文件中使用此 $ name 变量:

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

在使用所需的布局和数据生成PDF后,您可以将其附加到邮件类上:

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

If you are using barryvdh/laravel-dompdf package, you can simply use loadView() function, that takes your .blade file and the data you want. Inside that blade, you will generate PDF layout and inject your variables. 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:

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

After you generate the pdf with desired layout and data, you can attach it to Mail class like this:

->attachData($pdf->output(), 'your_file_name');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文