PHP code example of maestrodimateo / simple-word

1. Go to this page and download the library: Download maestrodimateo/simple-word library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maestrodimateo / simple-word example snippets


word()->template('order.docx')
    ->set('name', 'John Doe')
    ->set('ref', 'CMD-001')
    ->set('date', now()->format('d/m/Y'))
    ->save(storage_path('app/orders/order-john.docx'));

word()->template('order.docx')
    ->set([
        'name' => 'John Doe',
        'ref'  => 'CMD-001',
        'date' => now()->format('d/m/Y'),
    ])
    ->download('order.docx');

word()->template('contract.docx')
    ->set([
        'client' => [
            'name'  => 'John Doe',
            'email' => '[email protected]',
            'address' => [
                'city' => 'Paris',
                'zip'  => '75001',
            ],
        ],
        'date' => now()->format('d/m/Y'),
    ])
    ->download('contract.docx');

word()->template('invoice.docx')
    ->table('items', [
        ['produit' => 'Widget A', 'prix' => '100 EUR'],
        ['produit' => 'Widget B', 'prix' => '200 EUR'],
        ['produit' => 'Widget C', 'prix' => '50 EUR'],
    ])
    ->download('invoice.docx');

word()->template('report.docx')
    ->set('company', 'Acme Corp')
    ->image('logo', storage_path('app/logo.png'), width: 150, height: 50)
    ->image('signature', storage_path('app/signature.png'))
    ->save(storage_path('app/report.docx'));

word()->template('report.docx')
    ->set('company', 'Acme Corp')
    ->image('logo', 'https://alfresco.example.com/api/-default-/public/alfresco/versions/1/nodes/abc-123/content', width: 150)
    ->download('report.docx');

word()->template('report.docx')
    ->set('company', 'Acme Corp')
    ->imageFromDisk('logo', 'alfresco', 'logos/company.png', width: 150, height: 50)
    ->imageFromDisk('signature', 's3', 'signatures/ceo.png')
    ->download('report.docx');

word()->template('offer.docx')
    ->set('client', 'Jane Doe')
    ->when('has_discount', $client->is_vip)
    ->when('has_warranty', $product->warranty_months > 0)
    ->download('offer.docx');

namespace App\Word;

use Maestrodimateo\SimpleWord\DocumentBuilder;
use Maestrodimateo\SimpleWord\WordTemplate;

class InvoiceDocument extends WordTemplate
{
    protected string $template = 'invoice.docx';

    public function __construct(
        private Invoice $invoice,
        private Collection $lines,
    ) {}

    public function build(DocumentBuilder $builder): void
    {
        $builder
            ->set([
                'client' => [
                    'name'  => $this->invoice->client->name,
                    'email' => $this->invoice->client->email,
                ],
                'date'  => $this->invoice->date->format('d/m/Y'),
                'total' => number_format($this->invoice->total, 2) . ' EUR',
            ])
            ->table('lines', $this->lines->map(fn ($l) => [
                'description' => $l->description,
                'quantity'    => $l->quantity,
                'price'       => number_format($l->price, 2) . ' EUR',
            ])->all())
            ->image('logo', storage_path('app/logo.png'), width: 120, height: 40)
            ->when('has_notes', $this->invoice->notes !== null);
    }
}

// In a controller
return word()->generate(new InvoiceDocument($invoice, $lines))
    ->download('invoice.docx');

// Save to disk
word()->generate(new InvoiceDocument($invoice, $lines))
    ->saveTo('s3', "invoices/{$invoice->id}.docx");

word()->template('contract.docx')
    ->set('name', 'John Doe')
    ->toPdf()
    ->download('contract.pdf');

use Maestrodimateo\SimpleWord\Contracts\PdfConverter;

class CloudConvertPdf implements PdfConverter
{
    public function convert(string $docxPath, string $pdfPath): void
    {
        // Your conversion logic
    }
}

// Use it directly
word()->template('doc.docx')
    ->set('key', 'value')
    ->toPdf(new CloudConvertPdf())
    ->download('doc.pdf');

// Save locally
->save(storage_path('app/document.docx'));

// Save to S3
->saveTo('s3', 'documents/2026/contract.docx');

// Download
return ->download('contract.docx');

// Preview in browser
return ->stream('contract.docx');

$builder = word()->template('complex.docx');

$processor = $builder->getProcessor();
$processor->cloneBlock('repeating_section', 3);

$builder->save('output.docx');

use Maestrodimateo\SimpleWord\Facades\Word;

Word::template('doc.docx')->set('key', 'value')->save('output.docx');
Word::generate(new InvoiceDocument($invoice, $lines))->download('invoice.docx');

word()->template('doc.docx')->set('key', 'value')->save('output.docx');

// config/word.php
return [
    // Default directory for .docx templates
    'templates_path' => env('WORD_TEMPLATES_PATH', resource_path('templates/word')),

    // PDF conversion
    'pdf' => [
        'driver'           => env('WORD_PDF_DRIVER', 'libreoffice'),
        'libreoffice_path' => env('LIBREOFFICE_PATH', '/usr/bin/libreoffice'),
        'gotenberg_url'    => env('GOTENBERG_URL', 'http://localhost:3000'),
    ],
];
bash
php artisan vendor:publish --tag=word-config
bash
php artisan make:word-template InvoiceDocument