无法将大多数TTF文件导入TCPDF

时间:2019-08-13 14:04:23

标签: php pdf tcpdf

我已经阅读了有关该主题的所有教程,我简直无法使它正常工作,示例代码和在线工具也是如此……

当我尝试将大多数.ttf文件导入TCPDF字体时,生成的php文件如下所示:

<?php
$type='TrueType';
$name='FontAwesome5Free-Solid';
$desc=array('Ascent'=>875,'Descent'=>-125,'CapHeight'=>875,'Flags'=>96,'FontBBox'=>'[5000 5000 -5000 -5000]','ItalicAngle'=>-50,'StemV'=>70,'MissingWidth'=>1000);
$up=-123;
$ut=49;
$dw=1000;
$cw=array(
0=>1000,1=>1000,2=>1000,3=>1000,4=>1000,5=>1000,6=>1000,7=>1000,8=>1000,9=>1000,
10=>1000,11=>1000,12=>1000,13=>1000,14=>1000,15=>1000,16=>1000,17=>1000,18=>1000,19=>1000,
20=>1000,21=>1000,22=>1000,23=>1000,24=>1000,25=>1000,26=>1000,27=>1000,28=>1000,29=>1000,
30=>1000,31=>1000,32=>1000,33=>1000,34=>1000,35=>1000,36=>1000,37=>1000,38=>1000,39=>1000,
40=>1000,41=>1000,42=>1000,43=>1000,44=>1000,45=>1000,46=>1000,47=>1000,48=>1000,49=>1000,
50=>1000,51=>1000,52=>1000,53=>1000,54=>1000,55=>1000,56=>1000,57=>1000,58=>1000,59=>1000,
60=>1000,61=>1000,62=>1000,63=>1000,64=>1000,65=>1000,66=>1000,67=>1000,68=>1000,69=>1000,
70=>1000,71=>1000,72=>1000,73=>1000,74=>1000,75=>1000,76=>1000,77=>1000,78=>1000,79=>1000,
80=>1000,81=>1000,82=>1000,83=>1000,84=>1000,85=>1000,86=>1000,87=>1000,88=>1000,89=>1000,
90=>1000,91=>1000,92=>1000,93=>1000,94=>1000,95=>1000,96=>1000,97=>1000,98=>1000,99=>1000,
100=>1000,101=>1000,102=>1000,103=>1000,104=>1000,105=>1000,106=>1000,107=>1000,108=>1000,109=>1000,
110=>1000,111=>1000,112=>1000,113=>1000,114=>1000,115=>1000,116=>1000,117=>1000,118=>1000,119=>1000,
120=>1000,121=>1000,122=>1000,123=>1000,124=>1000,125=>1000,126=>1000,127=>1000,128=>1000,129=>1000,
130=>1000,131=>1000,132=>1000,133=>1000,134=>1000,135=>1000,136=>1000,137=>1000,138=>1000,139=>1000,
140=>1000,141=>1000,142=>1000,143=>1000,144=>1000,145=>1000,146=>1000,147=>1000,148=>1000,149=>1000,
150=>1000,151=>1000,152=>1000,153=>1000,154=>1000,155=>1000,156=>1000,157=>1000,158=>1000,159=>1000,
160=>1000,161=>1000,162=>1000,163=>1000,164=>1000,165=>1000,166=>1000,167=>1000,168=>1000,169=>1000,
170=>1000,171=>1000,172=>1000,173=>1000,174=>1000,175=>1000,176=>1000,177=>1000,178=>1000,179=>1000,
180=>1000,181=>1000,182=>1000,183=>1000,184=>1000,185=>1000,186=>1000,187=>1000,188=>1000,189=>1000,
190=>1000,191=>1000,192=>1000,193=>1000,194=>1000,195=>1000,196=>1000,197=>1000,198=>1000,199=>1000,
200=>1000,201=>1000,202=>1000,203=>1000,204=>1000,205=>1000,206=>1000,207=>1000,208=>1000,209=>1000,
210=>1000,211=>1000,212=>1000,213=>1000,214=>1000,215=>1000,216=>1000,217=>1000,218=>1000,219=>1000,
220=>1000,221=>1000,222=>1000,223=>1000,224=>1000,225=>1000,226=>1000,227=>1000,228=>1000,229=>1000,
230=>1000,231=>1000,232=>1000,233=>1000,234=>1000,235=>1000,236=>1000,237=>1000,238=>1000,239=>1000,
240=>1000,241=>1000,242=>1000,243=>1000,244=>1000,245=>1000,246=>1000,247=>1000,248=>1000,249=>1000,
250=>1000,251=>1000,252=>1000,253=>1000,254=>1000,255=>1000);
$enc='cp1252';
$diff='';
$file='lol.z';
$originalsize=191836;
// --- EOF ---

基本上所有字符都无效。

这是我使用PHP源或使用为TCPDF制作的在线转换器(例如:http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdfhttp://fonts.snm-portal.com/)得到的结果。

我正在尝试将字体真棒的TTF文件(例如fa-solid-900.ttf)添加到我的TCPDF中,但是出现此错误。

我不知道现在该怎么办。

1 个答案:

答案 0 :(得分:1)

您需要将FontAwesome导入为Unicode字体。 [也要注意:在此示例中,字符0-255的字符宽度为1000,因为该字体中不存在这些字符,而1000是为丢失的字形宽度定义的宽度。]对于其他出现故障的字体,请尝试导入它们也可以使用Unicode字体或使用不同的编码表。

//Ignore the warnings about undefined indexes on first import. 
//There's no H or x glyphs in the font, so the warning is expected.
$fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf',  'TrueTypeUnicode', '', 4);
//Note: using empty string for $enc since this is a Unicode font
//Note: using 4 for $flags to signify a symbol font. 32 for text fonts.
$pdf->setFont($fa);
$pdf->writeHTMLCell(100,120,40,10,"&#xf11b;");

//An example inlined with normal text:
$pdf->setFont('helvetica');
$pdf->writeHTMLCell(100,120,40,20,"Should be a gamepad: <span style=\"font-family:$fa;\">&#xf11b;</span>");

测试输出结果的附加屏幕截图:

Example with gamepad icon.

测试脚本:

<?php
//Update to your TCPDF path
include 'TCPDF/tcpdf.php';

$pdf = new TCPDF('P', 'mm','Letter', true, 'UTF-8', false, true);
$pdf->addPage();
$pdf->setFontSubsetting(true);
$fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf',  'TrueTypeUnicode', '', 4);

$testtext = "Font name: {$fa}<br>".
  "Should be a gamepad: <span style=\"font-family:$fa;\">&#xf11b;</span>";
$pdf->writeHTMLCell(100,10,10,10,$testtext);
$pdf->Output(dirname(__FILE__).'/fasolid-test.pdf', 'F');