Skip to content

Instantly share code, notes, and snippets.

@snipe
Last active May 26, 2022 17:46
Show Gist options
  • Save snipe/f4af01244c141c9fd56522541e3ec261 to your computer and use it in GitHub Desktop.
Save snipe/f4af01244c141c9fd56522541e3ec261 to your computer and use it in GitHub Desktop.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use PDF;
// This example uses Laravel 5.5, Cashier 7.0.13, and requires the barryvdh/laravel-dompdf package.
class InvoiceChargeSucceeded extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($invoice, $user)
{
$this->invoice = $invoice;
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$invoice = $this->invoice;
$total = $invoice->total();
$user_name = $this->user->name;
$company_name = $this->user->company;
// Make sure you have barryvdh/laravel-dompdf installed, and have set the PDF facade up in your config/app.php
// facades section, per the laravel-dompdf documentation.
$pdf = PDF::loadview('cashier.receipt', compact('invoice','company_name','user_name', 'total'));
return (new MailMessage)->markdown('mail.account.invoice-succeeded', [
'invoice' => $invoice,
'total' => $total,
'user_name' => $user_name,
'company_name' => $company_name
])
->attachData($pdf->output(), $invoice->id . '-'.date('Y-m-d').'.pdf', ['mime' => 'application/pdf'])
->subject('Payment Processed');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
@snipe
Copy link
Author

snipe commented May 26, 2022

@hmreumann The question was never how to generate a PDF, it was to send the PDF as an attachment in email via notifications. Also, this thread is from 2018. A lot has changed in cashier and stripe since then.

Regardless, Dompdf is what we're calling when we say use PDF, as the installation of Dompdf has you set that as an alias in your app config.

Glad you found a solution that works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment