Skip to content

Commit

Permalink
add getLogo method
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jan 22, 2025
1 parent 7959575 commit f8a4dd2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Try out [the interactive demo](https://elegantengineering.tech/laravel-invoices)
- [Displaying Your Invoice as a PDF](#displaying-your-invoice-as-a-pdf)
- [Attaching Your Invoice to an Email](#attaching-your-invoice-to-an-email)
- [Customizing the PDF Invoice](#customizing-the-pdf-invoice)
- [Adding a Dynamic Logo](#adding-a-dynamic-logo)

1. [The PdfInvoice Class](#the-pdfinvoice-class)

Expand Down Expand Up @@ -390,6 +391,31 @@ To customize how your model is converted to a `PdfInvoice`, follow these steps:
];
```

### Adding a Dynamic Logo

If you need to set the logo dynamically on the invoice, for example, when allowing users to upload their own logo, you can achieve this by overriding the `getLogo` method. Follow the steps outlined in the [Customizing the PDF Invoice](#customizing-the-pdf-invoice) section to create your own Model.

To dynamically set the logo, define the `getLogo` method as shown below:

```php
class Invoice extends \Finller\Invoice\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;
}
}
```

> [!NOTE]
> The returned value must be either a base64-encoded data URL or a path to a locally accessible file.
## The PdfInvoice Class

This package includes a standalone `PdfInvoice` class, making it easy to render invoices as a PDF or directly within a view.
Expand Down
11 changes: 10 additions & 1 deletion src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,14 @@ public function toMailAttachment(): Attachment
->withMime('application/pdf');
}

/**
* @return string|null A base64 encoded data url or a path to a local file
*/
public function getLogo(): ?string
{
return null;
}

public function toPdfInvoice(): PdfInvoice
{
return new PdfInvoice(
Expand All @@ -463,7 +471,8 @@ public function toPdfInvoice(): PdfInvoice
description: $this->description,
items: $this->items->map(fn (InvoiceItem $item) => $item->toPdfInvoiceItem())->all(),
tax_label: $this->getTaxLabel(),
discounts: $this->getDiscounts()
discounts: $this->getDiscounts(),
logo: $this->getLogo(),
);
}
}

0 comments on commit f8a4dd2

Please sign in to comment.