ananikomlanmh/word-for-laravel 问题修复 & 功能扩展

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

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

ananikomlanmh/word-for-laravel

Composer 安装命令:

composer require ananikomlanmh/word-for-laravel

包简介

A Laravel package to generate Word documents easily

README 文档

README

Build Status Coding Standards Total Downloads Latest Stable Version License

Generate Word documents (.docx) from Laravel Blade templates using PHPWord under the hood. This package provides an elegant API for creating professional Word documents with the full power of Blade templating.

Table of Contents

Requirements

  • PHP 8.1 or higher
  • Laravel 10.x or higher

Installation

Install the package via Composer:

composer require ananikomlanmh/word-for-laravel

Publish the configuration file (optional):

php artisan vendor:publish --tag="word-for-laravel-config"

Basic Usage

Simple Document Generation

use WordForLaravel\Facades\WordForLaravel;

// Generate and download a document
return WordForLaravel::load('word.invoice', [
        'invoiceNumber' => 'INV-001',
        'client' => 'John Doe',
        'total' => 1500.00
    ])
    ->download('invoice.docx');

Saving to Disk

use Illuminate\Support\Facades\Storage;
use WordForLaravel\Facades\WordForLaravel;

// Save to the default disk
WordForLaravel::load('word.report', $data)
    ->save('reports/monthly-report.docx');

// Save to a specific disk
WordForLaravel::load('word.report', $data)
    ->save('reports/monthly-report.docx', 's3');

// Get the full path
$path = Storage::disk('local')->path('reports/monthly-report.docx');

Setting Document Properties

WordForLaravel::load('word.contract', $data)
    ->setProperties([
        'title' => 'Employment Contract',
        'creator' => 'HR Department',
        'company' => 'ACME Corporation',
        'subject' => 'Contract Agreement',
        'description' => 'Employment contract for new hire'
    ])
    ->download('contract.docx');

Controller Example

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use WordForLaravel\Facades\WordForLaravel;

class DocumentController extends Controller
{
    public function downloadInvoice($id)
    {
        $invoice = Invoice::findOrFail($id);
        
        return WordForLaravel::load('word.invoice', [
                'invoice' => $invoice,
                'client' => $invoice->client,
                'items' => $invoice->items
            ])
            ->setProperties([
                'title' => "Invoice #{$invoice->number}",
                'creator' => config('app.name'),
            ])
            ->download("invoice-{$invoice->number}.docx");
    }
}

Creating Word Templates

Using Artisan Command

Generate a new Blade template specifically for Word documents:

php artisan make:word-template invoice

This creates a file at resources/views/word/invoice.blade.php with a basic structure.

Template Structure

Create a Blade view with HTML structure. The package converts HTML to Word format:

