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
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
},
));