Kotchasan Framework Documentation

Kotchasan Framework Documentation

Htmldoc

TH 05 Feb 2026 07:52

Htmldoc

\Kotchasan\Htmldoc สำหรับสร้างเอกสาร Microsoft Word จาก HTML

การใช้งาน

use Kotchasan\Htmldoc;

$doc = new Htmldoc();

$html = '<html>
<head><title>รายงานประจำเดือน</title></head>
<body>
    <h1>รายงานยอดขาย</h1>
    <p>เดือนมกราคม 2567</p>
    <table border="1">
        <tr><th>สินค้า</th><th>จำนวน</th></tr>
        <tr><td>สินค้า A</td><td>100</td></tr>
        <tr><td>สินค้า B</td><td>200</td></tr>
    </table>
</body>
</html>';

// ดาวน์โหลดเป็นไฟล์ Word
$doc->createDoc($html, 'sales_report');

Constructor

$doc = new Htmldoc();

ค่าเริ่มต้น:

  • htmlHead: ว่าง
  • htmlBody: ว่าง
  • docFile: 'Untitled.doc'

Methods

createDoc()

สร้างและดาวน์โหลดเอกสาร Word

$doc->createDoc($html, $file = '');
Parameter Type Description
$html string HTML content
$file string ชื่อไฟล์ (ไม่ต้องมี .doc) ถ้าว่างจะใช้จาก <title>

setDocFileName()

กำหนดชื่อไฟล์

$doc->setDocFileName('monthly_report');
// จะเป็น: monthly_report.doc

Returns: $this (สำหรับ method chaining)

ตัวอย่างการใช้งาน

สร้างรายงานอย่างง่าย

use Kotchasan\Htmldoc;

$doc = new Htmldoc();

$html = <<<HTML
<html>
<head>
    <title>รายงานพนักงาน</title>
    <style>
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid black; padding: 8px; }
        th { background-color: #f0f0f0; }
    </style>
</head>
<body>
    <h1>รายงานพนักงาน</h1>
    <p>วันที่: 1 กุมภาพันธ์ 2567</p>

    <table>
        <tr>
            <th>รหัส</th>
            <th>ชื่อ-นามสกุล</th>
            <th>แผนก</th>
        </tr>
        <tr>
            <td>E001</td>
            <td>นายสมชาย ใจดี</td>
            <td>วิศวกรรม</td>
        </tr>
        <tr>
            <td>E002</td>
            <td>นางสมหญิง รักงาน</td>
            <td>การตลาด</td>
        </tr>
    </table>
</body>
</html>
HTML;

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

Export จาก Template

use Kotchasan\Htmldoc;
use Kotchasan\Template;

// โหลด template
$template = Template::create('report', 'report_template');
$template->add([
    'TITLE' => 'รายงานยอดขาย',
    'DATE' => date('d/m/Y'),
    'CONTENT' => $tableContent
]);

// สร้าง Word document
$doc = new Htmldoc();
$doc->createDoc($template->render(), 'sales_report');

ใช้กับ Method Chaining

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

Export รายงานการเงิน

use Kotchasan\Htmldoc;
use Kotchasan\Model;

// ดึงข้อมูล
$orders = Model::createQuery()
    ->select('*')
    ->from('orders')
    ->where(['MONTH(order_date)', date('m')])
    ->execute();

// สร้าง 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>รายงานคำสั่งซื้อ</title></head>
<body>
    <h1>รายงานคำสั่งซื้อประจำเดือน</h1>
    <table border="1" cellpadding="5">
        <tr>
            <th>เลขที่</th>
            <th>ลูกค้า</th>
            <th>ยอดเงิน</th>
        </tr>
        $rows
        <tr>
            <td colspan="2"><strong>รวม</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

เอกสารที่สร้างจะมีการตั้งค่าหน้าเริ่มต้น:

  • ขนาดกระดาษ: A4 (21cm x 29.7cm)
  • ขอบกระดาษ: 1cm ทุกด้าน
  • Orientation: Portrait

ข้อจำกัด

[!NOTE]

  • รองรับเฉพาะ MS Word format (.doc)
  • ไม่รองรับ JavaScript ใน HTML
  • CSS บางส่วนอาจไม่แสดงผลเหมือนใน browser
  • รูปภาพต้องเป็น absolute URL หรือ base64

คลาสที่เกี่ยวข้อง