Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Implement invoice renderer #1304

Merged
merged 5 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions config/cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,22 @@

/*
|--------------------------------------------------------------------------
| 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'
| These options are the defaults for all invoices generated
| by Cashier. You are free to customize these based on
| the renderer used by your Laravel application.
|
*/

'paper' => env('CASHIER_PAPER', 'letter'),
'invoices' => [
'renderer' => env('CASHIER_INVOICE_RENDERER', Laravel\Cashier\Invoices\DompdfInvoiceRenderer::class),
'options' => [
// Supported dompdf paper sizes: 'letter', 'legal', 'A4'
'paper' => env('CASHIER_PAPER', 'letter'),
],
],

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

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

Expand All @@ -54,6 +56,20 @@ protected function configure()
);
}

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

/**
* Bind the Stripe logger interface to the Cashier 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();
}
}