PHPackages                             ambengers/eloquent-pdf - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. ambengers/eloquent-pdf

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

ambengers/eloquent-pdf
======================

Laravel package for creating PDF files with Eloquent ORM dependencies.

5.0(2y ago)215854MITPHPPHP ^8.0

Since Dec 8Pushed 2y ago2 watchersCompare

[ Source](https://github.com/ambengers/eloquent-pdf)[ Packagist](https://packagist.org/packages/ambengers/eloquent-pdf)[ Docs](https://github.com/ambengers/eloquent-pdf)[ RSS](/packages/ambengers-eloquent-pdf/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (5)Versions (16)Used By (0)

Laravel Eloquent PDF
====================

[](#laravel-eloquent-pdf)

This package provides an elegant way to generate PDF with Eloquent Models. Uses [Laravel Snappy](https://github.com/barryvdh/laravel-snappy) to generate PDF and [Laravel Medialibrary](https://github.com/spatie/laravel-medialibrary) to associate PDF as model media.

[![CircleCI](https://camo.githubusercontent.com/f6276e3d56f70d622dd7bfc7c710d3c94932c9028e47453c3ff83c2b08b883c7/68747470733a2f2f636972636c6563692e636f6d2f67682f616d62656e676572732f656c6f7175656e742d7064662f747265652f6d61737465722e7376673f7374796c653d737667)](https://circleci.com/gh/ambengers/eloquent-pdf/tree/master)[![StyleCI](https://camo.githubusercontent.com/ad83ff3b7273e4ea7502ba390d4186439ff70fe9996b370b20ec6f8908b9423c/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3331363435343635382f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/316454658?branch=master)

Spatie Media Library Version Compatibility
------------------------------------------

[](#spatie-media-library-version-compatibility)

VersionMedialibraryv1.\*~ 7.20v2.\*^ 8.0v3.\*^ 9.0v4.\*^ 10.0v5.\*^ 10.0Installation
------------

[](#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
-----

[](#usage)

### Eloquent PDF class

[](#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

[](#downloading-pdf)

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

### Print Preview PDF

[](#print-preview-pdf)

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

### Eloquent PDF with Medialibrary

[](#eloquent-pdf-with-medialibrary)

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

```
use Ambengers\EloquentPdf\InteractsWithMediaLibrary;

class PostPdf extends AbstractEloquentPdf
{
    use InteractsWithMediaLibrary;
}
```

Then on your controller, much like how you'd do on medialibrary, just provide the collection name in which the PDF file will be associated with.

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

For additional convenience you can also chain other medialibrary methods.

```
return app(PostPdf::class)
    ->model($post)
    ->toMediaCollection('reports')
    ->withCustomProperties(['foo' => 'bar'])
    ->withAttributes(['creator_id' => auth()->id()])
    ->handle();
```

Behind the scenes, Eloquent PDF will forward these method calls to the medialibrary `FileAdder::class` so you can further take advantage of its features.

### Customizations

[](#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
--------

[](#security)

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

License
-------

[](#license)

Please see the [license file](license.md) for more information.

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~81 days

Recently: every ~184 days

Total

14

Last Release

980d ago

Major Versions

1.4 → 2.02021-10-19

2.0 → 3.02021-10-19

1.6 → 3.12021-10-25

3.1 → 4.02022-04-08

4.0 → 5.02023-10-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/545ea2630b623d1556d595190fc55b0a32731291629f2378c632bcf0dd3164c2?d=identicon)[ambengers](/maintainers/ambengers)

---

Top Contributors

[![ambengers](https://avatars.githubusercontent.com/u/17848299?v=4)](https://github.com/ambengers "ambengers (108 commits)")

---

Tags

laravelEloquentPdf

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ambengers-eloquent-pdf/health.svg)

```
[![Health](https://phpackages.com/badges/ambengers-eloquent-pdf/health.svg)](https://phpackages.com/packages/ambengers-eloquent-pdf)
```

###  Alternatives

[maatwebsite/excel

Supercharged Excel exports and imports in Laravel

12.9k157.3M893](/packages/maatwebsite-excel)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)[api-platform/laravel

API Platform support for Laravel

58171.5k14](/packages/api-platform-laravel)[ublabs/blade-simple-icons

A package to easily make use of Simple Icons in your Laravel Blade views.

1963.4k](/packages/ublabs-blade-simple-icons)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
