zaynasheff/document-generator
Composer 安装命令:
composer require zaynasheff/document-generator
包简介
Generate DOCX and PDF documents from DOCX templates using PHPWord and LibreOffice.
README 文档
README
Generate DOCX and PDF documents from Microsoft Word templates using PHPWord and LibreOffice.
Document Generator provides a simple and fluent API for generating DOCX documents from templates and optionally converting them to PDF.
Features
- Generate DOCX documents from Microsoft Word templates
- Replace template placeholders
- Convert generated DOCX files to PDF
- Fluent and expressive API
- Laravel package auto-discovery
- Configurable LibreOffice binary
- PHPUnit tested
- PHPStan (max level)
- Laravel Pint
- GitHub Actions CI
Requirements
- PHP 7.4+
- Laravel 8+
- LibreOffice (required only for PDF generation)
Installation
Install the package using Composer:
composer require zaynasheff/document-generator
Publish the configuration file:
php artisan vendor:publish --tag=document-generator-config
Configuration
Set the path to the LibreOffice executable in your .env file.
Default:
DOCUMENT_GENERATOR_OFFICE_BINARY=soffice
macOS
DOCUMENT_GENERATOR_OFFICE_BINARY=/Applications/LibreOffice.app/Contents/MacOS/soffice
Windows
DOCUMENT_GENERATOR_OFFICE_BINARY="C:\Program Files\LibreOffice\program\soffice.exe"
Linux
DOCUMENT_GENERATOR_OFFICE_BINARY=/usr/bin/soffice
Note
If
sofficeis available in your systemPATH, you can simply use:DOCUMENT_GENERATOR_OFFICE_BINARY=soffice
Configuration File
return [ 'libreoffice' => [ 'binary' => env( 'DOCUMENT_GENERATOR_OFFICE_BINARY', 'soffice' ), 'timeout' => 60, ], ];
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
$result = DocumentGenerator::make() ->template($template) ->values($values) ->docx() ->output($output) ->generate();
Generate PDF Only
$result = DocumentGenerator::make() ->template($template) ->values($values) ->pdf() ->output($output) ->generate();
Generation Result
The generate() method returns a GenerationResult instance.
$result->hasDocx(); $result->docxPath(); $result->hasPdf(); $result->pdfPath();
Example:
if ($result->hasPdf()) { return response()->download( $result->pdfPath() ); }
API Reference
template(string $template)
Sets the DOCX template file.
->template($template)
values(array $values)
Sets template placeholder values.
->values([ 'FIRST_NAME' => 'John', 'LAST_NAME' => 'Smith', ])
docx()
Enables DOCX generation.
->docx()
pdf()
Enables PDF generation.
->pdf()
output(string $directory)
Sets the output directory.
->output(storage_path('documents'))
generate()
Generates the requested document(s) and returns a GenerationResult.
$result = DocumentGenerator::make() ->template($template) ->values($values) ->docx() ->output($output) ->generate();
Testing
Run PHPUnit:
composer test
Run PHPStan:
composer analyse
Run Pint:
composer format:test
Run all quality checks:
composer quality
Automatically fix code style:
composer fix
Contributing
Contributions are welcome.
Before opening a Pull Request, please make sure all quality checks pass:
composer quality
License
The MIT License (MIT).
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-02