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
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
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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.