1. Go to this page and download the library: Download finller/laravel-invoices library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
finller / laravel-invoices example snippets
use Brick\Money\Money;
use Elegantly\Invoices\Collections\InvoiceDiscountCollection;
use Elegantly\Invoices\Collections\InvoiceTaxCollection;
use Elegantly\Invoices\Collections\PdfInvoiceItemCollection;
use Elegantly\Invoices\Enums\InvoiceState;
use Elegantly\Invoices\Enums\InvoiceType;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\InvoiceTax;
use Elegantly\Invoices\Pdf\PdfInvoice;
use Elegantly\Invoices\Pdf\PdfInvoiceItem;
use Elegantly\Invoices\Support\Address;
use Elegantly\Invoices\Support\Identity;
use Elegantly\Invoices\Support\Party;
use Elegantly\Invoices\Support\PaymentInstruction;
use Elegantly\Invoices\Support\TaxId;
$pdfInvoice = new PdfInvoice(
type: InvoiceType::Invoice,
state: InvoiceState::Pending,
serial_number: 'INV-260001',
created_at: now(),
due_at: now()->addDays(30),
fields: ['Order' => 'PO0234'],
seller: new Party(
company: 'Acme Studio',
name: 'Jane Doe',
address: new Address(
street: "Place de l'Opéra",
city: 'Paris',
postal_code: '75009',
country: 'FR',
),
tax_id: new TaxId(country: 'FR', code: '123456789'),
email: '[email protected]',
phone: '+33 1 23 45 67 89',
identities: [new Identity(type: 'SIREN', code: '732829320')],
fields: ['Website' => 'https://example.com'],
),
buyer: new Party(
company: 'Doe Corporation',
name: 'John Doe',
address: new Address(
street: '8405 Old James St',
city: 'New York',
postal_code: '14609',
state: 'NY',
country: 'US',
),
shipping_address: new Address(
street: ['8405 Old James St', 'Apartment 1'],
city: 'New York',
postal_code: '14609',
state: 'NY',
country: 'US',
),
email: '[email protected]',
),
items: new PdfInvoiceItemCollection([
new PdfInvoiceItem(
label: 'Consulting',
unit_price: Money::of('100.00', 'EUR'),
quantity: 2,
quantity_unit: 'hours',
description: 'Application development',
discounts: new InvoiceDiscountCollection([
new InvoiceDiscount(
code: 'WELCOME',
label: 'Welcome offer',
percentage: 10,
),
]),
taxes: new InvoiceTaxCollection([
new InvoiceTax(
type: 'vat',
country: 'FR',
percentage: 20,
label: 'VAT France (20%)',
),
]),
),
]),
description: 'Thank you for your business.',
paymentInstructions: [
new PaymentInstruction(
name: 'Bank transfer',
description: 'Use the invoice number as the payment reference.',
fields: [
'IBAN' => 'GB12ACME12345678123456',
'SWIFT/BIC' => 'ACMEGB2L',
'Reference' => 'INV-260001',
],
),
],
// Optional: logo: public_path('images/logo.png'),
template: 'default.layout',
templateData: ['font' => 'DejaVu Sans'],
);
$pdfInvoice->denormalize();
// Subtotal: EUR 200.00; discount: EUR 20.00;
// tax: EUR 36.00; total: EUR 216.00.
return $pdfInvoice->stream();
namespace App\Mail;
use Elegantly\Invoices\Pdf\PdfInvoice;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
class PdfInvoiceMail extends Mailable
{
public function __construct(public PdfInvoice $pdfInvoice) {}
public function content(): Content
{
return new Content(htmlString: '<p>Your invoice is attached.</p>');
}
public function attachments(): array
{
return [
$this->pdfInvoice->toMailAttachment(filename: 'invoice.pdf'),
];
}
}
use App\Mail\PdfInvoiceMail;
use Illuminate\Support\Facades\Mail;
Mail::to('[email protected]')->send(new PdfInvoiceMail($pdfInvoice));
namespace App\Notifications;
use Elegantly\Invoices\Pdf\PdfInvoice;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class PdfInvoiceNotification extends Notification
{
public function __construct(public PdfInvoice $pdfInvoice) {}
public function via(object $notifiable): array
{
return ['mail'];
}
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject('Your invoice')
->line('Your invoice is attached.')
->attach($this->pdfInvoice);
}
}
use App\Notifications\PdfInvoiceNotification;
use Illuminate\Support\Facades\Notification;
Notification::route('mail', '[email protected]')
->notify(new PdfInvoiceNotification($pdfInvoice));
use Brick\Money\CurrencyDisplay;
use Brick\Money\Money;
use function Elegantly\Invoices\format_money;
$formatted = format_money(
Money::of('100.00', 'EUR'),
locale: 'fr',
currencyDisplay: CurrencyDisplay::Symbol,
hideFractionIfWhole: true,
);
namespace App\ValueObjects;
class PdfInvoice extends \Elegantly\Invoices\Pdf\PdfInvoice
{
public function getFilename(): string
{
return 'acme-'.parent::getFilename();
}
}
use Brick\Money\Money;
use Elegantly\Invoices\Collections\InvoiceDiscountCollection;
use Elegantly\Invoices\Collections\InvoiceTaxCollection;
use Elegantly\Invoices\Enums\InvoiceState;
use Elegantly\Invoices\Enums\InvoiceType;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\InvoiceTax;
use Elegantly\Invoices\Models\Invoice;
use Elegantly\Invoices\Models\InvoiceItem;
use Elegantly\Invoices\Support\PaymentInstruction;
use Illuminate\Support\Facades\DB;
$invoice = new Invoice([
'type' => InvoiceType::Invoice->value,
'state' => InvoiceState::Pending->value,
'state_set_at' => now(),
'seller_information' => config('invoices.default_seller'),
'buyer_information' => [
'company' => 'Doe Corporation',
'name' => 'John Doe',
'address' => [
'street' => ['8405 Old James St', 'Apartment 1'],
'city' => 'New York',
'postal_code' => '14609',
'state' => 'NY',
'country' => 'US',
],
'email' => '[email protected]',
],
'description' => 'Application development',
'due_at' => now()->addDays(30),
'fields' => ['Order' => 'PO0234'],
'metadata' => ['source' => 'customer-portal'],
'payment_instructions' => [
new PaymentInstruction(
name: 'Bank transfer',
fields: ['IBAN' => 'GB12ACME12345678123456'],
),
],
]);
$invoice->configureSerialNumber(
format: 'PPP-YYCCCC',
prefix: 'INV',
year: now()->year,
);
$invoice->setItems([
new InvoiceItem([
'label' => 'Consulting',
'unit_price' => Money::of('100.00', 'EUR'),
'quantity' => 2,
'quantity_unit' => 'hours',
'description' => 'Application development',
'discounts' => new InvoiceDiscountCollection([
new InvoiceDiscount(code: 'WELCOME', percentage: 10),
]),
'taxes' => new InvoiceTaxCollection([
new InvoiceTax(type: 'vat', country: 'FR', percentage: 20),
]),
'metadata' => ['product_id' => 'consulting'],
]),
]);
DB::transaction(fn () => $invoice->denormalize()->saveWithItems());
// The invoice and its item are stored, with total_amount = EUR 216.00.
$invoice->denormalize()->saveWithItems();
use Brick\Money\Money;
use Elegantly\Invoices\Collections\InvoiceDiscountCollection;
use Elegantly\Invoices\Collections\InvoiceTaxCollection;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\InvoiceTax;
use Elegantly\Invoices\Models\Invoice;
use Elegantly\Invoices\Models\InvoiceItem;
$invoice = new Invoice([
'subtotal_amount' => Money::of('200.00', 'EUR'),
'discount_amount' => Money::of('20.00', 'EUR'),
'tax_amount' => Money::of('36.00', 'EUR'),
'total_amount' => Money::of('216.00', 'EUR'),
]);
$invoice->setItems([
new InvoiceItem([
'label' => 'Consulting',
'quantity' => 2,
'unit_price' => Money::of('100.00', 'EUR'),
'price_subtotal' => Money::of('200.00', 'EUR'),
'price_discount' => Money::of('20.00', 'EUR'),
'price_tax' => Money::of('36.00', 'EUR'),
'price' => Money::of('216.00', 'EUR'),
'discounts' => new InvoiceDiscountCollection([
new InvoiceDiscount(code: 'WELCOME', amount: Money::of('20.00', 'EUR')),
]),
'taxes' => new InvoiceTaxCollection([
new InvoiceTax(type: 'vat', country: 'FR', amount: Money::of('36.00', 'EUR')),
]),
]),
]);
$invoice->saveWithItems();
use Brick\Money\Money;
use Elegantly\Invoices\Models\InvoiceItem;
// Set the items.
$invoice->setItems([
new InvoiceItem(['label' => 'Support', 'unit_price' => Money::of('50.00', 'EUR')]),
]);
// Add more items.
$invoice->addItems([
new InvoiceItem(['label' => 'Hosting', 'unit_price' => Money::of('10.00', 'EUR')]),
]);
$invoice->denormalize(force: true)->saveWithItems();
use Brick\Money\Money;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\InvoiceTax;
$item = $invoice->items->first();
$item->addDiscounts(new InvoiceDiscount(
code: 'SUMMER',
label: 'Summer offer',
percentage: 10,
));
$item->addTaxes([
new InvoiceTax(type: 'vat', country: 'FR', percentage: 20),
new InvoiceTax(label: 'Fixed levy', amount: Money::of('2.00', 'EUR')),
]);
$invoice->denormalize(force: true)->saveWithItems();
use Elegantly\Invoices\Support\Party;
use Elegantly\Invoices\Support\PaymentInstruction;
$invoice->seller_information = Party::make(config('invoices.default_seller'));
$invoice->buyer_information = [
'company' => 'Doe Corporation',
'tax_id' => ['country' => 'FR', 'code' => '123456789'],
'identities' => [['type' => 'SIREN', 'code' => '732829320']],
'fields' => ['Customer number' => 'ACME-42'],
];
$invoice->mergeFields(['Order' => 'PO0234']);
$invoice->metadata = ['external_id' => 'order_123'];
$invoice->payment_instructions = [
new PaymentInstruction(
name: 'Bank transfer',
description: 'Use the invoice number as the reference.',
fields: ['IBAN' => 'GB12ACME12345678123456'],
),
];
$invoice->save();
namespace App\Models;
use Elegantly\Invoices\Models\Invoice;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
class Customer extends Model
{
public function invoices(): MorphMany
{
return $this->morphMany(Invoice::class, 'buyer');
}
}
use Elegantly\Invoices\Models\Invoice;
$credit = new Invoice(['type' => 'credit']);
$credit->parent()->associate($invoice);
// Add credit items and amounts, then denormalize and save.
// Capture the current configured logo.
$invoice->setLogoFromConfig();
// Or capture a local file / uploaded file.
$invoice->setLogoFromPath(public_path('images/logo.png'));
$invoice->setLogoFromFile($uploadedFile);
// Or assign raw image bytes directly.
$invoice->logo = $rawImageBytes;
$invoice->save();
namespace App\Http\Controllers;
use Elegantly\Invoices\Models\Invoice;
use Illuminate\Support\Facades\Storage;
class InvoiceController extends Controller
{
public function show(Invoice $invoice)
{
return $invoice->loadMissing('items')->toPdfInvoice()->stream();
}
public function download(Invoice $invoice)
{
return $invoice->loadMissing('items')->toPdfInvoice()->download();
}
public function preview(Invoice $invoice)
{
return $invoice->loadMissing('items')->toPdfInvoice()->view();
}
public function store(Invoice $invoice)
{
$pdfInvoice = $invoice->loadMissing('items')->toPdfInvoice();
Storage::put(
'invoices/'.$pdfInvoice->getFilename(),
$pdfInvoice->getPdfOutput(),
);
return response()->noContent();
}
}
use App\Http\Controllers\InvoiceController;
use Illuminate\Support\Facades\Route;
Route::get('/invoices/{invoice}', [InvoiceController::class, 'show']);
Route::get('/invoices/{invoice}/download', [InvoiceController::class, 'download']);
Route::get('/invoices/{invoice}/preview', [InvoiceController::class, 'preview']);
Route::post('/invoices/{invoice}/pdf', [InvoiceController::class, 'store']);
namespace App\Livewire;
use Elegantly\Invoices\Models\Invoice;
use Livewire\Component;
class InvoiceDownload extends Component
{
public Invoice $invoice;
public function mount(Invoice $invoice): void
{
$this->invoice = $invoice;
}
public function download()
{
$pdfInvoice = $this->invoice->loadMissing('items')->toPdfInvoice();
return response()->streamDownload(function () use ($pdfInvoice) {
echo $pdfInvoice->getPdfOutput();
}, $pdfInvoice->getFilename(), ['Content-Type' => 'application/pdf']);
}
public function render()
{
return view('livewire.invoice-download');
}
}
namespace App\Models;
use Elegantly\Invoices\Pdf\PdfInvoice;
class Invoice extends \Elegantly\Invoices\Models\Invoice
{
public function toPdfInvoice(): PdfInvoice
{
$pdfInvoice = parent::toPdfInvoice();
$pdfInvoice->template = 'invoices::my-custom.layout';
$pdfInvoice->templateData = ['font' => 'DejaVu Sans'];
$pdfInvoice->fields['Support'] = '[email protected]';
return $pdfInvoice;
}
}
namespace App\Mail;
use Elegantly\Invoices\Models\Invoice;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
class InvoiceMail extends Mailable
{
public function __construct(public Invoice $invoice) {}
public function content(): Content
{
return new Content(htmlString: '<p>Your invoice is attached.</p>');
}
public function attachments(): array
{
return [$this->invoice->toMailAttachment()];
}
}
use App\Mail\InvoiceMail;
use Elegantly\Invoices\Models\Invoice;
use Illuminate\Support\Facades\Mail;
$invoice = Invoice::with('items')->findOrFail($invoiceId);
Mail::to($invoice->buyer_information->email)->send(new InvoiceMail($invoice));
namespace App\Notifications;
use Elegantly\Invoices\Models\Invoice;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class InvoiceNotification extends Notification
{
public function __construct(public Invoice $invoice) {}
public function via(object $notifiable): array
{
return ['mail'];
}
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject('Your invoice')
->line('Your invoice is attached.')
->attach($this->invoice);
}
}
use App\Notifications\InvoiceNotification;
use Elegantly\Invoices\Models\Invoice;
use Illuminate\Support\Facades\Notification;
$invoice = Invoice::with('items')->findOrFail($invoiceId);
Notification::route('mail', $invoice->buyer_information->email)
->notify(new InvoiceNotification($invoice));