99 lines
4.3 KiB
PHP
99 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* Certificate Generator Helper using TCPDF
|
|
* TPU Survey System
|
|
*/
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
|
|
/**
|
|
* Generates a PDF certificate with the recipient's name
|
|
*
|
|
* @param string $name Name to print on the certificate
|
|
* @param string $outputPath Path to save the generated PDF. If empty, returns raw PDF string.
|
|
* @param int $yPos Vertical offset (in points) where the name should be drawn
|
|
* @return string Path to the file, or raw PDF string
|
|
*/
|
|
function generateCertificate($name, $outputPath = '', $yPos = 82.5, $certCode = '', $bgImage = '', $codeY = 190.5) {
|
|
// สร้างเอกสาร PDF ขนาดมาตรฐาน A4 แนวนอน (297 x 210 มิลลิเมตร)
|
|
$pdf = new TCPDF('L', 'mm', 'A4', true, 'UTF-8', false);
|
|
|
|
// Set document information
|
|
$pdf->SetCreator(PDF_CREATOR);
|
|
$pdf->SetAuthor('TPU Survey System');
|
|
$pdf->SetTitle('เกียรติบัตร - ' . $name);
|
|
$pdf->SetSubject('เกียรติบัตรผู้ผ่านการอบรม');
|
|
|
|
// Remove default header/footer
|
|
$pdf->setPrintHeader(false);
|
|
$pdf->setPrintFooter(false);
|
|
|
|
// Set margins to 0
|
|
$pdf->SetMargins(0, 0, 0);
|
|
$pdf->SetAutoPageBreak(false, 0);
|
|
|
|
// Add page
|
|
$pdf->AddPage();
|
|
|
|
// Background image path
|
|
$imgFile = __DIR__ . '/../pic/AI Antigravity.png';
|
|
if (!empty($bgImage)) {
|
|
$customImgFile = __DIR__ . '/../' . $bgImage;
|
|
if (file_exists($customImgFile)) {
|
|
$imgFile = $customImgFile;
|
|
}
|
|
}
|
|
|
|
if (!file_exists($imgFile)) {
|
|
throw new Exception("ไม่พบไฟล์เทมเพลตเกียรติบัตรที่: " . $imgFile);
|
|
}
|
|
|
|
// วาดภาพพื้นหลังให้พอดีกับกระดาษ A4 แนวนอน (297 x 210 มิลลิเมตร)
|
|
$pdf->Image($imgFile, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);
|
|
|
|
// เรียกใช้ฟอนต์ภาษาไทย Sarabun
|
|
$fontRegular = 'sarabun';
|
|
$fontBold = 'sarabunb';
|
|
|
|
// กำหนดฟอนต์ชื่อ: หนา (Bold) ขนาด 26pt (สเกลย่อส่วนสำหรับ A4)
|
|
$pdf->SetFont($fontBold, '', 26);
|
|
$pdf->SetTextColor(30, 41, 59);
|
|
|
|
// จัดวางชื่อกึ่งกลางหน้ากระดาษ (ความกว้าง A4 = 297 มม.) (และแก้ปัญหาสระลอย)
|
|
$pdf->SetXY(0, $yPos);
|
|
$pdf->Cell(297, 15, fixThaiVowels($name), 0, false, 'C', 0, '', 0, false, 'T', 'M');
|
|
|
|
// วาดรหัสตรวจสอบเกียรติบัตรที่กึ่งกลางล่างสุดของกระดาษ (ขนาด 8pt พิกัด Y = $codeY มม.)
|
|
if (!empty($certCode)) {
|
|
$pdf->SetFont($fontRegular, '', 8);
|
|
$pdf->SetTextColor(100, 116, 139); // Muted Slate color
|
|
$pdf->SetXY(0, $codeY);
|
|
$pdf->Cell(297, 10, fixThaiVowels('รหัสตรวจสอบเกียรติบัตร: ' . $certCode), 0, false, 'C', 0, '', 0, false, 'T', 'M');
|
|
}
|
|
|
|
// Output
|
|
if (!empty($outputPath)) {
|
|
$dir = dirname($outputPath);
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0777, true);
|
|
}
|
|
$pdf->Output($outputPath, 'F');
|
|
return $outputPath;
|
|
} else {
|
|
return $pdf->Output('', 'S');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* แก้ไขปัญหาสระลอยและวรรณยุกต์ซ้อนทับกันในไฟล์ PDF ภาษาไทย (สำหรับ TCPDF/FPDF)
|
|
* โดยการใช้รหัส Unicode ในส่วน Private Use Area (PUA) ของฟอนต์ไทยมาตรฐาน
|
|
*
|
|
* @param string $text ข้อความภาษาไทยที่ต้องการจัดรูปแบบสระ/วรรณยุกต์
|
|
* @return string ข้อความที่จัดตำแหน่งสระ/วรรณยุกต์แล้ว
|
|
*/
|
|
function fixThaiVowels($text) {
|
|
// TCPDF with the Sarabun font natively handles Thai vowel and tone mark positioning correctly.
|
|
// The previous PUA mapping caused square box rendering issues.
|
|
return $text;
|
|
}
|