PHP code example of beziworld / laravel-docx-templates

1. Go to this page and download the library: Download beziworld/laravel-docx-templates 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/ */

    

beziworld / laravel-docx-templates example snippets


use Beziworld\DocxTemplates\Data\Image;
use Beziworld\DocxTemplates\Data\Table;
use Beziworld\DocxTemplates\Facades\DocxTemplates;

$document = DocxTemplates::template('invoice.docx')       // templates/invoice.docx on the template disk
    ->with($invoice->load('customer'))                     // Eloquent model, array, collection or object
    ->set('note', "Payment within 14 days\nThank you")    // one value, dot notation allowed
    ->block('discount', $invoice->discount > 0)            // keep or remove a block
    ->block('payments', $invoice->payments)                // repeat a block per item: ${amount} or ${payments.amount}
    ->rows('item.name', $invoice->items)                   // repeat the row with ${item.name}, ${item.qty}, ...
    ->image('logo', Image::fromDisk('logo.png', 'public')->size(120, 40))
    ->table('summary', Table::make(['Net', 'VAT', 'Gross'], [[$net, $vat, $gross]]))
    ->filename("invoice-{$invoice->number}.docx")
    ->render();

$document->store('invoices/');                 // output disk, returns "invoices/invoice-....docx"
$document->store('archive/x.docx', 's3');      // another disk
$document->save(storage_path('app/x.docx'));   // local path
return $document->download();                  // or ->inline(), or return $document itself
$document->contents();                         // bytes

DocxTemplates::template('contracts/nda.docx', 's3');
DocxTemplates::fromPath(resource_path('templates/nda.docx'));
DocxTemplates::fromContents($uploadedBytes, 'nda.docx');

namespace App\Documents;

use App\Models\Invoice;
use Beziworld\DocxTemplates\TemplateDefinition;

final class InvoiceDocument extends TemplateDefinition
{
    public function __construct(private readonly Invoice $invoice) {}

    public function template(): string
    {
        return 'invoices/invoice.docx';
    }

    public function data(): mixed
    {
        return $this->invoice->load('customer');
    }

    public function rows(): array
    {
        return ['item.name' => $this->invoice->items];
    }

    public function blocks(): array
    {
        return ['discount' => $this->invoice->discount > 0];
    }

    public function 

Route::get('/invoices/{invoice}/docx', fn (Invoice $invoice) => DocxTemplates::render(new InvoiceDocument($invoice)));

DocxTemplates::make(new InvoiceDocument($invoice))->set('note', 'Copy')->pdf()->store('invoices/');

$pdf = DocxTemplates::render(new InvoiceDocument($invoice))->pdf();   // RenderedDocument with application/pdf
$pdf->store('invoices/');
return $pdf->inline();

DocxTemplates::document($bytes, 'upload.docx')->pdf();               // convert an existing document

Process::fake(function (PendingProcess $process) {
    $outdir = $process->command[array_search('--outdir', $process->command) + 1];
    file_put_contents($outdir . '/document.pdf', '%PDF-1.7');

    return Process::result();
});
bash
php artisan make:docx-template InvoiceDocument --template=invoices/invoice.docx
bash
php artisan docx:variables invoice.docx            # placeholders and blocks of a template on the disk
php artisan docx:variables ./draft.docx --local
php artisan make:docx-template InvoiceDocument --template=invoices/invoice.docx