Skip to content

Commit 00eaac0

Browse files
committed
doc
1 parent 336cdda commit 00eaac0

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Try out [the interactive demo](https://elegantly.dev/laravel-invoices) to explor
4545
- [Attaching Invoices to Mailables](#attaching-invoices-to-mailables)
4646
- [Attaching Invoices to Notifications](#attaching-invoices-to-notifications)
4747
- [Customizing PDF Output from the Model](#customizing-pdf-output-from-the-model)
48+
- [Using a Custom PdfInvoice Class](#using-a-custom-pdfinvoice-class)
4849
- [Casting `state` and `type` to Enums](#casting-state-and-type-to-enums)
4950
- [Using a Dynamic Logo](#using-a-dynamic-logo)
5051
- [Testing](#testing)
@@ -910,6 +911,56 @@ return [
910911
];
911912
```
912913

914+
#### Using a Custom PdfInvoice Class
915+
916+
You can extend the default PdfInvoice class provided by the package to customize its behavior, such as changing the generated filename or adding additional logic.
917+
918+
1. Create Your Custom PdfInvoice Class
919+
920+
```php
921+
class PdfInvoice extends \Elegantly\Invoices\Pdf\PdfInvoice
922+
{
923+
924+
public function __construct(
925+
// your custom constructor
926+
){
927+
// ...
928+
}
929+
930+
public function getFilename(): string
931+
{
932+
return str($this->serial_number)
933+
->replace(['/', '\\'], '_')
934+
->append('.pdf')
935+
->value();
936+
}
937+
}
938+
```
939+
940+
In this example, we're overriding the `getFilename` method.
941+
942+
2. Return Your Custom `PdfInvoice` from the Invoice Model
943+
944+
Update your `Invoice` model to return an instance of your custom `PdfInvoice` class.
945+
946+
```php
947+
namespace App\Models;
948+
949+
use App\ValueObjects\PdfInvoice;
950+
951+
class Invoice extends \Elegantly\Invoices\Models\Invoice
952+
{
953+
function toPdfInvoice(): PdfInvoice
954+
{
955+
return new PdfInvoice(
956+
// Pass any required data to your custom PdfInvoice constructor
957+
);
958+
}
959+
}
960+
```
961+
962+
By overriding the `toPdfInvoice` method, you can inject your custom logic while preserving compatibility with the rest of the package.
963+
913964
### Casting `state` and `type` to Enums
914965

915966
By default, the `type` and `state` properties on the `Invoice` model are stored as strings. This approach offers flexibility, as it doesn't restrict you to predefined values and they are not automatically cast to Enum objects.

0 commit comments

Comments
 (0)