You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,7 @@ Try out [the interactive demo](https://elegantly.dev/laravel-invoices) to explor
45
45
-[Attaching Invoices to Mailables](#attaching-invoices-to-mailables)
46
46
-[Attaching Invoices to Notifications](#attaching-invoices-to-notifications)
47
47
-[Customizing PDF Output from the Model](#customizing-pdf-output-from-the-model)
48
+
-[Using a Custom PdfInvoice Class](#using-a-custom-pdfinvoice-class)
48
49
-[Casting `state` and `type` to Enums](#casting-state-and-type-to-enums)
49
50
-[Using a Dynamic Logo](#using-a-dynamic-logo)
50
51
-[Testing](#testing)
@@ -910,6 +911,56 @@ return [
910
911
];
911
912
```
912
913
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
+
913
964
### Casting `state` and `type` to Enums
914
965
915
966
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