zaynasheff/document-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 soffice is available in your system PATH, 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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固