在TCPDF和FPDI中使用中文字体。编码问题 [英] Using Chinese fonts in TCPDF and FPDI. Encoding problems

查看:1360
本文介绍了在TCPDF和FPDI中使用中文字体。编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来生成中文字符工作表(所以学生可以生成和练习写作)

脚本从索引中的表单传递一个15个字符的字符串.PHP。
然后将字符串分解成15个元素(每个汉字)。
当我想使用Write()函数来填充这些字符的文件时,出现这个问题,我用输入来选择合适的图像没有任何问题,但现在它是字体的编码给我一个很难。



PS。我需要使用草书/手写字体作为默认的打印字体不适合手写练习。
理想情况下,我想使用

I am writing a script that generates Chinese character worksheets (so students can generate and practice writing)

The script is passed a 15 character string from a form in index.php. The string is then exploded into an array of 15 elements (each a Chinese character). The problem arises when I want to use the Write() function to populate the file with these characters, I've used the input to pick appropiate images without any problems but now it's the encoding of the fonts that gives me a hard time.

PS. I need to use a cursive/handwritten font as default 'print' fonts are not suitable for handwriting practice. Ideally I would like to use HDZB_36.TTF or Sharp Regular Script Font

See the code below as well as images of errors I get with some different fonts.

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


?>

解决方案

Just a suggestion:

这篇关于在TCPDF和FPDI中使用中文字体。编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