38 lines
1018 B
PHP
38 lines
1018 B
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Option;
|
|
use App\Models\Invoice;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Illuminate\Http\Response;
|
|
|
|
class PdfController extends Controller
|
|
{
|
|
/**
|
|
* Return an invoice as download for the specified resource.
|
|
*/
|
|
public function downloadInvoice(int $invoice_id): Response
|
|
{
|
|
return $this->buildInvoicePdf($invoice_id)->stream();
|
|
}
|
|
|
|
/**
|
|
* Return a rendered pdf invoice as string to build an email attachment from it for the specified resource.
|
|
*/
|
|
public function attachInvoice(int $invoice_id): string
|
|
{
|
|
return $this->buildInvoicePdf($invoice_id)->output();
|
|
}
|
|
|
|
/**
|
|
* Render the pdf view for the given invoice.
|
|
*/
|
|
protected function buildInvoicePdf(int $invoice_id): \Barryvdh\DomPDF\PDF
|
|
{
|
|
$invoice = Invoice::find($invoice_id);
|
|
|
|
return Pdf::loadView('pdfs.invoice', ['invoice' => $invoice->load(['address', 'delivery']), 'options' => Option::optionsAsObject()]);
|
|
}
|
|
}
|