<!-- resources/views/word/invoice.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Invoice</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        h1 {
            color: #2c3e50;
            font-size: 24pt;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <h1>INVOICE #{{ $invoiceNumber }}</h1>
    
    <p><strong>Date:</strong> {{ now()->format('d/m/Y') }}</p>
    <p><strong>Client:</strong> {{ $client }}</p>
    
    <h2>Items</h2>
    <table>
        <thead>
            <tr>
                <th>Description</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Total</th>
            </tr>
        </thead>
        <tbody>
            @foreach($items as $item)
            <tr>
                <td>{{ $item['description'] }}</td>
                <td>{{ $item['quantity'] }}</td>
                <td>{{ number_format($item['price'], 2) }} €</td>
                <td>{{ number_format($item['total'], 2) }} €</td>
            </tr>
            @endforeach
        </tbody>
    </table>
    
    <p><strong>Total: {{ number_format($total, 2) }} €</strong></p>
</body>
</html>

Supported HTML Elements

The package supports standard HTML elements that are converted to Word format:

  • Headings: <h1>, <h2>, <h3>, <h4>, <h5>, <h6>
  • Paragraphs: <p> with inline formatting
  • Inline text: <strong>, <b>, <em>, <i>, <u>, <del>, <s>, <strike>, <mark>, <code>, <small>, <sub>, <sup>, <a>
  • Lists: <ul>, <ol>, <li> (nested lists supported)
  • Tables: <table>, <thead>, <tbody>, <tr>, <th>, <td> (colspan/rowspan basic support)
  • Line breaks and rules: <br>, <hr>

Special utility tags handled by the parser:

  • <pagebreak orientation="..." />: splits the document into sections. Orientation can be landscape, portrait, L, or P.
  • <page-number format="..." restart="..." />: renders dynamic page numbers. Supported variables: {PAGE}, {NUMPAGES}, {SECTIONPAGES}.
  • <wordheader>...</wordheader>: content rendered in the section header.
  • <wordfooter>...</wordfooter>: content rendered in the section footer.

Advanced Usage

Getting Document Content

// Get raw content as string (useful for storage or email attachments)
$content = WordForLaravel::load('word.report', $data)
    ->getContent();

// Store in database or send via email
Mail::send('emails.report', [], function ($message) use ($content) {
    $message->attachData($content, 'report.docx', [
        'mime' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    ]);
});

Accessing PHPWord Instance

For advanced customization, access the underlying PHPWord object:

WordForLaravel::load('word.custom', $data);

$phpWord = WordForLaravel::getPhpWord();

// Add custom section
$section = $phpWord->addSection();
$section->addText('Custom content added directly via PHPWord');

// Download the customized document
return WordForLaravel::download('custom.docx');

Resetting the Converter

When generating multiple documents in the same request:

// First document
WordForLaravel::load('document1', $data1)
    ->save('doc1.docx');

// Reset for new document
WordForLaravel::reset();

// Second document
WordForLaravel::load('document2', $data2)
    ->save('doc2.docx');

Configuration

The configuration file (config/word-for-laravel.php) allows you to customize:

return [
    // Default disk for saving documents
    'default_disk' => env('BLADE_TO_WORD_DISK', 'local'),

    // Temporary directory for document generation
    'temp_dir' => env('BLADE_TO_WORD_TEMP_DIR', sys_get_temp_dir()),

    // Default orientation for sections that don't specify one
    'default_orientation' => env('WORD_DEFAULT_ORIENTATION', 'portrait'),

    // Default section styling (margins in twips: 1000 ≈ 1.76cm)
    'section_style' => [
        'marginTop' => 1000,
        'marginBottom' => 1000,
        'marginLeft' => 1000,
        'marginRight' => 1000,
        'headerHeight' => 720,
        'footerHeight' => 720,
    ],

    // Default document properties
    'default_properties' => [
        'creator' => env('APP_NAME', 'Laravel'),
        'company' => env('APP_NAME', 'Laravel'),
    ],
];

Orientation

Set default page orientation for sections that do not specify one via <pagebreak>:

WordForLaravel::orientation('landscape')
    ->load('word.report', $data)
    ->download('report.docx');

You can also control orientation per section using <pagebreak orientation="landscape" /> in your Blade HTML.

Testing

Run the tests with:

composer test

Run tests with coverage:

composer test-coverage

Examples

Invoice Example

// Controller
public function generateInvoice(Invoice $invoice)
{
    return WordForLaravel::load('word.invoice', [
            'invoice' => $invoice,
            'company' => [
                'name' => 'ACME Corp',
                'address' => '123 Business St',
                'city' => 'Paris',
            ],
            'items' => $invoice->items->map(fn($item) => [
                'description' => $item->description,
                'quantity' => $item->quantity,
                'price' => $item->price,
                'total' => $item->quantity * $item->price,
            ]),
            'total' => $invoice->total,
        ])
        ->setProperties([
            'title' => "Invoice {$invoice->number}",
            'subject' => 'Sales Invoice',
        ])
        ->download("invoice-{$invoice->number}.docx");
}

Report with Tables

<!-- resources/views/word/sales-report.blade.php -->
<!DOCTYPE html>
<html>
<body>
    <h1>Sales Report - {{ $period }}</h1>
    
    <h2>Summary</h2>
    <p>Total Sales: <strong>{{ number_format($totalSales, 2) }} €</strong></p>
    <p>Total Orders: <strong>{{ $totalOrders }}</strong></p>
    
    <h2>Top Products</h2>
    <table>
        <thead>
            <tr>
                <th>Product</th>
                <th>Units Sold</th>
                <th>Revenue</th>
            </tr>
        </thead>
        <tbody>
            @foreach($topProducts as $product)
            <tr>
                <td>{{ $product['name'] }}</td>
                <td>{{ $product['units'] }}</td>
                <td>{{ number_format($product['revenue'], 2) }} €</td>
            </tr>
            @endforeach
        </tbody>
    </table>
</body>
</html>

CSS Support

The converter supports a useful subset of CSS on tags, classes, and inline styles. Examples:

  • Font: font-size, color, font-family, font-weight, font-style, text-decoration, background-color, text-transform
  • Paragraph: text-align, line-height, margin-left (indent), margin-top/margin-bottom (spacing), text-indent
  • Tables/Cells: border-color, border-width, vertical-align, width, background-color
  • Images: width, height, text-align, float, margins

Units supported include px, pt, cm, mm, in. Colors support hex (#RRGGBB), rgb(...), and named colors.

Images

Images can be referenced by:

  • Local path (absolute or relative)
  • Remote URL (downloaded at runtime)
  • Data URI (data:image/png;base64,...)

Alignment and sizing can be controlled via CSS or HTML attributes (width, height).

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email inanakomlan@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

ananikomlanmh/word-for-laravel 适用场景与选型建议

ananikomlanmh/word-for-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 192 次下载、GitHub Stars 达 12, 最近一次更新时间为 2025 年 11 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「document generation」 「PHP Package」 「Word-for-Laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ananikomlanmh/word-for-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ananikomlanmh/word-for-laravel 我们能提供哪些服务?
定制开发 / 二次开发

基于 ananikomlanmh/word-for-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 192
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-22