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

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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题