简体   繁体   English

如何在 Laravel 的电子邮件中使用 DOMPDF 附加 PDF

[英]How to attach a PDF using DOMPDF in email in laravel

Hi i am trying to attach a pdf directly to an email without storing to storage using laravel mail function and sending it to the user.嗨,我正在尝试将 pdf 直接附加到电子邮件,而不是使用 laravel 邮件功能存储到存储中并将其发送给用户。 Everything is working fine as expected but the problem is the pdf instead of an attachment is coming as base64 encoded email content.一切都按预期工作正常,但问题是 pdf 而不是附件作为 base64 编码的电子邮件内容出现。

Can anyone please tell me what i am doing wrong?谁能告诉我我做错了什么?

Here is my code .这是我的代码。 This is inside mailer这是内部邮件

$subject ='Hello! Your invoice is here';

        $data['invoice_id']=$this->content['invoice_id'];

        $pdf = PDF::loadView('frontend.pages.invoice.invoice',$data);


        return $this->from('info@examplecompany.com')
            ->subject($subject)
            ->view('backend.emailtemplates.invoiceEmail')
            ->attachData($pdf->output(),'name.pdf', [
                'mime' => 'application/pdf',
            ]) ;

I am following this How to attach a PDF using barryvdh/laravel-dompdf into an email in Laravel thread, even though i did the same way as its answer suggest , still not working.我正在关注如何使用 barryvdh/laravel-dompdf 将 PDF 附加到 Laravel线程中的电子邮件中,尽管我按照其答案建议的方式进行了操作,但仍然无法正常工作。

Can anyone please help me on this.任何人都可以帮我解决这个问题。 Thank you.谢谢你。

Recommend to use this package for pdf.推荐使用此包为 pdf。

https://github.com/niklasravnsborg/laravel-pdf https://github.com/niklasravnsborg/laravel-pdf

you can easily manage your pdf export:您可以轻松管理您的 pdf 导出:

  • output(): Outputs the PDF as a string. output():将 PDF 输出为字符串。
  • save($filename): Save the PDF to a file save($filename): 将 PDF 保存到文件
  • download($filename): Make the PDF downloadable by the user. download($filename):使用户可以下载 PDF。
  • stream($filename): Return a response with the PDF to show in the browser. stream($filename):返回带有 PDF 的响应以显示在浏览器中。

I think you want to send pdf as an attachment in your email without saving it in your system .我认为您想将 pdf 作为附件发送到您的电子邮件中而不将其保存在您的系统中。

your can refer the following code to do this .您可以参考以下代码来执行此操作。

 $data["email"]='useremail@gmail.com';
            $data["client_name"]='user_name';
            $data["subject"]='sending pdf as attachment';

            //Generating PDF with all the post details

            $pdf = PDF::loadView('userdata');  // this view file will be converted to PDF

            //Sending Mail to the corresponding user

            Mail::send([], $data, function($message)use($data,$pdf) {
            $message->to($data["email"], $data["client_name"])
            ->subject($data["subject"])
            ->attachData($pdf->output(), 'Your_Post_Detail.pdf', [
                       'mime' => 'application/pdf',
                   ])
            ->setBody('Hi, welcome user! this is the body of the mail');
            });

hope it helped you .希望对你有帮助。

for better understanding you can follow this article为了更好地理解你可以按照这篇文章

how to send pdf as an attachment in email in laravel 如何在laravel的电子邮件中将pdf作为附件发送

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

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