dskripchenko/laravel-php-docx

Laravel bridge for dskripchenko/php-docx — Docx facade, Blade view to DOCX, config-driven page setup, response()->docx() macro, DOCX to HTML import and template-variable detection.

Maintainers

Package info

github.com/dskripchenko/laravel-php-docx

pkg:composer/dskripchenko/laravel-php-docx

Transparency log

Statistics

Installs: 197

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-07-18 18:40 UTC

This package is auto-updated.

Last update: 2026-07-20 09:48:59 UTC


README

Laravel bridge for dskripchenko/php-docx — the zero-dependency, MIT-licensed DOCX ⇄ HTML toolkit. Blade views straight to Word documents, DOCX import back to HTML, template-variable detection — with output validated against the ECMA-376 schemas on every push and reader fidelity tracked on an external corpus.

Tests Latest Version License: MIT PHP Laravel

Installation

composer require dskripchenko/laravel-php-docx

The service provider and the Docx facade register automatically (package discovery). Optionally publish the config:

php artisan vendor:publish --tag=php-docx-config

Usage

Blade view → DOCX

use Dskripchenko\LaravelPhpDocx\Facades\Docx;

// download a rendered Blade view as a Word document
Route::get('/contracts/{contract}', function (Contract $contract) {
    return Docx::view('contracts.show', ['contract' => $contract])
        ->download("contract-{$contract->number}.docx");
});

// header/footer views and a text watermark
Docx::view('contracts.show', $data,
    headerView: 'contracts.header',
    footerView: 'contracts.footer',
    watermark: 'DRAFT',
);

// views with <style> blocks / CSS classes (needs the suggested
// tijsverkoyen/css-to-inline-styles package)
Docx::view('contracts.styled', $data, inlineStyles: true);

HTML → DOCX

$bytes = Docx::fromHtml('<h1>Report</h1><p>Body with <b>bold</b>.</p>')->bytes();

Docx::fromHtml($html)->save(storage_path('report.docx'));

// HTTP responses
return Docx::fromHtml($html)->download('report.docx');   // attachment
return Docx::fromHtml($html)->inline('report.docx');     // inline

// or via the response macro — accepts PendingDocx, Document, or bytes
return response()->docx(Docx::fromHtml($html), 'report.docx');

fromHtml() understands inline style="…" attributes; fromHtmlWithStyles() additionally resolves <style> blocks and CSS classes first.

DOCX → HTML (import)

$imported = Docx::toHtml(file_get_contents('from-word.docx'));

$imported->bodyHtml;        // clean HTML with inline styles
$imported->headerHtml;      // header / footer as HTML
$imported->footerHtml;
$imported->watermarkText;

Reads documents produced by Word, Google Docs, LibreOffice, PHPWord — fidelity is tracked on an external corpus in php-docx's CI.

Template variables

$variables = Docx::detectVariables($docxBytes);
// MERGEFIELD, SDT content controls, {{x}} / ${x} / %x% patterns

// substitute values while importing to HTML
$html = Docx::toHtml($docxBytes, ['customer_name' => 'ACME Corp'])->bodyHtml;

Fluent builder

$document = Docx::builder()
    ->heading(1, 'Quarterly report')
    ->paragraph(fn ($p) => $p->text('Prepared for ')->bold('ACME Corp'))
    ->build();

return Docx::render($document)->download('report.docx');

The full builder API (tables, lists, images, page setup, watermarks) is documented in php-docx.

Configuration

config/php-docx.php:

return [
    'paper' => env('PHP_DOCX_PAPER', 'a4'),            // a3 | a4 | a5 | letter | legal
    'orientation' => env('PHP_DOCX_ORIENTATION', 'portrait'),
    'margins' => [                                      // millimetres; null = library default
        'top' => null, 'right' => null, 'bottom' => null, 'left' => null,
    ],
];

Applied to every document produced through the facade; per-document setups via the native API win.

Related packages

License

MIT.