php FPDF 错误:无法包含字体度量文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6521000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:37:58  来源:igfitidea点击:

FPDF error: Could not include font metric file

phppdf-generationfpdf

提问by Prady

I have a app which was done by someone else and now i am asked to look into one issue.

我有一个由其他人完成的应用程序,现在我被要求调查一个问题。

When a pdf report is generated it throws an error. This app uses FPDF to generate the PDF

当生成 pdf 报告时,它会引发错误。此应用程序使用 FPDF 生成 PDF

FPDF error: Could not include font metric file

Earlier it was throwing the following error

早些时候它抛出以下错误

Warning: FPDF::include(helveticab.php) [function.FPDF-include]: failed to open stream: No such file or directory 
Warning: FPDF::include() [function.include]: Failed opening 'helveticab.php' for inclusion 
FPDF error: Could not include font metric file

This was resolved by including a font folder with helveticab.php and other php files related to other fonts

这是通过在字体文件夹中包含 helveticab.php 和与其他字体相关的其他 php 文件来解决的

But the Error FPDF error: Could not include font metric file is still there. On searching the net the possible reasons were

但是错误 FPDF 错误:无法包含字体度量文件仍然存在。在网上搜索可能的原因是

  1. font directory missing

  2. Doesnt have access permissions for the font files.

  1. 字体目录丢失

  2. 没有字体文件的访问权限。

I am not sure what permission need to given to the font folder or files in the folder. Any help in this regard would be of great help.

我不确定需要授予字体文件夹或文件夹中的文件什么权限。在这方面的任何帮助都会有很大帮助。

回答by Rasta

I had the same issue. The issue was the path was incorrect to the folder with all of the fonts. So, I added updated the following line in the PHP file to reflect the correct path to the folder with all of the fonts.

我遇到过同样的问题。问题是包含所有字体的文件夹的路径不正确。因此,我在 PHP 文件中添加了更新的以下行,以反映包含所有字体的文件夹的正确路径。

define('FPDF_FONTPATH','class/fpdf_font/');

定义('FPDF_FONTPATH','class/fpdf_font/');

So, double check the path that this line defines, and it should work fine.

因此,仔细检查此行定义的路径,它应该可以正常工作。

回答by Rafee

I belive you have already extracted fpdf zip file onto your localhost or system

我相信您已经将 fpdf zip 文件解压缩到您的本地主机或系统上

Once, zip file is extracted you see directory structure as in below image

一次,解压缩 zip 文件,您会看到如下图所示的目录结构

enter image description here

在此处输入图片说明

and insert the below code in test.php

并在 test.php 中插入以下代码

<?php
define('FPDF_FONTPATH','font/');
//above line is import to define, otherwise it gives an error : Could not include font metric file
require('fpdf.php');



$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

Now enjoy

现在享受

回答by Edgar Sampere

My problem was, because of downloading the fpdf library from the page, some of the scripts they have there uses Arial font but, that font specially, was not included in the fonts directory. I just added define('FPDF_FONTPATH','fpdf/font/');with relative path to the fpdf dir and changed font to Courier and ready!

我的问题是,由于从页面下载了 fpdf 库,他们那里的一些脚本使用了 Arial 字体,但特别是该字体未包含在字体目录中。我刚刚添加define('FPDF_FONTPATH','fpdf/font/');了 fpdf 目录的相对路径并将字体更改为 Courier 并准备就绪!

回答by devpjeeva

if you are using a external class that extends FPDF

如果您使用的是扩展 FPDF 的外部类

like file name Custom_pdf.php

像文件名Custom_pdf.php

in that file you wrote your cutom codes

在该文件中,您编写了自定义代码

require_once('fpdf.php')


class Custom_PDF extends FPDF{
   ...
}

then you included in to your coding like

然后你包含在你的编码中

require_once('custom_pdf.php');
$pdf = new Custom_pdf();
$pdf->Write();
....
$pdf->output();

this is problem...

这是问题...

So you directly place the code in the file you need

所以你直接把代码放在你需要的文件里

require_once('fpdf.php')
class Custom_PDF extends FPDF{
....
}
$pdf = new Custom_pdf();
$pdf->Write();
...
$pdf->output();

it will works fine...

它会正常工作...

Thanks

谢谢

sorry for the english mistakes...

抱歉英语错误...

回答by Raúl Castro

In my case I use Linux (Debian), I had the same issue and the directories was right. I solved adding 777 permissions to the /font directory. And now it works like charm =)))

在我的情况下,我使用 Linux (Debian),我遇到了同样的问题并且目录是正确的。我解决了向 /font 目录添加 777 权限的问题。现在它的作用就像魅力 =)))