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

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.

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题