Skip to content

Commit

Permalink
[12.x] Implement invoice renderer (#1304)
Browse files Browse the repository at this point in the history
* Implement invoice renderer

* wip

* wip

* formatting

* Add default renderer in service provider

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
driesvints and taylorotwell committed Jan 25, 2022
1 parent 2dbcf86 commit e0767e6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 20 deletions.
21 changes: 14 additions & 7 deletions config/cashier.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Laravel\Cashier\Invoices\DompdfInvoiceRenderer;

return [

/*
Expand Down Expand Up @@ -100,18 +102,23 @@

/*
|--------------------------------------------------------------------------
| Invoice Paper Size
| Invoice Settings
|--------------------------------------------------------------------------
|
| This option is the default paper size for all invoices generated using
| Cashier. You are free to customize this settings based on the usual
| paper size used by the customers using your Laravel applications.
|
| Supported sizes: 'letter', 'legal', 'A4'
| The following options determine how Cashier invoices are converted from
| HTML into PDFs. You're free to change the options based on the needs
| of your application or your preferences regarding invoice styling.
|
*/

'paper' => env('CASHIER_PAPER', 'letter'),
'invoices' => [
'renderer' => env('CASHIER_INVOICE_RENDERER', DompdfInvoiceRenderer::class),

'options' => [
// Supported: 'letter', 'legal', 'A4'
'paper' => env('CASHIER_PAPER', 'letter'),
],
],

/*
|--------------------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions src/CashierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Laravel\Cashier\Console\WebhookCommand;
use Laravel\Cashier\Contracts\InvoiceRenderer;
use Laravel\Cashier\Invoices\DompdfInvoiceRenderer;
use Stripe\Stripe;
use Stripe\Util\LoggerInterface;

Expand Down Expand Up @@ -40,6 +42,7 @@ public function register()
{
$this->configure();
$this->bindLogger();
$this->bindInvoiceRenderer();
}

/**
Expand Down Expand Up @@ -68,6 +71,18 @@ protected function bindLogger()
});
}

/**
* Bind the default invoice renderer.
*
* @return void
*/
protected function bindInvoiceRenderer()
{
$this->app->bind(InvoiceRenderer::class, function ($app) {
return $app->make(config('cashier.invoices.renderer', DompdfInvoiceRenderer::class));
});
}

/**
* Register the Stripe logger.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/InvoiceRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Cashier\Contracts;

use Laravel\Cashier\Invoice;

interface InvoiceRenderer
{
/**
* Render the invoice as a PDF and return the raw bytes.
*
* @param \Laravel\Cashier\Invoice $invoice
* @param array $data
* @param array $options
* @return string
*/
public function render(Invoice $invoice, array $data = [], array $options = []): string;
}
19 changes: 6 additions & 13 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace Laravel\Cashier;

use Carbon\Carbon;
use Dompdf\Dompdf;
use Dompdf\Options;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
use JsonSerializable;
use Laravel\Cashier\Contracts\InvoiceRenderer;
use Laravel\Cashier\Exceptions\InvalidInvoice;
use Stripe\Customer as StripeCustomer;
use Stripe\Invoice as StripeInvoice;
Expand Down Expand Up @@ -565,19 +564,13 @@ public function view(array $data)
*/
public function pdf(array $data)
{
if (! defined('DOMPDF_ENABLE_AUTOLOAD')) {
define('DOMPDF_ENABLE_AUTOLOAD', false);
}

$options = new Options;
$options->setChroot(base_path());
$options = config('cashier.invoices.options', []);

$dompdf = new Dompdf($options);
$dompdf->setPaper(config('cashier.paper', 'letter'));
$dompdf->loadHtml($this->view($data)->render());
$dompdf->render();
if ($paper = config('cashier.paper')) {
$options['paper'] = $paper;
}

return $dompdf->output();
return app(InvoiceRenderer::class)->render($this, $data, $options);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Invoices/DompdfInvoiceRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Laravel\Cashier\Invoices;

use Dompdf\Dompdf;
use Dompdf\Options;
use Laravel\Cashier\Contracts\InvoiceRenderer;
use Laravel\Cashier\Invoice;

class DompdfInvoiceRenderer implements InvoiceRenderer
{
/**
* {@inheritDoc}
*/
public function render(Invoice $invoice, array $data = [], array $options = []): string
{
if (! defined('DOMPDF_ENABLE_AUTOLOAD')) {
define('DOMPDF_ENABLE_AUTOLOAD', false);
}

$dompdfOptions = new Options;
$dompdfOptions->setChroot(base_path());

$dompdf = new Dompdf($dompdfOptions);
$dompdf->setPaper($options['paper'] ?? 'letter');
$dompdf->loadHtml($invoice->view($data)->render());
$dompdf->render();

return (string) $dompdf->output();
}
}

0 comments on commit e0767e6

Please sign in to comment.