padocia/laravel-nova-pdf

Generate Pdf from nova resources

v1.0.2 2021-07-08 15:27 UTC

This package is auto-updated.

Last update: 2024-03-27 01:37:53 UTC


README

Laravel-Nova-Excel

✨ Features

  • 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.

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.