Export nova resources to PDF. Easily export nova resources to pdf file using blade templates designed by you ! It could be an invoice, a report .... sky is the limit.
PDF files are generated from blade templates. Generating pdf has never been so easy, Write your pdf template using blade and export it as pdf.
Support Tailwind Css to style blade templates. Prefer to use Tailwind css framework to style your blade templates ? wrap your div with .tailwind-container
and you are ready to go.
This package is using Browsershot, The conversion is done behind the scenes by Puppeteer which controls a headless version of Google Chrome.
You can install the package via composer:
composer require padocia/laravel-nova-pdf
If you don't have the puppeteer
on your project:
npm install puppeteer
You can publish the default blade template :
php artisan vendor:publish --tag nova-pdf-template
Export to pdf nova actions may be generated using :
php artisan nova:ExportToPdfAction InvoiceAction
A new nova action is created under app\nova\actions
, Feel free to customize or change the view
<?php
namespace App\Nova\Actions;
use Illuminate\Support\Collection;
use Illuminate\View\View;
use Laravel\Nova\Fields\ActionFields;
use Padocia\NovaPdf\Actions\ExportToPdf;
class InvoiceAction extends ExportToPdf
{
/**
* @param \Laravel\Nova\Fields\ActionFields $fields
* @param \Illuminate\Support\Collection $models
*
* @return \Illuminate\View\View
*/
public function preview(ActionFields $fields, Collection $models) : View
{
$resource = $this->resource;
return view('nova-pdf.template', compact('models','resource'));
}
}
We'll Use the User
resource as an example to add your action to the actions() list.
<?php
namespace App\Nova;
use Illuminate\Http\Request;
use App\Nova\Actions\InvoiceAction;
class User extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\\User';
// Other default resource methods
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function actions(Request $request)
{
return [
new InvoiceAction,
];
}
}
Override the handleBrowsershotOptions
method in the action class :
protected function handleBrowsershotOptions()
{
$this->browsershot = $this->browsershot->format('A4');
return $this;
}
Override the filename
method in the action class :
protected function filename()
{
return "your_new_filename.pdf";
}
$this->withDisk('public');
$this->addStyle('nova_style_name');
$this->useTailwind(false);
protected $downloadUrlExpirationTime = 1; // 1 min
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email zakaria.tayebbey@gmail.com.
The MIT License (MIT). Please see License File for more information.