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 Elegantly\Invoices\Models\Invoice;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\Models\InvoiceItem;
use Elegantly\Invoices\Enums\InvoiceType;
return [
'model_invoice' => Invoice::class,
'model_invoice_item' => InvoiceItem::class,
'discount_class' => InvoiceDiscount::class,
'cascade_invoice_delete_to_invoice_items' => true,
'serial_number' => [
/**
* If true, will generate a serial number on creation
* If false, you will have to set the serial_number yourself
*/
'auto_generate' => true,
/**
* Define the serial number format used for each invoice type
*
* P: Prefix
* S: Serie
* M: Month
* Y: Year
* C: Count
* Example: IN0012-220234
* Repeat letter to set the length of each information
* Examples of formats:
* - PPYYCCCC : IN220123 (default)
* - PPPYYCCCC : INV220123
* - PPSSSS-YYCCCC : INV0001-220123
* - SSSS-CCCC: 0001-0123
* - YYCCCC: 220123
*/
'format' => 'PPYYCCCC',
/**
* Define the default prefix used for each invoice type
*/
'prefix' => [
InvoiceType::Invoice->value => 'IN',
InvoiceType::Quote->value => 'QO',
InvoiceType::Credit->value => 'CR',
InvoiceType::Proforma->value => 'PF',
],
],
'date_format' => 'Y-m-d',
'default_seller' => [
'name' => null,
'address' => [
'street' => null,
'city' => null,
'postal_code' => null,
'state' => null,
'country' => null,
],
'email' => null,
'phone' => null,
'tax_number' => null,
'company_number' => null,
],
/**
* ISO 4217 currency code
*/
'default_currency' => 'USD',
'pdf' => [
'paper' => [
'paper' => 'a4',
'orientation' => 'portrait',
],
/**
* Default DOM PDF options
*
* @see Available options https://github.com/barryvdh/laravel-dompdf#configuration
*/
'options' => [
'isRemoteEnabled' => true,
'isPhpEnabled' => false,
'fontHeightRatio' => 1,
/**
* Supported values are: 'DejaVu Sans', 'Helvetica', 'Courier', 'Times', 'Symbol', 'ZapfDingbats'
*/
'defaultFont' => 'Helvetica',
'fontDir' => storage_path('fonts'), // advised by dompdf (https://github.com/dompdf/dompdf/pull/782)
'fontCache' => storage_path('fonts'),
'tempDir' => sys_get_temp_dir(),
'chroot' => realpath(base_path()),
],
/**
* The logo displayed in the PDF
*/
'logo' => null,
/**
* The template used to render the PDF
*/
'template' => 'default.layout',
'template_data' => [
/**
* The color displayed at the top of the PDF
*/
'color' => '#050038',
],
],
];
use Brick\Money\Money;
use Elegantly\Invoices\Models\Invoice;
use Elegantly\Invoices\Enums\InvoiceState;
use Elegantly\Invoices\Enums\InvoiceType;
// Let's say your app edit invoices for your users
$customer = User::find(1);
// Let's imagine that your users have purchased something in your app
$order = Order::find(2);
$invoice = new Invoice([
'type' => InvoiceType::Invoice,
'state' => InvoiceState::Draft,
'description' => 'A description for my invoice',
'seller_information' => config('invoices.default_seller'),
'buyer_information' => [
'name' => 'John Doe',
'address' => [
'street' => '8405 Old James St.Rochester',
'city' => 'New York',
'postal_code' => '14609',
'state' => 'New York (NY)',
'country' => 'United States',
],
'email' => '[email protected]',
'tax_number' => "FR123456789",
],
// ...
]);
$invoice->buyer()->associate($customer); // optionnally associate the invoice to any model
$invoice->invoiceable()->associate($order); // optionnally associate the invoice to any model
$invoice->save();
$invoice->items()->saveMany([
new InvoiceItem([
'unit_price' => Money::of(100, 'USD'),
'unit_tax' => Money::of(20, 'USD'),
'currency' => 'USD',
'quantity' => 1,
'label' => 'A label for my item',
'description' => 'A description for my item',
]),
]);
use Elegantly\Invoices\Models\Invoice;
$invoice = new Invoice();
$invoice->configureSerialNumber(
prefix: "ORG",
serie: $buyer_id,
);
namespace App\Http\Controllers;
use App\Models\Invoice;
use Illuminate\Http\Request;
class InvoiceController extends Controller
{
public function show(Request $request, string $serial)
{
/** @var Invoice $invoice */
$invoice = Invoice::where('serial_number', $serial)->firstOrFail();
$this->authorize('view', $invoice);
return $invoice->toPdfInvoice()->stream();
}
public function download(Request $request, string $serial)
{
/** @var Invoice $invoice */
$invoice = Invoice::where('serial_number', $serial)->firstOrFail();
$this->authorize('view', $invoice);
return $invoice->toPdfInvoice()->download();
}
}
namespace App\Mail;
use App\Models\Invoice;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class PaymentInvoice extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct(
protected Invoice $invoice,
) {}
public function attachments(): array
{
return [
$this->invoice->toMailAttachment()
];
}
}
class Invoice extends \Elegantly\Invoices\Models\Invoice
{
// ...
public function getLogo(): ?string
{
$file = new File(public_path('logo.png'));
$mime = $file->getMimeType();
$logo = "data:{$mime};base64," . base64_encode($file->getContent());
return $logo;
}
}
use Elegantly\Invoices\Pdf\PdfInvoice;
use Elegantly\Invoices\Support\Address;
use Elegantly\Invoices\Support\Seller;
use Elegantly\Invoices\Support\Buyer;
$pdfInvoice = new PdfInvoice(
name: "Invoice",
state: "Paid",
serial_number: "INV-241200001",
seller: new Seller(
name: 'elegantly',
address: new Address(
street: "Place de l'Opéra",
city: 'Paris',
postal_code: '75009',
country: 'France',
),
email: '[email protected]',
tax_number: 'FR123456789',
fields: [
"foo" => "bar"
]
),
buyer: new Buyer(
name: 'John Doe',
address: new Address(
street: '8405 Old James St.Rochester',
city: 'New York',
postal_code: '14609',
state: 'New York (NY)',
country: 'United States',
),
email: '[email protected]',
data: [
"foo" => "bar"
]
),
description: "An invoice description",
created_at: now(),
due_at: now(),
paid_at: now(),
tax_label: "VAT (20%)",
fields: [],
items: [],
discounts: [],
logo: public_path('/images/logo.png'),
template: "default.layout",
templateData: [
'color' => '#050038'
],
)
namespace App\Http\Controllers;
use Elegantly\Invoices\Pdf\PdfInvoice;
class InvoiceController extends Controller
{
public function showAsView()
{
$pdfInvoice = new PdfInvoice(
// ...
);
return $pdfInvoice->view();
}
public function showAsPdf()
{
$pdfInvoice = new PdfInvoice(
// ...
);
return $pdfInvoice->stream();
}
public function download()
{
$pdfInvoice = new PdfInvoice(
// ...
);
return $pdfInvoice->download();
}
}
namespace App\Http\Controllers;
use Elegantly\Invoices\Pdf\PdfInvoice;
class Invoice extends Component
{
public function download()
{
$pdfInvoice = new PdfInvoice(
// ...
);
return response()->streamDownload(function () use ($pdfInvoice) {
echo $pdf->getPdfOutput();
}, 'laravel-invoices-demo.pdf');
}
}