duanji2014
2017-08-23 12:47
浏览 103
已采纳

PHP,Fpdf解析错误:使用AddFont()时

I have an issue with displaying what ever on a PDF file when I change the font.

<?php
session_start();
include $_SERVER['DOCUMENT_ROOT'] . "/library/fpdf.php";
$pdf = new FPDF('P',"mm",'A4');
$pdf->AddPage();
$pdf->AddFont('DejaVu','','DejaVuSans-Bold.ttf',true);
$pdf->SetFont('DejaVu','',14);
foreach ($_SESSION['settings'] as $settings) {
    $pdf->Cell(130,5,$settings,1,1);
}

$pdf->Output();
?>

I tried changing the font with other files and I just keep getting random errors like:

Warning: Unexpected character in input:

or

Parse error [my path to the ttf file]

Even if I don't print anything in the PDF just leave it blank , I still get an error due to the AddFont,

If I remove

$pdf->AddFont('DejaVu','','DejaVuSans-Bold.ttf',true);
$pdf->SetFont('DejaVu','',14);

and replace it with

$pdf->SetFont('Arial','',14);

it works fine.

图片转代码服务由CSDN问答提供 功能建议

我在更改字体时显示PDF文件的内容时遇到问题。 &lt;?php session_start(); include $ _SERVER ['DOCUMENT_ROOT']。 “/library/fpdf.php"; pdpdf = new FPDF('P',”mm“,'A4'); $ pdf-&gt; AddPage(); $ pdf-&gt; AddFont( 'DejaVu','','DejaVuSans-Bold.ttf',true); $ pdf-&gt; SetFont('DejaVu','',14); foreach($ _SESSION ['settings'] as $ settings ){ $ pdf-&gt; Cell(130,5,$ settings,1,1); } $ pdf-&gt; Output(); ?&gt;

我尝试用其他文件更改字体,我只是不断收到如下错误:

警告:输入中出现意外字符 :

解析错误[我的ttf文件路径] \ n

即使我没有在PDF中打印任何内容,只需将其留空,由于AddFont,我仍然会收到错误,

如果 我删除了

  $ pdf-&gt; AddFont('DejaVu','','DejaVuSans-Bold.ttf',true); 
 $ pdf-&gt; SetFont(  'DejaVu','',14); 
   
 
 

并将其替换为

  $ pdf-&gt;  SetFont('Arial','',14); 
   
 
 

它工作正常。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douang1243 2017-08-23 13:22
    已采纳

    The AddFont function requires a particular type of file to be able to use the font, not just the font file itself. If you take the DejaVuSans-Bold.ttf file and upload it here, it will convert it into the required format for you.

    There will be 2 files generated, DejaVuSans-Bold.php and DejaVuSans-Bold.z. Copy both of these into your project, and then change the line of code to the following:

    $pdf->AddFont('DejaVu','','DejaVuSans-Bold.php');
    

    There's more information on how to add new fonts on the following tutorial. It has details on how to generate the font files manually on your own PC, however there is an online tool (linked above) that will do it for you:

    Finally, for TrueType and OpenType fonts, you can also generate the files online instead of doing it manually.

    Note: you may get some warnings when you convert online, saying certain characters are missing. If you scroll to the bottom, the converted files will still be available to download, you just won't be able to use any of the listed missing characters in your PDF.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题