-->

DOMPDF - Class 'Font' not found

2019-06-16 13:05发布

问题:

I'm trying to add a font via Command line.

Every time I try i get the following error.

Fatal error: Class 'Font' not found in 
/home/scripts/public_html/MarketingPalv2/load_font.php on line 139

I also get a similar error when I try to use @font-face

Fatal error: Class 'Font' not found in 
/home/scripts/public_html/MarketingPalv2/include/font_metrics.cls.php on line 346

Hope you guys can help.

回答1:

I figured out what it was.

I downloaded the newest version of php-font-lib but i needed a older version.



回答2:

I got this working for version 0.3.1 / 0.4 of pdf-font-lib (commit: b8af0ca) and DomPDF v6.1.0 (commit: c3527d9) by making a Font class that extends FontLib\Font;

<?php
class Font extends FontLib\Font {
    //this is a namespace fix:
}

And in the class where I use DomPDF:

require_once('lib/dompdf/include/autoload.inc.php');
require_once('lib/dompdf/lib/php-font-lib/src/FontLib/Autoloader.php');
require_once('dir/where/you/placed/the/file/Font.php');

Now the class Font is available in the global namespace.

I choose this approach because I'm in a bit more dynamic situation and didn't want to change/edit the original library since it is from the master branch and it is LGPL licensed.

However, note that DomPDF 0.7.0 beta, that is released on 1 May, is out of the box compatible with the latest pdf-font-lib. Check it out: https://github.com/dompdf/dompdf/releases/tag/v0.7.0-beta



回答3:

Solved this for dompdf 0.6.1 using latest pdf-font-lib by editing load_font.php and making the header look like:

require_once "dompdf_config.inc.php";

require_once "lib/php-font-lib/classes/Autoloader.php"; use FontLib\Font;



回答4:

I used dompdf-master V.0.6.1, I solved it by

require_once "../lib/php-font-lib/classes/Autoloader.php"; use FontLib\Font; to font_metrics.cls.php when I install new font



回答5:

I got this working for version DomPDF v6.1 by adding

use FontLib\Font;

to the file font_metrics.cls.php above

class Font_Metrics {


回答6:

I hope this will help someone.

In case you get this error,

Fatal error: Class 'Font' not found in dompdf/include/font_metrics.cls.php on line xxx

You have to change dompdf/include/font_metrics.cls.php file as below. So it will look like;

require_once DOMPDF_LIB_DIR . "/class.pdf.php";
require_once DOMPDF_LIB_DIR."/php-font-lib/classes/Autoloader.php"; 
use FontLib\Font;

You need to add the second & third lines only. First one will be already there.



回答7:

I fixed the issue by change dompdf/include/font_metrics.cls.php file as bellow

require_once DOMPDF_LIB_DIR . "/class.pdf.php";
require_once DOMPDF_LIB_DIR."/php-font-lib/classes/Autoloader.php"; 
use FontLib\Font;

copy past this code on top



回答8:

I'm late to the party here, but working with dompdf in 2018 still seems to experience this issue when loading Google Fonts with CSS. The specific error I see is:

"Uncaught Error: Class 'FontLib\Font' not found"

With version 0.8.2 (July 2018) I solved by loading the php-font-lib library before loading dompdf using the following:

// Load DOMPDF library
require_once( 'composer/phenx/php-font-lib/src/FontLib/Autoloader.php' );
require_once( 'composer/dompdf/dompdf/src/Autoloader.php' );
Dompdf\Autoloader::register();

You'll need to make sure your composer path is correct here, I've removed a variable from the require_once() calls.



标签: php dompdf