Padocia / Laravel-Nova-Pdf

MIT License
21 stars 9 forks source link

Export nova resources to pdf

Laravel-Nova-Excel

✨ Features

Requirements

This package is using Browsershot, The conversion is done behind the scenes by Puppeteer which controls a headless version of Google Chrome.

Installation

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

Usage

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'));
    }
}

Attach action to a 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,
        ];
    }
}

Customize Browsershot

Override the handleBrowsershotOptions method in the action class :

protected function handleBrowsershotOptions()
{
    $this->browsershot = $this->browsershot->format('A4');

    return $this;
}

Change Filename

Override the filename method in the action class :

protected function filename()
{
    return "your_new_filename.pdf";
}

Change Disk

$this->withDisk('public');

Add new css file

$this->addStyle('nova_style_name');

Remove tailwind css

$this->useTailwind(false);

Extend download url expiration time

protected $downloadUrlExpirationTime = 1; // 1 min

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email zakaria.tayebbey@gmail.com.

Credits

License

The MIT License (MIT). Please see License File for more information.