Kotchasan Framework Documentation
\Kotchasan\Pdf is a Class for generating PDF documents, extending FPDF to support Thai language and HTML to PDF conversion.
Basic Usage
Creating a PDF Document
use Kotchasan\Pdf;
// Create instance
$pdf = new Pdf();
// Add page
$pdf->AddPage();
// Set font (Supports Thai)
$pdf->SetFont('THSarabunNew', '', 14);
// Write text
$pdf->Cell(0, 10, 'Hello World', 0, 1);
// Output PDF
$pdf->Output();Explanation:
- Create the DB instance
- Run a simple read operation
- The comment shows the expected result
HTML to PDF Conversion
A key feature of Kotchasan\Pdf is converting HTML to PDF with CSS support.
$html = '
<style>
.header { color: red; font-size: 20px; font-weight: bold; }
.content { color: #333; }
table { width: 100%; border-collapse: collapse; }
td { border: 1px solid #000; padding: 5px; }
</style>
<div class="header">Document Header</div>
<div class="content">
<p>Document content...</p>
<table>
<tr>
<td>ID</td>
<td>Item</td>
<td>Price</td>
</tr>
<tr>
<td>1</td>
<td>Product A</td>
<td>100</td>
</tr>
</table>
</div>
';
$pdf = new Pdf();
$pdf->AddPage();
$pdf->SetFont('THSarabunNew', '', 14);
$pdf->WriteHTML($html);
$pdf->Output();CSS Support
WriteHTML supports basic CSS properties for styling:
color: Text color (#RRGGBB)font-size: Font sizefont-weight: Bold intensityfont-style: Italic styletext-align: Alignment (left, center, right, justify)border: Border (for tables)padding: Internal spacingwidth: Widthheight: Heightbackground-color: Background color
Method Reference
Constructor
__construct(string $orientation = 'P', string $unit = 'mm', string $size = 'A4', int $fontSize = 10)
Create PDF instance
$orientation: 'P' (Portrait) or 'L' (Landscape)$unit: 'pt', 'mm', 'cm', 'in'$size: 'A3', 'A4', 'A5', 'Letter', 'Legal'$fontSize: Default font size
HTML Rendering
WriteHTML(string $html, string $charset = 'cp874')
Convert HTML to PDF
- Supports basic HTML tags:
div,p,br,hr,b,i,u,strong,em,h1-h6,table,tr,td,th,img,ul,ol,li,font,center
SetCssClass(string $className, array $attributes)
Set Styles for a CSS Class
$pdf->SetCssClass('my-class', [
'color' => 'red',
'font-weight' => 'bold'
]);SetStyles(string $tag, array $attributes)
Set default styles for an HTML Tag
FPDF Extended Methods
Cell(float $w, float $h = 0, string $txt = '', mixed $border = 0, int $ln = 0, string $align = '', bool $fill = false, mixed $link = '', float $tPadding = 0, float $rPadding = 0, float $bPadding = 0, float $lPadding = 0)
Create Cell with Padding support (Extension of FPDF)
MultiCell(float $w, float $h, string $txt, mixed $border = 0, string $align = 'J', bool $fill = false, float $tPadding = 0, float $rPadding = 0, float $bPadding = 0, float $lPadding = 0)
Create MultiCell (multi-line text) with Padding support
Ln(float $h = null)
Line break
$h: Line height (uses last value if null)
Output
Output(string $dest = 'I', string $name = '', bool $isUTF8 = false)
Output PDF file (Inherited from FPDF)
$dest: 'I' (Inline), 'D' (Download), 'F' (File), 'S' (String)