2015-05-08 3 views
0

Я пишу скрипт, который генерирует китайские персональные листы (так что ученики могут создавать и практиковать запись)Использование китайских шрифтов в TCPDF и FPDI. Проблемы с кодированием

Сценарий передается из 15 символов из формы в index.php. Затем строка взорвалась в массив из 15 элементов (каждый из которых - китайский символ). Проблема возникает, когда я хочу использовать функцию Write(), чтобы заполнить файл этими символами, я использовал вход для выбора подходящих изображений без каких-либо проблем, но теперь именно кодировка шрифтов затрудняет мне время.

PS. Мне нужно использовать шрифт cursive/handwritten, поскольку шрифты «print» по умолчанию не подходят для практики почерка. В идеале я хотел бы использовать HDZB_36.TTF или Sharp Regular Script Font

См. Приведенный ниже код, а также изображения ошибок, которые я получаю с некоторыми разными шрифтами.

<?php 
 

 
    header('Content-Type: text/html; charset=utf-8'); 
 

 
// linking TCPDF and FPDI libraries 
 

 
    require_once('tcpdf/tcpdf.php'); 
 
    require_once('fpdi/fpdi.php'); 
 

 
// First retrieve a 15 chinese charcters long string from POST form in index.php 
 

 
    $hanzi = $_POST["hanzi"]; 
 

 
// Explode the hanzi into a 15 items array 
 

 
    function mb_str_split($hanzi){ 
 
    return preg_split('/(?<!^)(?!$)/u', $hanzi); 
 
    } 
 

 
    $charlist = mb_str_split($hanzi); 
 

 
// Define starting y positions of each line of the grid 
 

 
    $yPos1 = 10.71; 
 
    $yPos2 = 17.94; 
 

 
// Creating new page with PDF as a background 
 

 
    $pdf = new FPDI(); 
 
    $background = $pdf->setSourceFile('images/worksheet_template1.pdf'); 
 

 
    $tplIdx = $pdf->importPage(1); 
 
    $pdf->AddPage(); 
 
    $pdf->useTemplate($tplIdx, 0, 0, 210, 285, false); 
 

 
/* 
 

 
This is where the problem starts, I can manage to display latin characters using helvetica 
 
but when I use any of the chinese fonts (usually encoded as GB2312 or BIG5) it fails. 
 

 
With some larger (ex. stsong) fonts I get a browser error saying: No data received ERR_EMPTY_RESPONSE (Image 1) 
 
With font 'htst3' the characters appeared upside down and were full of artifacts (Image 2). 
 
With font HDZB_36 the characters were not rendered at all. 
 

 
Other fonts will result in all of the chars displayed as '?' (Image 3) 
 

 
*/ 
 

 
$fontname = TCPDF_FONTS::addTTFfont('ukai.ttf', 'TrueTypeUnicode', '', 64); 
 
$pdf->SetFont('ukai','', 20); 
 

 

 
for ($i = 0; $i <= 14; $i++){ 
 

 
// Generating path of the stroke order image (that works fine) 
 

 
$sImgPath = "images/x-s.png"; 
 
$sImgPath = str_ireplace('x', $charlist[$i], $sImgPath); 
 

 
// Stroke order image 
 

 
$pdf->Image($sImgPath, '14', $yPos1, '','5'); 
 

 
// Here we will populate grid of the worksheet with chinese characters as TEXT 
 

 
$pdf->SetXY(12.4,$yPos2); 
 
$pdf->SetTextColor(0, 0, 0); 
 
$pdf->Write(0, $charlist[$i], '', false); 
 

 
$pdf->SetXY(24.2,$yPos2); 
 
$pdf->SetTextColor(192,192,192); 
 
$pdf->Write(0, $charlist[$i], '', false); 
 

 
// Increase the y pos values so the next run of for() will draw in another line 
 

 
$yPos1 = $yPos1+17.83; 
 
$yPos2 = $yPos2+17.78; 
 

 
} 
 

 
ob_clean(); 
 
$pdf->Output('worksheet.pdf', 'I'); 
 

 

 
?>

Image1 Image2 Image3

ответ

0

Только предложение:

  • Файл можно создавать worksheet.pdf, возможно, следует иметь ту же кодировку, как ваши письма.
  • PDF должен иметь соответствующую кодировку, см.: https://stackoverflow.com/a/10656899/1933185