PHP code example of kovami / htmldocx

1. Go to this page and download the library: Download kovami/htmldocx 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/ */

    

kovami / htmldocx example snippets


use Kovami\HtmlDocx\HtmlDocx;

$docx = HtmlDocx::plain()->fromHtml('<h1>Report</h1><p>Hello <b>world</b></p>')->toDocx();
$html = HtmlDocx::plain()->fromDocx($docx)->toHtml();

use Kovami\HtmlDocx\Editor;

$html = HtmlDocx::for(Editor::CKEditor)->fromDocxFile('report.docx')->toHtml();

use Kovami\HtmlDocx\Config\PageLayout;
use Kovami\HtmlDocx\HtmlDocx;
use Kovami\HtmlDocx\Options;

$converter = HtmlDocx::plain(new Options(
    language: 'en-GB',
    pageLayout: PageLayout::a4Portrait(marginCm: 2.0),
));

$bytes = $converter->fromHtml($html)->toDocx();            // the .docx as a string
$converter->fromHtml($html)->saveDocx('/tmp/out.docx');    // straight to a file
$converter->fromHtml($html)->streamDocx($stream);          // straight to a stream
$converter->fromHtmlFile('/tmp/in.html')->toDocx();        // from a file

$converter->fromHtml($html, PageLayout::a4Landscape())->toDocx();

$html = $converter->fromDocx($bytes)->toHtml();
$html = $converter->fromDocxFile('/tmp/report.docx')->toHtml();
$html = $converter->fromDocxStream($stream)->toHtml();
$converter->fromDocxFile('/tmp/report.docx')->saveHtml('/tmp/report.html');

$html = HtmlDocx::plain(new Options(fullHtmlDocument: true))->fromDocxFile('/tmp/report.docx')->toHtml();

$document = $converter->fromDocx($bytes)->document();   // .docx → Kovami\HtmlDocx\Model\Document
$document = $converter->fromHtml($html)->document();    // HTML  → Document

$converter->fromDocument($document)->toDocx();          // Document → .docx bytes
$converter->fromDocument($document)->toHtml();          // Document → HTML

$converter = HtmlDocx::plain()
    ->withOptions(new Options(language: 'de-DE'))
    ->withLocalImageBaseDir(__DIR__.'/public')
    ->withWarningHandler(static fn (string $message) => Log::info($message));

HtmlDocx::plain();                   // standard HTML, for any editor or none
HtmlDocx::for(Editor::CKEditor);     // CKEditor 5
HtmlDocx::for(Editor::TinyMce);      // TinyMCE
HtmlDocx::for(Editor::TipTap);       // TipTap / ProseMirror
HtmlDocx::for(Editor::SunEditor);    // SunEditor 3
HtmlDocx::for('tinymce');            // by name, case-insensitive, aliases 

new Options(fontFamily: 'Times New Roman', fontSizePt: 12.0, textColor: '222222');
new Options(extraStylesheet: 'body { font-family: Georgia; } p { margin: 0 0 12px; }');

$options = (new Options(fontFamily: 'Georgia'))->with(['cssUnit' => 'pt', 'idPrefix' => 'doc1-']);

$converter = HtmlDocx::plain()
    // relative paths are resolved inside this directory only, never above it
    ->withLocalImageBaseDir(public_path())
    // http(s) images go through your fetcher, where your allowlist and timeouts live
    ->withRemoteImages(fn (string $url): ?string => Http::timeout(5)->get($url)->body());

use Kovami\HtmlDocx\Image\CallbackImageHandler;
use Kovami\HtmlDocx\Model\ImageData;

$converter = HtmlDocx::plain()->withImageHandler(new CallbackImageHandler(
    function (ImageData $image, string $description): ?string {
        Storage::put($path = "media/{$image->hash()}.{$image->extension}", $image->bytes);

        return Storage::url($path);   // or null to leave the picture out
    },
));

$converter = HtmlDocx::plain()->withWarningHandler(function (string $message): void {
    Log::notice("htmldocx: {$message}");
});

// Laravel — app/Providers/AppServiceProvider.php
use Kovami\HtmlDocx\Config\PageLayout;
use Kovami\HtmlDocx\Editor;
use Kovami\HtmlDocx\HtmlDocx;
use Kovami\HtmlDocx\Options;

$this->app->singleton(HtmlDocx::class, fn (): HtmlDocx => HtmlDocx::for(Editor::CKEditor, new Options(
    fontFamily: config('documents.font_family'),
    fontSizePt: config('documents.font_size_pt'),
    language: config('app.locale'),
    pageLayout: PageLayout::fromArray(config('documents.page', [])),
))
    ->withLocalImageBaseDir(public_path())
    ->withWarningHandler(static fn (string $message) => Log::notice("htmldocx: {$message}")));

PageLayout::fromArray(['size' => 'a4', 'orientation' => 'landscape', 'margin_cm' => 1.5]);