【问题标题】:Laravel 5 generate invoice & send it by email in attachment with mpdfLaravel 5 生成发票并通过电子邮件将其与 mpdf 一起发送
【发布时间】:2023-01-07 17:27:25
【问题描述】:

我正在运行 Laravel 5 项目,我在 App->Mail->SendEmail 中创建了一个控制器,通过 mpdf 发送带有附加系统生成发票的电子邮件,使用以下代码生成发票,但在发送之前在我的浏览器中下载,并且当将输出更改为 s 时,出现错误“在 null 上调用成员函数 output()”

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Order $order, $data, $mpdf)
    {
        $mpdf = new \Mpdf\Mpdf();
        $mpdf->WriteHTML(view('invoices.paid_invoice', compact('order', 'data')));
        $fileName = 'Invoice-'.$order->id.'.pdf';
        $mpdf->Output($fileName,"D");
        $this->order = $order;
    }
 
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.send_order')
        ->subject(('my subject'))
        ->attachData($this->mpdf->output(), 'invoice.pdf', [
            'mime' => 'application/pdf',])
        ;
}}

【问题讨论】:

    标签: php laravel mpdf


    【解决方案1】:

    在您的代码中,$this->mpdf 没有引用任何内容。

    创建文件后,您不再需要使用 MDF,因此只需保存文件名并在构建函数中重用它

    private $filename;
    
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Order $order, $data, $mpdf)
    {
        $mpdf = new MpdfMpdf();
        $mpdf->WriteHTML(view('invoices.paid_invoice', compact('order', 'data')));
        $this->filename = 'Invoice-'.$order->id.'.pdf';
        $mpdf->Output($this->filename,"D");
        $this->order = $order;
    }
    
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.send_order')
            ->subject(('my subject'))
            ->attach($this->filename, [
                'as' => 'invoice.pdf',
                'mime' => 'application/pdf',
            ]);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 2011-04-24
      • 2019-03-08
      • 1970-01-01
      • 2020-04-17
      • 2018-03-21
      相关资源
      最近更新 更多