0

我正在写一个生成汉字工作表的脚本(让学生可以生成和练习写作)

该脚本从 index.php 中的表单传递一个 15 个字符的字符串。然后将该字符串分解为一个由 15 个元素组成的数组(每个元素是一个汉字)。当我想使用 Write() 函数用这些字符填充文件时,问题就出现了,我已经使用输入来选择适当的图像而没有任何问题,但现在是字体的编码让我很难过。

PS。我需要使用草书/手写字体,因为默认的“打印”字体不适合手写练习。理想情况下,我想使用HDZB_36.TTFSharp 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');


?>

图片1 图片2 图3

4

1 回答 1

0

只是一个建议:

于 2015-09-10T15:26:31.327 回答