Content is user-generated and unverified.
<?php require_once __DIR__ . '/vendor/autoload.php'; /** * คลาสสำหรับแปลงตัวเลขเป็นตัวอักษรไทย */ class ThaiNumberConverter { private static $ones = ['', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า']; private static $places = ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน']; /** * แปลงตัวเลขเป็นตัวอักษรไทย */ public static function numberToThaiText($number) { if ($number == 0) return 'ศูนย์'; $result = ''; $digits = array_reverse(str_split((string)$number)); for ($i = 0; $i < count($digits); $i++) { $digit = (int)$digits[$i]; if ($digit != 0) { if ($i == 1 && $digit == 2) { $result = 'ยี่สิบ' . $result; } elseif ($i == 1 && $digit == 1) { $result = 'สิบ' . $result; } elseif ($i == 1) { $result = self::$ones[$digit] . 'สิบ' . $result; } elseif ($i == 0 && $digit == 1 && isset($digits[1]) && (int)$digits[1] > 0) { $result = 'เอ็ด' . $result; } else { $result = self::$ones[$digit] . self::$places[$i] . $result; } } } return $result; } /** * แปลงจำนวนเงินเป็นตัวอักษรไทย */ public static function convertToThaiCurrency($amount) { if (!is_numeric($amount) || $amount < 0) { return 'จำนวนเงินไม่ถูกต้อง'; } $baht = floor($amount); $satang = round(($amount - $baht) * 100); $result = ''; if ($baht > 0) { $result .= self::numberToThaiText($baht) . 'บาท'; } if ($satang > 0) { if ($baht > 0) { $result .= self::numberToThaiText($satang) . 'สตางค์'; } else { $result = self::numberToThaiText($satang) . 'สตางค์'; } } elseif ($baht > 0) { $result .= 'ถ้วน'; } return $result ?: 'ศูนย์บาทถ้วน'; } } /** * ตัวอย่างการใช้งานกับ mPDF */ try { // สร้าง mPDF instance $mpdf = new \Mpdf\Mpdf([ 'mode' => 'utf-8', 'format' => 'A4', 'margin_left' => 20, 'margin_right' => 20, 'margin_top' => 20, 'margin_bottom' => 20, 'default_font' => 'sarabun' // ใช้ฟอนต์ไทย ]); // ข้อมูลตัวอย่างสำหรับใบเบิกจ่าย $expenseData = [ ['รายการ' => 'ค่าเดินทาง กรุงเทพฯ-เชียงใหม่', 'จำนวน' => 2500.50], ['รายการ' => 'ค่าที่พัก (1 คืน)', 'จำนวน' => 1200.00], ['รายการ' => 'ค่าอาหาร', 'จำนวน' => 450.75], ['รายการ' => 'ค่าน้ำมันรถ', 'จำนวน' => 800.00], ]; $totalAmount = array_sum(array_column($expenseData, 'จำนวน')); // สร้าง HTML สำหรับ PDF $html = ' <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> body { font-family: "sarabun", sans-serif; font-size: 14px; line-height: 1.6; } .header { text-align: center; font-size: 18px; font-weight: bold; margin-bottom: 30px; border-bottom: 2px solid #333; padding-bottom: 10px; } .info-section { margin-bottom: 20px; } .info-row { margin-bottom: 8px; } table { width: 100%; border-collapse: collapse; margin: 20px 0; } th, td { border: 1px solid #333; padding: 8px; text-align: left; } th { background-color: #f0f0f0; font-weight: bold; text-align: center; } .number { text-align: right; } .total-row { font-weight: bold; background-color: #f9f9f9; } .amount-text { margin-top: 20px; padding: 15px; border: 2px solid #333; background-color: #f9f9f9; } .signature-section { margin-top: 40px; display: flex; justify-content: space-between; } .signature-box { width: 200px; text-align: center; border-top: 1px solid #333; padding-top: 10px; margin-top: 60px; } </style> </head> <body> <div class="header"> ใบเบิกจ่ายค่าเดินทางไปราชการ </div> <div class="info-section"> <div class="info-row"><strong>ชื่อผู้เบิก:</strong> นายสมชาย ใจดี</div> <div class="info-row"><strong>ตำแหน่ง:</strong> นักวิชาการคอมพิวเตอร์</div> <div class="info-row"><strong>หน่วยงาน:</strong> กรมพัฒนาดิจิทัล</div> <div class="info-row"><strong>วันที่:</strong> ' . date('d/m/Y') . '</div> </div> <table> <thead> <tr> <th width="10%">ลำดับ</th> <th width="60%">รายการค่าใช้จ่าย</th> <th width="30%">จำนวนเงิน (บาท)</th> </tr> </thead> <tbody>'; // เพิ่มรายการค่าใช้จ่าย foreach ($expenseData as $index => $item) { $html .= ' <tr> <td style="text-align: center;">' . ($index + 1) . '</td> <td>' . $item['รายการ'] . '</td> <td class="number">' . number_format($item['จำนวน'], 2) . '</td> </tr>'; } $html .= ' <tr class="total-row"> <td colspan="2" style="text-align: center;">รวมทั้งสิ้น</td> <td class="number">' . number_format($totalAmount, 2) . '</td> </tr> </tbody> </table> <div class="amount-text"> <strong>จำนวนเงิน (ตัวอักษร):</strong> ' . ThaiNumberConverter::convertToThaiCurrency($totalAmount) . ' </div> <div style="margin-top: 30px;"> <p><strong>หมายเหตุ:</strong> การเดินทางไปราชการ ณ จังหวัดเชียงใหม่ เพื่อเข้าร่วมการประชุมสัมมนา</p> </div> <table style="border: none; margin-top: 40px;"> <tr style="border: none;"> <td style="border: none; text-align: center; width: 50%;"> <div style="margin-top: 60px; border-top: 1px solid #333; padding-top: 5px;"> ลายมือชื่อผู้เบิก<br> ( นายสมชาย ใจดี ) </div> </td> <td style="border: none; text-align: center; width: 50%;"> <div style="margin-top: 60px; border-top: 1px solid #333; padding-top: 5px;"> ลายมือชื่อผู้อนุมัติ<br> ( ..................................... ) </div> </td> </tr> </table> </body> </html>'; // เขียน HTML ลง PDF $mpdf->WriteHTML($html); // แสดงผลหรือบันทึกไฟล์ $mpdf->Output('expense_report_' . date('Ymd') . '.pdf', 'I'); // 'I' = แสดงในเบราว์เซอร์, 'D' = ดาวน์โหลด } catch (\Mpdf\MpdfException $e) { echo 'เกิดข้อผิดพลาด: ' . $e->getMessage(); } // ตัวอย่างการใช้งานแบบง่าย ๆ echo "<h3>ตัวอย่างการแปลงตัวเลข:</h3>"; $testAmounts = [5.00, 5.30, 5.05, 1234.56, 1000, 0.75]; foreach ($testAmounts as $amount) { echo $amount . " → " . ThaiNumberConverter::convertToThaiCurrency($amount) . "<br>"; } ?>
Content is user-generated and unverified.
    ใช้งานตัวแปลงตัวเลขไทยกับ mPDF | Claude