Quick Start¶
Minimal example: an export class with a Word template and global tokens, then download via the facade.
1. Template¶
Create a Word file (e.g. storage/app/templates/hello.docx) with:
2. Export class¶
Create a class that implements FromWordTemplate and GlobalTokens. For a single block you can also use TokensFromCollection.
namespace App\Http\Export;
use Santwer\Exporter\Concerns\FromWordTemplate;
use Santwer\Exporter\Concerns\GlobalTokens;
use Santwer\Exporter\Concerns\TokensFromCollection;
use Illuminate\Support\Collection;
class HelloExport implements FromWordTemplate, GlobalTokens, TokensFromCollection
{
public function wordTemplateFile(): string
{
return 'templates/hello.docx';
}
public function values(): array
{
return [
'Title' => 'Quick Start Export',
];
}
public function blockName(): string
{
return 'greeting';
}
public function items(): Collection
{
return collect([
['name' => 'Jane', 'email' => 'jane@example.com'],
['name' => 'Bob', 'email' => 'bob@example.com'],
]);
}
public function itemTokens($item): array
{
return $item;
}
}
3. Download¶
In a controller or route:
use Santwer\Exporter\Facade\WordExport;
use App\Http\Export\HelloExport;
return WordExport::download(new HelloExport(), 'hello.docx');
The response is a file download. Use .pdf as extension to get PDF (requires LibreOffice). See Export Classes and Template Syntax for more.
Security Note¶
All text values are automatically escaped for XML safety. Special characters like &, <, >, ", ' are converted to XML entities. This prevents injection vulnerabilities. See XML Escaping & Security for details on allowTags mode for formatted content.