Kotchasan Framework Documentation

Kotchasan Framework Documentation

Htmldoc

EN 05 Feb 2026 07:52

Htmldoc

\Kotchasan\Htmldoc generates Microsoft Word documents from HTML.

Usage

use Kotchasan\Htmldoc;

$doc = new Htmldoc();

$html = '<html>
<head><title>Monthly Report</title></head>
<body>
    <h1>Sales Report</h1>
    <p>January 2024</p>
    <table border="1">
        <tr><th>Product</th><th>Quantity</th></tr>
        <tr><td>Product A</td><td>100</td></tr>
        <tr><td>Product B</td><td>200</td></tr>
    </table>
</body>
</html>';

// Download as Word file
$doc->createDoc($html, 'sales_report');

Constructor

$doc = new Htmldoc();

Defaults:

  • htmlHead: Empty
  • htmlBody: Empty
  • docFile: 'Untitled.doc'

Methods

createDoc()

Create and download Word document.

$doc->createDoc($html, $file = '');
Parameter Type Description
$html string HTML content
$file string Filename (without .doc). If empty, uses <title> content.

setDocFileName()

Set filename.

$doc->setDocFileName('monthly_report');
// Result: monthly_report.doc

Returns: $this (for method chaining)

Usage Examples

Simple Report

use Kotchasan\Htmldoc;

$doc = new Htmldoc();

$html = <<<HTML
<html>
<head>
    <title>Employee Report</title>
    <style>
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid black; padding: 8px; }
        th { background-color: #f0f0f0; }
    </style>
</head>
<body>
    <h1>Employee Report</h1>
    <p>Date: 1 February 2024</p>

    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Department</th>
        </tr>
        <tr>
            <td>E001</td>
            <td>John Doe</td>
            <td>Engineering</td>
        </tr>
        <tr>
            <td>E002</td>
            <td>Jane Smith</td>
            <td>Marketing</td>
        </tr>
    </table>
</body>
</html>
HTML;

$doc->createDoc($html, 'employee_report');

Export from Template

use Kotchasan\Htmldoc;
use Kotchasan\Template;

// Load template
$template = Template::create('report', 'report_template');
$template->add([
    'TITLE' => 'Sales Report',
    'DATE' => date('d/m/Y'),
    'CONTENT' => $tableContent
]);

// Create Word document
$doc = new Htmldoc();
$doc->createDoc($template->render(), 'sales_report');

Method Chaining

(new Htmldoc())
    ->setDocFileName('custom_report')
    ->createDoc($html);

Financial Report Export

use Kotchasan\Htmldoc;
use Kotchasan\Model;

// Retrieve data
$orders = Model::createQuery()
    ->select('*')
    ->from('orders')
    ->where(['MONTH(order_date)', date('m')])
    ->execute();

// Create HTML
$rows = '';
$total = 0;
foreach ($orders as $order) {
    $rows .= "<tr>
        <td>{$order->order_id}</td>
        <td>{$order->customer}</td>
        <td>" . number_format($order->amount, 2) . "</td>
    </tr>";
    $total += $order->amount;
}

$html = <<<HTML
<html>
<head><title>Order Report</title></head>
<body>
    <h1>Monthly Order Report</h1>
    <table border="1" cellpadding="5">
        <tr>
            <th>No.</th>
            <th>Customer</th>
            <th>Amount</th>
        </tr>
        $rows
        <tr>
            <td colspan="2"><strong>Total</strong></td>
            <td><strong>" . number_format($total, 2) . "</strong></td>
        </tr>
    </table>
</body>
</html>
HTML;

(new Htmldoc())->createDoc($html, 'orders_' . date('Y-m'));

Page Setup

Generated documents have default page settings:

  • Paper Size: A4 (21cm x 29.7cm)
  • Margins: 1cm on all sides
  • Orientation: Portrait

Limitations

[!NOTE]

  • Supports only MS Word format (.doc)
  • JavaScript in HTML is not supported
  • Some CSS may not render exactly as in browser
  • Images must be absolute URL or base64