Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SiNi_Si's avatar

laravel 9 Mailable with Attachment::fromData

I'm getting an error, the mailler thinks it a string

Object of class Barryvdh\DomPDF\PDF could not be converted to string

I really don't want to be saving the pdf to the server, I want to make one and just send. Any thoughts on this would be a great help

My Controller.. [code] $data = [ 'id' => $Transactions->id, 'Who_1' => $Transactions->shipping_address, 'row_item' => $lineitem, 'amount' => $Transactions->amount, 'The_Date' => \Carbon\Carbon::parse($Transactions->created_at)->format('d/m/Y'),

            'total'       => $Transactions->amount,
            'vat'         => request('b_vat'),
            'grand_total' => $Transactions->total,
        ];

        //return view('users.pdf', $data);

        $pdf_Data    = PDF::loadView('frontend.shared.pdf', $data);

        Mail::to(request('b_email'))->send(new \App\Mail\UserOrderVouchers(request('b_f_name'), request('b_email'), $company->email, $subject, $body, $company->name, $pdf_Data));

[/code]

My Mailable looks like... namespace App\Mail;

use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; use Illuminate\Mail\Mailables\Attachment; use Illuminate\Mail\Mailables\Address;

class UserOrderVouchers extends Mailable{ use Queueable, SerializesModels;

public $name, $email, $company_email, $subject, $body, $company_name, $attachfileName;
public function __construct($name, $email, $company_email, $subject, $body, $company_name, $attachfileName){
    $this->name = $name;
    $this->email = $email;
    $this->company_email = $company_email;
    $this->subject = $subject;
    $this->body = $body;
    $this->company_name = $company_name;
    $this->attachfileName = $attachfileName;
}


public function envelope(){
    return new Envelope(
        from: new Address($this->company_email, $this->company_name),
        subject: 'Order Vouchers',
        tags: ['Order Vouchers'],
    );
}


public function content(){
    return new Content(
        html: 'emails.user_vouchers_email',
    );
}

public function attachments(){
    return [
        Attachment::fromData(fn () => $this->attachfileName, 'Report.pdf')
            ->withMime('application/pdf'),
    ];
}

}

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

You're very close! The issue is that you're passing the PDF object ($pdf_Data) directly to Attachment::fromData, but Attachment::fromData expects a callable that returns the raw file data (string), not the PDF object itself.

You need to return the PDF content as a string using the output() method of the DomPDF instance.

Here's how you can fix it:

In your Controller:

$pdf_Data = PDF::loadView('frontend.shared.pdf', $data);

Mail::to(request('b_email'))->send(
    new \App\Mail\UserOrderVouchers(
        request('b_f_name'),
        request('b_email'),
        $company->email,
        $subject,
        $body,
        $company->name,
        $pdf_Data // still pass the PDF object
    )
);

In your Mailable:

public function attachments()
{
    return [
        Attachment::fromData(
            fn () => $this->attachfileName->output(), // <-- call output() here
            'Report.pdf'
        )->withMime('application/pdf'),
    ];
}

Explanation:

  • $this->attachfileName is your DomPDF instance.
  • Calling output() on it returns the raw PDF data as a string, which is what Attachment::fromData expects.

Summary:
Change your attachments() method to use $this->attachfileName->output() inside the closure.

Let me know if you need more help!

1 like
SiNi_Si's avatar

that fixed it, thanks AI

$this->attachfileName->output(),

Please or to participate in this conversation.