FPDI (Free PDF Document Importer) library में character encoding सेट करने के लिए fonts और encoding का सही संयोजन उपयोग करें।
समस्या
FPDI/TCPDF डिफ़ॉल्ट रूप से core fonts (Helvetica, Times, Courier) उपयोग करते हैं जो केवल Latin-1 (ISO-8859-1) support करते हैं। Unicode (हिंदी, चीनी, अरबी, आदि) के लिए TrueType fonts (TTF) आवश्यक हैं।
समाधान: Unicode Font Add करें
<?php
require_once('fpdi/autoload.php');
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
// DejaVu Sans font add करें (Unicode support के लिए)
// Font files: DejaVuSans.ttf, DejaVuSans-Bold.ttf, आदि
$pdf->addTTFfont(
__DIR__ . '/fonts/DejaVuSans.ttf', // Regular font file
'DejaVuSans', // Font family name
'', // Style (empty = regular)
32 // Font type: 32 = Unicode TrueType
);
$pdf->addTTFfont(
__DIR__ . '/fonts/DejaVuSans-Bold.ttf',
'DejaVuSans',
'B', // Bold style
32
);
// Font set करें
$pdf->SetFont('DejaVuSans', '', 10);
$pdf->AddPage();
// Unicode text लिखें
$pdf->Write(5, 'नमस्ते दुनिया! Hello World! 你好!');
$pdf->Output('unicode_test.pdf', 'D');
Font Files प्राप्त करना
# DejaVu fonts download करें (open source, complete Unicode) wget https://github.com/dejavu-fonts/dejavu-fonts/releases/download/version_2_37/dejavu-fonts-ttf-2.37.zip unzip dejavu-fonts-ttf-2.37.zip cp ttf/DejaVuSans*.ttf /path/to/your/project/fonts/ # या Google Fonts से Noto Sans (सभी भाषाओं के लिए) # https://fonts.google.com/noto/specimen/Noto+Sans
Encoding Constants (FPDI/TCPDF में परिभाषित)
| Encoding Constant | Coverage |
|---|---|
cp1250 | Central Europe |
cp1251 | Cyrillic |
cp1252 | Western Europe (default) |
cp1253 | Greek |
cp1254 | Turkish |
cp1255 | Hebrew |
cp1257 | Baltic |
cp1258 | Vietnamese |
cp874 | Thai |
ISO-8859-1 | Western Europe |
ISO-8859-2 | Central Europe |
ISO-8859-5 | Cyrillic |
ISO-8859-7 | Greek |
ISO-8859-9 | Turkish |
KOI8-R | Russian |
KOI8-U | Ukrainian |
Legacy Encoding Conversion (यदि TTF fonts नहीं उपयोग कर सकते)
<?php
// UTF-8 से cp1250 (Central Europe) में convert
$utf8String = 'zazółć gęślą jaźń';
$encoded = iconv('UTF-8', 'cp1250//TRANSLIT', $utf8String);
// Core font के साथ उपयोग (limited characters)
$pdf->SetFont('Helvetica', '', 10);
$pdf->Write(5, $encoded); // Only works for Latin-2 characters
Complete Working Example
<?php
require_once 'vendor/autoload.php';
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
// 1. Source PDF import करें
$pageCount = $pdf->setSourceFile('template.pdf');
// 2. Unicode font add करें
$fontDir = __DIR__ . '/fonts/';
$pdf->addTTFfont($fontDir . 'DejaVuSans.ttf', 'DejaVu', '', 32);
$pdf->addTTFfont($fontDir . 'DejaVuSans-Bold.ttf', 'DejaVu', 'B', 32);
// 3. Template pages copy करें
for ($pageNum = 1; $pageNum <= $pageCount; $pageNum++) {
$tplId = $pdf->importPage($pageNum);
$pdf->AddPage();
$pdf->useTemplate($tplId);
// 4. Unicode text overlay करें
$pdf->SetFont('DejaVu', '', 12);
$pdf->SetXY(50, 50);
$pdf->Write(0, 'हिंदी टेक्स्ट: नमस्ते!');
$pdf->SetFont('DejaVu', 'B', 10);
$pdf->SetXY(50, 70);
$pdf->Write(0, 'English: Hello World!');
}
// 5. Output
$pdf->Output('output.pdf', 'D');
Troubleshooting
- Font not found: Font file path verify करें,
addTTFfont()से पहले file exists check करें - Characters missing: Font में required glyphs नहीं हैं - Noto fonts उपयोग करें
- Performance: Large TTF fonts slow कर सकते हैं - subset fonts बनाएं
- Memory:
ini_set('memory_limit', '256M')बढ़ाएं