ambengers/eloquent-pdf

Laravel package for creating PDF files with Eloquent ORM dependencies.

1.0 2020-12-17 06:14 UTC

This package is auto-updated.

Last update: 2021-04-17 07:22:42 UTC


README

This package provides an elegant way to generate PDF with eloquent models. Uses Laravel Snappy to generate PDF and Laravel Medialibrary to associate PDF as model media.

Build Status StyleCI

Installation

Via Composer

$ composer require ambengers/eloquent-pdf

Optionally, you can publish the config file by running the following command.

php artisan vendor:publish --tag=eloquent-pdf-config

Usage

Eloquent PDF class

You can generate your Eloquent PDF class using the command.

$ php artisan make:eloquent-pdf PostPdf

By default, the class will be located at App\Pdf namespace. You can customize this in config file.

Your Eloquent PDF class will contain 2 methods:

  • getData() provides the data to be used on the view
  • getView() the name of the view file as pdf template
namespace App\Pdf;

class PostPdf extends AbstractEloquentPdf
{
    public function getData() : array
    {
        return [
            'title' => $this->model->title,
            'body'  => $this->model->body,
        ];
    }

    public function getView() : string
    {
        return 'posts.pdf';
    }
}

You can now use the Eloquent PDF class from your controller (or anywhere in your application).

Downloading PDF

return app(PostPdf::class)
    ->model($post)
    ->download()
    ->handle();

Print Preview PDF

return app(PostPdf::class)
    ->model($post)
    ->stream()
    ->handle();

Eloquent PDF to Medialibrary

This package also offers an elegant way to associate the PDF file to the eloquent model using Medialibrary package. To do that, you will need to use a trait to your Eloquent PDF class,

namespace App\Pdf;

use Ambengers\EloquentPdf\InteractsWithMediaLibrary;

class PostPdf extends AbstractEloquentPdf
{
    use InteractsWithMediaLibrary;

    // ...
}

Then on your controller, just provide the collection name in which the PDF file will be associated with.

return app(PostPdf::class)
    ->model($post)
    ->toMediaCollection('reports')
    ->handle();

Customizations

If you need further customizations such as changing the default PDF filename, extension or setting PDF options, you can override some methods from your Eloquent PDF class.

namespace App\Pdf;

class PostPdf extends AbstractEloquentPdf
{
    public function getOrientation(): string
    {
        return 'landscape';
    }

    public function getOptions(): array
    {
        return [
            'footer-right'     => 'Right footer text goes here!',
            'footer-font-size' => 8,
            'encoding'         => 'UTF-8',
        ];
    }

    public function getFilename(): string
    {
        return 'new-file-name';
    }

    public function getExtension(): string
    {
        return 'odt';
    }
}

Alternatively, if you want to only customize during runtime, you can chain some setter methods when you call your Eloquent PDF class

return app(PostPdf::class)
    ->model($post)
    ->orientation('landscape')
    ->options(['footer-font-size' => 8])
    ->filename('some-cool-filename')
    ->toMediaCollection('reports')
    ->handle();

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

License

Please see the license file for more information.