PHPackages                             zaynasheff/document-generator - 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. [Templating &amp; Views](/categories/templating)
4. /
5. zaynasheff/document-generator

ActiveLibrary[Templating &amp; Views](/categories/templating)

zaynasheff/document-generator
=============================

Generate DOCX documents and PDF packages from Microsoft Word templates.

v0.5.1(1mo ago)014MITPHPPHP ^7.4 || ^8.0CI passing

Since Jul 2Pushed 1mo agoCompare

[ Source](https://github.com/zaynasheff/document-generator)[ Packagist](https://packagist.org/packages/zaynasheff/document-generator)[ Docs](https://github.com/zaynasheff/document-generator)[ RSS](/packages/zaynasheff-document-generator/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (5)Dependencies (18)Versions (8)Used By (0)

Document Generator
==================

[](#document-generator)

[![Latest Version on Packagist](https://camo.githubusercontent.com/0d9bd12d8e5041981e57d3331d7a4ed8bccc312438cf20d915668ad806bb8af2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a61796e6173686566662f646f63756d656e742d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](...)[![Tests](https://camo.githubusercontent.com/fabf94623cacd826f94332f938bbd5d499865328564b6702257e1b074d9a00ed/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7a61796e6173686566662f646f63756d656e742d67656e657261746f722f74657374732e796d6c3f6272616e63683d6d61696e)](...)[![PHP Version](https://camo.githubusercontent.com/ef039316a528fbcfcdb5e4b02f1bdcc6cdeba4f609f3d5730e754384c899f06a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7a61796e6173686566662f646f63756d656e742d67656e657261746f72)](...)[![License](https://camo.githubusercontent.com/71720e40e2e3d5c9e5acb01cdc274cb543c8d3fda2accee7de6b3d9b8dc0f491/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7a61796e6173686566662f646f63756d656e742d67656e657261746f72)](...)

Generate **DOCX**, **PDF**, and **merged PDF packages** from Microsoft Word templates using **PHPWord** and **LibreOffice**.

Document Generator is a Laravel package that provides a fluent API for generating documents from DOCX templates, converting them to PDF, generating multiple documents in a single operation, and merging them into one PDF package.

Contents
--------

[](#contents)

- Features
- Installation
- Configuration
- Basic Usage
- Package Generation
- API Reference
- Testing
- Contributing
- License

---

Features
--------

[](#features)

- Generate DOCX documents from Microsoft Word templates
- Replace template placeholders
- Generate PDF documents using LibreOffice
- Generate multiple documents in a single operation
- Merge generated PDF documents into a single package
- Custom output filenames
- Fluent and expressive API
- Laravel auto-discovery
- Configurable LibreOffice executable
- PHPUnit tested
- PHPStan (max level)
- Laravel Pint
- GitHub Actions ready

---

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

[](#requirements)

- PHP 7.4 or higher
- Laravel 8+
- LibreOffice (required only for PDF generation)

---

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

[](#installation)

Install the package using Composer.

```
composer require zaynasheff/document-generator
```

Publish the configuration file.

```
php artisan vendor:publish --tag=document-generator-config
```

---

Configuration
-------------

[](#configuration)

Set the LibreOffice executable in your `.env`.

Default:

```
DOCUMENT_GENERATOR_OFFICE_BINARY=soffice
DOCUMENT_GENERATOR_OFFICE_PROFILE=
DOCUMENT_GENERATOR_TIMEOUT=60
```

### macOS

[](#macos)

```
DOCUMENT_GENERATOR_OFFICE_BINARY=/Applications/LibreOffice.app/Contents/MacOS/soffice
```

### Linux

[](#linux)

```
DOCUMENT_GENERATOR_OFFICE_BINARY=/usr/bin/soffice
```

### Windows

[](#windows)

```
DOCUMENT_GENERATOR_OFFICE_BINARY="C:\Program Files\LibreOffice\program\soffice.exe"
```

If LibreOffice is available in your system `PATH`, simply use:

```
DOCUMENT_GENERATOR_OFFICE_BINARY=soffice
```

`DOCUMENT_GENERATOR_OFFICE_PROFILE` is optional. It can be used in Docker, PHP-FPM or other headless environments where LibreOffice cannot initialize its default user profile.

Example:

```
DOCUMENT_GENERATOR_OFFICE_PROFILE="/tmp/document-generator-profile"
```

---

Configuration File
------------------

[](#configuration-file)

```
return [

    'libreoffice' => [

        'binary' => env(
            'DOCUMENT_GENERATOR_OFFICE_BINARY',
            'soffice'
        ),

        'timeout' => env(
            'DOCUMENT_GENERATOR_TIMEOUT',
            60
        ),
        'profile' => env(
            'DOCUMENT_GENERATOR_OFFICE_PROFILE',
        ),

    ]

];
```

---

Basic Usage
-----------

[](#basic-usage)

Generate both DOCX and PDF.

```
use Zaynasheff\DocumentGenerator\DocumentGenerator;

$result = DocumentGenerator::make()
    ->template(
        storage_path('templates/contract.docx')
    )
    ->values([
        'FIRST_NAME' => 'John',
        'LAST_NAME'  => 'Anderson',
        'CITY'       => 'Berlin',
    ])
    ->docx()
    ->pdf()
    ->output(
        storage_path('documents')
    )
    ->generate();
```

---

Generate DOCX Only
------------------

[](#generate-docx-only)

Generate a Microsoft Word document without creating a PDF.

```
$result = DocumentGenerator::make()
    ->template($template)
    ->values($values)
    ->docx()
    ->output($output)
    ->generate();
```

---

Generate PDF Only
-----------------

[](#generate-pdf-only)

Generate only a PDF document.

```
$result = DocumentGenerator::make()
    ->template($template)
    ->values($values)
    ->pdf()
    ->output($output)
    ->generate();
```

---

Generate DOCX and PDF
---------------------

[](#generate-docx-and-pdf)

Generate both formats in a single operation.

```
$result = DocumentGenerator::make()
    ->template($template)
    ->values($values)
    ->docx()
    ->pdf()
    ->output($output)
    ->generate();
```

---

Custom Output Filename
----------------------

[](#custom-output-filename)

By default the generated filename is based on the template filename.

Use `name()` to specify your own filename.

```
$result = DocumentGenerator::make()
    ->template($template)
    ->values($values)
    ->name('contract_001')
    ->pdf()
    ->output($output)
    ->generate();
```

Generated files:

```
contract_001.docx
contract_001.pdf

```

---

Generation Result
-----------------

[](#generation-result)

The `generate()` method returns a `GenerationResult`.

```
$result->hasDocx();

$result->docxPath();

$result->hasPdf();

$result->pdfPath();
```

Example:

```
if ($result->hasPdf()) {

    return response()->download(
        $result->pdfPath()
    );

}
```

---

Multiple Placeholder Values
---------------------------

[](#multiple-placeholder-values)

Template placeholders are passed as an associative array.

```
$result = DocumentGenerator::make()
    ->template($template)
    ->values([
        'FIRST_NAME' => 'John',
        'LAST_NAME'  => 'Anderson',
        'CITY'       => 'Berlin',
        'EMAIL'      => 'john@example.com',
        'PHONE'      => '+1 555 123 45 67',
    ])
    ->pdf()
    ->output($output)
    ->generate();
```

---

Supported Placeholder Types
---------------------------

[](#supported-placeholder-types)

The following value types are supported:

- string
- integer
- float
- boolean
- null

Example:

```
->values([
    'NAME'      => 'John',
    'AGE'       => 35,
    'BALANCE'   => 1500.75,
    'ACTIVE'    => true,
    'COMMENT'   => null,
])
```

---

Package Generation
==================

[](#package-generation)

Generate multiple documents in a single operation.

```
use Zaynasheff\DocumentGenerator\DocumentPackage;

$package = DocumentPackage::make();

$package->output(
    storage_path('documents')
);

$package
    ->addDocument()
    ->template(
        storage_path('templates/contract.docx')
    )
    ->values([
        'FIRST_NAME' => 'John',
        'LAST_NAME'  => 'Anderson',
    ])
    ->name('contract')
    ->pdf();

$package
    ->addDocument()
    ->template(
        storage_path('templates/invoice.docx')
    )
    ->values([
        'FIRST_NAME' => 'John',
    ])
    ->name('invoice')
    ->pdf();

$result = $package->generate();
```

Generated files:

```
documents/
    contract.docx
    contract.pdf

    invoice.docx
    invoice.pdf

```

---

Merge PDF
=========

[](#merge-pdf)

Automatically merge all generated PDF files into a single document.

```
$package = DocumentPackage::make();

$package
    ->output(
        storage_path('documents')
    )
    ->name('package')
    ->mergePdf();

$package
    ->addDocument()
    ->template($contractTemplate)
    ->values($contractValues)
    ->name('contract')
    ->pdf();

$package
    ->addDocument()
    ->template($invoiceTemplate)
    ->values($invoiceValues)
    ->name('invoice')
    ->pdf();

$result = $package->generate();
```

Generated files:

```
documents/

    contract.docx
    contract.pdf

    invoice.docx
    invoice.pdf

    package.pdf

```

---

Multiple Copies
---------------

[](#multiple-copies)

Generate multiple copies of the same document.

```
$package = DocumentPackage::make();

$package
    ->output(storage_path('documents'));

$package
    ->addDocument()
    ->template($template)
    ->values($values)
    ->name('contract')
    ->copies(3)
    ->pdf();

$result = $package->generate();
```

Generated files:

```
contract.pdf
contract_2.pdf
contract_3.pdf

```

Copies are generated in the specified order and are automatically included in merged PDF packages.

---

Blank Pages
-----------

[](#blank-pages)

Insert blank pages between generated documents when creating a merged PDF.

```
use Zaynasheff\DocumentGenerator\DocumentPackage;

$package = DocumentPackage::make();

$package
    ->output(storage_path('documents'))
    ->name('contracts')
    ->mergePdf();

$package
    ->addDocument()
    ->template($template1)
    ->values($values1)
    ->pdf();

$package->addBlankPage();

$package
    ->addDocument()
    ->template($template2)
    ->values($values2)
    ->pdf();

$result = $package->generate();
```

Blank pages are supported only when PDF merging is enabled.

If a blank page is added without calling `mergePdf()`, a `DocumentGeneratorException` will be thrown.

---

Package Result
==============

[](#package-result)

Package generation returns a `PackageResult`.

```
$result->count();

$result->results();

$result->hasMergedPdf();

$result->mergedPdfPath();
```

Example:

```
if ($result->hasMergedPdf()) {

    return response()->download(
        $result->mergedPdfPath()
    );

}
```

---

Complete Package Example
========================

[](#complete-package-example)

```
use Zaynasheff\DocumentGenerator\DocumentPackage;

$package = DocumentPackage::make();

$package
    ->output(
        storage_path('documents')
    )
    ->name('contracts')
    ->mergePdf();

$package
    ->addDocument()
    ->template(
        storage_path('templates/contract.docx')
    )
    ->values([
        'FIRST_NAME' => 'John',
        'LAST_NAME'  => 'Anderson',
        'CITY'       => 'Berlin',
    ])
    ->name('contract')
    ->pdf();

$package
    ->addDocument()
    ->template(
        storage_path('templates/invoice.docx')
    )
    ->values([
        'FIRST_NAME' => 'John',
        'AMOUNT'     => '1500 €',
    ])
    ->name('invoice')
    ->pdf();

$result = $package->generate();

if ($result->hasMergedPdf()) {

    return response()->download(
        $result->mergedPdfPath()
    );

}
```

---

Current Capabilities
====================

[](#current-capabilities)

✅ DOCX generation

✅ PDF generation

✅ Custom output filenames

✅ Multiple document generation

✅ PDF package generation

✅ Merged PDF packages

✅ Fluent API

✅ Laravel auto-discovery

---

Why Document Generator?
=======================

[](#why-document-generator)

Document Generator focuses on simplicity and readability.

```
$result = DocumentPackage::make()
    ->output($output)
    ->name('contracts')
    ->mergePdf();

$result
    ->addDocument()
    ->template($contract)
    ->values($contractData)
    ->pdf();

$result
    ->addDocument()
    ->template($invoice)
    ->values($invoiceData)
    ->pdf();

$result = $result->generate();
```

The package hides all low-level details of DOCX generation, PDF conversion and PDF merging behind a clean, fluent API.

---

API Reference
=============

[](#api-reference)

DocumentGenerator
-----------------

[](#documentgenerator)

### template(string $template)

[](#templatestring-template)

Sets the DOCX template file.

```
->template($template)
```

---

### values(array $values)

[](#valuesarray-values)

Sets template placeholder values.

```
->values([
    'FIRST_NAME' => 'John',
    'LAST_NAME'  => 'Smith',
])
```

---

### name(string $name)

[](#namestring-name)

Sets the output filename without extension.

```
->name('contract')
```

---

### docx()

[](#docx)

Enables DOCX generation.

```
->docx()
```

---

### pdf()

[](#pdf)

Enables PDF generation.

```
->pdf()
```

---

### output(string $directory)

[](#outputstring-directory)

Sets the output directory.

```
->output(storage_path('documents'))
```

---

### generate()

[](#generate)

Generates the requested document(s).

```
$result = DocumentGenerator::make()
    ->template($template)
    ->values($values)
    ->pdf()
    ->output($output)
    ->generate();
```

---

DocumentPackage
===============

[](#documentpackage)

### addDocument()

[](#adddocument)

Adds a document to the package.

```
$package->addDocument();
```

---

### output(string $directory)

[](#outputstring-directory-1)

Sets the package output directory.

```
$package->output(
    storage_path('documents')
);
```

---

### name(string $name)

[](#namestring-name-1)

Sets the merged PDF filename.

```
$package->name('contracts');
```

Produces:

```
contracts.pdf

```

---

### mergePdf()

[](#mergepdf)

Enables automatic PDF merging.

```
$package->mergePdf();
```

---

### generate()

[](#generate-1)

Generates the complete package.

```
$result = $package->generate();
```

---

GenerationResult
================

[](#generationresult)

```
$result->hasDocx();

$result->docxPath();

$result->hasPdf();

$result->pdfPath();
```

---

PackageResult
=============

[](#packageresult)

```
$result->count();

$result->results();

$result->hasMergedPdf();

$result->mergedPdfPath();
```

---

Error Handling
==============

[](#error-handling)

All package exceptions extend:

```
Zaynasheff\DocumentGenerator\Exceptions\DocumentGeneratorException
```

Example:

```
use Zaynasheff\DocumentGenerator\Exceptions\DocumentGeneratorException;

try {

    $result = DocumentGenerator::make()
        ->template($template)
        ->pdf()
        ->output($output)
        ->generate();

} catch (DocumentGeneratorException $exception) {

    report($exception);

}
```

---

Testing
=======

[](#testing)

Run PHPUnit.

```
composer test
```

Run PHPStan.

```
composer analyse
```

Run Laravel Pint.

```
composer format:test
```

Run the complete quality pipeline.

```
composer quality
```

Automatically fix coding style.

```
composer fix
```

---

Contributing
============

[](#contributing)

Contributions are welcome.

Before opening a Pull Request, please make sure all quality checks pass.

```
composer quality
```

Please follow the existing coding style and architecture.

---

Roadmap
=======

[](#roadmap)

The following features are planned for future releases.

- Blank pages inside document packages
- Multiple document copies
- ZIP package generation
- Watermarks
- Page numbering
- Digital signatures

---

License
=======

[](#license)

The MIT License (MIT).

---

Made with ❤️ for the Laravel community.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance92

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 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 ~1 days

Total

7

Last Release

42d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16214406?v=4)[zaynasheff](/maintainers/zaynasheff)[@zaynasheff](https://github.com/zaynasheff)

---

Top Contributors

[![zaynasheff](https://avatars.githubusercontent.com/u/16214406?v=4)](https://github.com/zaynasheff "zaynasheff (41 commits)")

---

Tags

laravelpdfgeneratortemplateworddocxofficePhpWorddocumentLibreOfficedocuments

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zaynasheff-document-generator/health.svg)

```
[![Health](https://phpackages.com/badges/zaynasheff-document-generator/health.svg)](https://phpackages.com/packages/zaynasheff-document-generator)
```

###  Alternatives

[phpoffice/phpword

PHPWord - A pure PHP library for reading and writing word processing documents (OOXML, ODF, RTF, HTML, PDF)

7.6k40.7M265](/packages/phpoffice-phpword)

PHPackages © 2026

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