簡體   English   中英

在TCPDF和FPDI中使用中文字體。 編碼問題

[英]Using Chinese fonts in TCPDF and FPDI. Encoding problems

我正在編寫一個生成漢字工作表的腳本(以便學生可以生成和練習寫作)

該腳本從index.php中的表單傳遞了15個字符串。 然后將字符串分解為15個元素的數組(每個元素為一個漢字)。 當我想使用Write()函數用這些字符填充文件時,就會出現問題,我已經使用輸入來選擇合適的圖像而沒有任何問題,但是現在正是字體的編碼使我很難受。

PS。 我需要使用草書/手寫字體,因為默認的“打印”字體不適合手寫練習。 理想情況下,我想使用HDZB_36.TTFSharp常規腳本字體

請參閱下面的代碼以及使用某些不同字體得到的錯誤圖像。

 <?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/xs.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'); ?> 

圖片1圖片2圖3

只是一個建議:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM