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->attachfileNameis your DomPDF instance.- Calling
output()on it returns the raw PDF data as a string, which is whatAttachment::fromDataexpects.
Summary:
Change your attachments() method to use $this->attachfileName->output() inside the closure.
Let me know if you need more help!