PHPackages                             contoweb/laravel-pdflib - 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. contoweb/laravel-pdflib

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

contoweb/laravel-pdflib
=======================

Laravel wrapper for PDFLib to make creating PDFs a breeze

v3.3.0(3mo ago)15803↓50%2MITPHPPHP ^8.0CI passing

Since Oct 1Pushed 3mo ago4 watchersCompare

[ Source](https://github.com/contoweb/laravel-pdflib)[ Packagist](https://packagist.org/packages/contoweb/laravel-pdflib)[ RSS](/packages/contoweb-laravel-pdflib/feed)WikiDiscussions master Synced 1mo ago

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

Laravel PDFlib
--------------

[](#laravel-pdflib)

[![Run tests](https://github.com/contoweb/laravel-pdflib/actions/workflows/run-tests.yml/badge.svg)](https://github.com/contoweb/laravel-pdflib/actions/workflows/run-tests.yml)[![StyleCI](https://camo.githubusercontent.com/6388f285345d27bcfef3c63140efa2e16d88d8e969f20bbbedd366842b926a11/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3231303435303433352f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/210450435)[![Latest Stable Version](https://camo.githubusercontent.com/a0ed398a71eca3dc01d1e607c3d54bbde866dd7da505e51dec4488f197fef1b4/68747470733a2f2f706f7365722e707567782e6f72672f636f6e746f7765622f6c61726176656c2d7064666c69622f762f737461626c65)](https://packagist.org/packages/contoweb/laravel-pdflib)[![License](https://camo.githubusercontent.com/f6ac5cfe3df8dd76a80bea0fce838247c1b64653a1e2a0c44d30ece002d427a8/68747470733a2f2f706f7365722e707567782e6f72672f636f6e746f7765622f6c61726176656c2d7064666c69622f6c6963656e7365)](https://packagist.org/packages/contoweb/laravel-pdflib)

This package is a Laravel wrapper for [PDFlib](https://www.pdflib.com/products/pdflib-family/overview/). It makes generating high professional (Print-)PDFs with PDFlib a breeze. PDFlib is the leading developer toolbox for generating and manipulating files in the Portable Document Format (PDF).

PDFlib itself is only free to use for demonstration purposes. If you want to bring it into production, you need a PDFlib license.

Documentation
-------------

[](#documentation)

- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#sub-items)
    - [Quick start](#quick-start)
    - [Pages](#create-a-page)
    - [Templates](#using-a-template)
        - [Preview &amp; Print](#preview-and-print-pdf)
    - [Navigation](#navigate-on-the-page)
    - [Text](#write-text)
        - [Fonts](#fonts)
        - [Colors](#colors)
    - [Images](#images)
    - [PDFlib functions](#pdflib-functions)
- [Customization](#customization)
- [License](#license)

Requirements
------------

[](#requirements)

Since PDFlib is much more powerful than any other PDF generator, it's PHP extension needs to be registered. You can download the extension file directly from the [PDFlib download](https://www.pdflib.com/download/pdflib-product-family/) page.

If you need further assistance installing PDFlib, check out the [installation guide](https://www.pdflib.com/fileadmin/pdflib/pdf/support/PDFlib-in-PHP-HowTo.pdf).

You also need:

- PHP: `^8.0`
- Laravel: `^9.0`

Please note that only supported Laravel versions are covered by test workflow!

Installation
------------

[](#installation)

Require this package with composer.

```
composer require contoweb/laravel-pdflib
```

All basic configuration is done in the `pdf.php` configuration file. To publish the config, run the vendor publish command:

```
php artisan vendor:publish --provider="Contoweb\Pdflib\PdflibServiceProvider"

```

You can then configure paths, measure unit and so on...

### Laravel 5.5+:

[](#laravel-55)

Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider. If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php. Since Laravel 11, the file for providers has changed to bootstrap/providers.php.

```
Contoweb\Pdflib\PdflibServiceProvider::class,
```

If you want to use the PDF facade, add this to your facades in app.php:

```
'Pdf' => Contoweb\Pdflib\Facades\Pdf::class,
```

Usage
-----

[](#usage)

To create a new document, you can use the `make:document` command:

```
php artisan make:document MarketingDocument
```

The new document file can be found in the `app/Documents` directory.

> app\\Documents\\MarketingDocument

Ìn a final step, generating a PDF is as easy as:

```
use App\Documents\MarketingDocument;
use Contoweb\Pdflib\Facades\Pdf;
use App\Http\Controllers\Controller;

class MarketingController extends Controller
{
    public function storeDocument()
    {
        return Pdf::store(new MarketingDocument, 'marketing.pdf');
    }
}
```

You can find your document in your configured export path then! But first, let us take a look how to write a simple PDF.

### Quick start

[](#quick-start)

Within your document file, you have a boilerplated method `draw()`:

```
public function draw(Writer $writer)
{
    $writer->newPage();
    $writer->useFont('Arial', 12);
    $writer->writeText('Start something great...');
}
```

Here you actually write the document's content. As you can see, a small example is already boilerplated:

1. Create a new page in the document.
2. Use an available font from the `fonts()` method.
3. Write the text.

### Create a page

[](#create-a-page)

To create a new page, you can use

```
$writer->newPage();
```

You can optionally define the width and height of your document by passing the parameters.

```
$writer->newPage(210, 297); // A4 portrait format
```

#### Using a template

[](#using-a-template)

In most cases, you want to write dynamic content on a already designed PDF. To use a PDF template, use the `FromTemplate` concern and define the template PDF in a new `template()` function:

```
namespace App\Documents;

use Contoweb\Pdflib\Concerns\FromTemplate;
use Contoweb\Pdflib\Concerns\WithDraw;
use Contoweb\Pdflib\Writers\PdfWriter as Writer;

class MarketingDocument implements FromTemplate, WithDraw
{
    public function template(): string {
        return 'template.pdf';
    }

    // ...

    public function draw(Writer $writer)
    {
        $writer->newPage()->fromTemplatePage(1);
    }
}
```

Now, your first page is using the page 1 from `template.pdf`. As you can see, you don't need to define a page size since it's using the template's size. Don't forget to configure your templates location in the configuration file.

##### Preview and print PDF

[](#preview-and-print-pdf)

If you're aware of (professional) print-ready PDFs, you may know that your print PDF isn't the same as the user finally sees.

[![pdf-bleed](https://user-images.githubusercontent.com/13394801/65696401-6e6fdc00-e079-11e9-96fa-86e9d40d6aa1.jpg)](https://user-images.githubusercontent.com/13394801/65696401-6e6fdc00-e079-11e9-96fa-86e9d40d6aa1.jpg)

There is a bleed box, crop marks and so on. For this case, you can use `WithPreview` combined with the `FromTemplate` concern. While your original template includes all the boxes and marks, your preview PDF is a preview of the final document.

This requires you to add a `previewTemplate()` and `offset()` method.

```
namespace App\Documents;

use Contoweb\Pdflib\Concerns\FromTemplate;
use Contoweb\Pdflib\Concerns\WithDraw;
use Contoweb\Pdflib\Concerns\WithPreview;
use Contoweb\Pdflib\Writers\PdfWriter as Writer;

class MarketingDocument implements FromTemplate, WithDraw, WithPreview
{
    public function template(): string {
        return 'print.pdf';
    }

    public function previewTemplate(): string
    {
        return 'preview.pdf';
    }

    public function offset(): array
    {
        return [
            'x' => 20,
            'y' => 20,
        ];
    }

    //
}
```

The `offset()` method defines the offset from the print PDF to the preview PDF (see image above).

Now you can generate the preview PDF with:

```
return Pdf::inPreviewMode()->store(new MarketingDocument, 'marketing.pdf');
```

You can also generate the print and preview PDF in one step:

```
return Pdf::store(new MarketingDocument, 'marketing.pdf')->withPreview();
```

The preview PDF will be automatically named to `_preview.pdf`. You can override this by passing the name in `->withPreview('othername.pdf')`.

### Navigate on the page

[](#navigate-on-the-page)

To tell PDFlib where your elements should be placed, you have to set the `X` and `Y` position of your "cursor".

```
$writer->setPosition(10, 100);

// only X axis
$writer->setXPosition(10);

//only Y axis
$writer->setYPosition(100);
```

In the configuration file, you can define which measure unit is used for positioning. You can choose between `mm` or `pt`.

> **Note**: It may be confusing in the beginning, but PDFlib Y axis are measured from the bottom. So position 0 0 is in the left bottom corner, not the left top corner.

### Write text

[](#write-text)

To write text, you can simply use:

```
$writer->writeTextLine('your text');

// or

$writer->writeText('your text');
```

Don't forget to set the cursor position and use the right font before writing text. Since the package extends PDFlib, you also can pass PDFlib options as a second parameter.

> You only have to use `writeText` when placing two text blocks next to each other. Behind the scenes, `wirteText()` uses PDFlibs `show()` method, while `wirteTextLine()` uses the mostly used PDFlib method `fit_text_line()`.

If you want to go to the next line, instead of reposition your cursor every time, you can use:

```
$writer->nextLine();
```

To use a custom line spacing instead of 1.0, just pass it as a parameter or set the line spacing with:

```
$writer->setLineSpacing(2.0);
```

#### Fonts

[](#fonts)

The boilerplate document loads `Arial` as an example font, but we don't provide a font file in the fonts folder. In this case, PDFlib tries to load it from your host fonts. You may want to use custom fonts and want ensure that your server is able to load it. So it's highly recommended to place the font files (currently .ttf and .otf is supported) inside your configured font location (see `pdf.php` configuration).

As a next step, you have to make the fonts available in your document. For TrueType fonts, just use the file name without the extension to auto-load the font:

```
public function fonts(): array
{
    return ['OpenSans-Regular'];
}
```

An underlying font file like `OpenSans-Regular.ttf` has to be available in your fonts location.

Now you can use the font in your document by it's name:

```
public function draw(Writer $writer)
{
    $writer->newPage();
    $writer->useFont('OpenSans-Regular', 12)
           ->writeText('This text is written with Open Sans font...');
}
```

You can also overwrite default font encoding and option list:

```
public function fonts(): array
    {
        return [
            'OpenSans-Regular' => [
                'encoding' => 'ansi',
                'optlist' => ''
            ],
        ];
    }
```

#### Colors

[](#colors)

If you need to colorize your text, you can use the `WithColor` concern. This requires you to define custom colors:

```
public function colors(): array
{
    return [
        'orange-rgb' => ['rgb', 255, 165, 0],
        'blue-cmyk' => ['cmyk', 100, 100, 0, 0],
    ];
}
```

You can use the color with:

```
$writer->useColor('orange-rgb');
```

or as a parameter when using a font:

```
$writer->useFont('OpenSans-Regular', 12, 'blue-cmyk');
```

### Tables

[](#tables)

To write a table you can follow this example:

```
$items = [
	['first_name' => 'John', 'last_name' => 'Doe'],
	['first_name' => 'Jane','last_name' => 'Doe'],
];

$table = $writer
	->setPosition(10, 150)
	->newTable($items);

$table
	->addColumn(50)
	->addColumn(50)
	->withHeader(['First name', 'Last name'])
	->place("stroke={ {line=horother linewidth=0}}")
;
```

### Images

[](#images)

You can place images with:

```
$writer->drawImage('/path/to/the/image.jpeg', 150, 100);
```

This places an image with and resize it to 150x100.

Since loading rounded images is just a pain in PDFlib, you can use the method:

```
$writer->circleImage('/path/to/the/image.jpeg', 100);
```

### PDFlib functions

[](#pdflib-functions)

Since this package extending PDFlib, you can use the whole PDFlib toolkit. The [PDFlib Cookbook](https://www.pdflib.com/pdflib-cookbook/) helps a lot, even to understand this package.

Extending
---------

[](#extending)

This package is just a basic beginning of wrapping PDFlib. Since PDFlib brings so much more functionality, we have to put the focus on the most used functions in the beginning.

You're welcome to PR your ideas!

Customization
-------------

[](#customization)

If you want to use a filesystem disk / path other than the config in a specific document, you can use the following concerns:

- `Contoweb\Pdflib\Concerns\DifferentExportLocation`: Custom export location
- `Contoweb\Pdflib\Concerns\DifferentFontsLocation`: Custom location for fonts
- `Contoweb\Pdflib\Concerns\DifferentTemplateLocation`: Custom template location

The storage and path are defined the same way as in the config file:

```
public function exportLocation(): array
{
	return [
		'disk' => 'other',
		'path' => null,
	];
}

public function fontsLocation(): array
{
	return [
		'disk' => 'other',
		'path' => 'custom-font-directory',
	];
}

public function templateLocation(): array
{
	return [
		'disk' => 'other',
		'path' => 'custom-template-directory',
	];
}
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance82

Actively maintained with recent releases

Popularity26

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 57% 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 ~211 days

Recently: every ~247 days

Total

12

Last Release

96d ago

Major Versions

v0.2.1 → v1.0.02020-09-10

v1.2.0 → v2.0.02022-10-24

v2.1.0 → v3.0.02024-05-23

PHP version history (3 changes)v0.1.0PHP ^7.0

v2.0.0PHP ^7.0|^8.0

v3.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/47742c40118be4ac4dd5fb4903d6c07b795ddeeabc1b28fdd1c42f5bc4c603c8?d=identicon)[fabricecw](/maintainers/fabricecw)

![](https://www.gravatar.com/avatar/5cc5b76c6f97090b07bab0bb6b2e4f1dca79b99c568879731021f2e19ace3765?d=identicon)[matthiascw](/maintainers/matthiascw)

---

Top Contributors

[![fabricecw](https://avatars.githubusercontent.com/u/13394801?v=4)](https://github.com/fabricecw "fabricecw (53 commits)")[![janiscw](https://avatars.githubusercontent.com/u/145035592?v=4)](https://github.com/janiscw "janiscw (22 commits)")[![thomashcw](https://avatars.githubusercontent.com/u/221326282?v=4)](https://github.com/thomashcw "thomashcw (9 commits)")[![matthiascw](https://avatars.githubusercontent.com/u/24254923?v=4)](https://github.com/matthiascw "matthiascw (5 commits)")[![joelrhenisch](https://avatars.githubusercontent.com/u/33358140?v=4)](https://github.com/joelrhenisch "joelrhenisch (2 commits)")[![yoshkin](https://avatars.githubusercontent.com/u/4170784?v=4)](https://github.com/yoshkin "yoshkin (2 commits)")

---

Tags

laravelpdfprintdocumentspdflib

### Embed Badge

![Health badge](/badges/contoweb-laravel-pdflib/health.svg)

```
[![Health](https://phpackages.com/badges/contoweb-laravel-pdflib/health.svg)](https://phpackages.com/packages/contoweb-laravel-pdflib)
```

###  Alternatives

[barryvdh/laravel-dompdf

A DOMPDF Wrapper for Laravel

7.3k87.6M278](/packages/barryvdh-laravel-dompdf)[barryvdh/laravel-snappy

Snappy PDF/Image for Laravel

2.8k24.8M48](/packages/barryvdh-laravel-snappy)[elibyy/tcpdf-laravel

tcpdf support for Laravel 6, 7, 8, 9, 10, 11

3542.7M5](/packages/elibyy-tcpdf-laravel)[lucasromanojf/laravel5-pdf

Provides the HTML2PDF functionality using the wkhtmltopdf library (Laravel 5)

1271.8k](/packages/lucasromanojf-laravel5-pdf)

PHPackages © 2026

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