dompdf fails to load

2019-02-02 21:23发布

I am trying to get dompdf running on an in-house server. With the default config.inc.php settings, I get the following when running the equivalent of the demo 'Hello Wolrd' script:

Warning: require_once(/var/www/dompdf-master/lib/php-font-lib/classes/font.cls.php): failed to open stream: No such file or directory in /var/www/dompdf-master/dompdf_config.inc.php on line 335
Fatal error: require_once(): Failed opening required '/var/www/dompdf-master/lib/php-font-lib/classes/font.cls.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/dompdf-master/dompdf_config.inc.php on line 335 

When I turn off DOMPDF_ENABLE_AUTOLOAD, I no longer get this warning, but the code fails with the following:

Fatal error: Class 'DOMPDF' not found in /var/www/rfq/test.php on line 115

The following is the code:

require_once("/var/www/dompdf-master/dompdf_config.inc.php");

$dompdf = new DOMPDF(); // this is the line that fails
$dompdf->load_html($quotehtml);
$dompdf->render();
$dompdf->stream("rfq".$_REQUEST['quoteid'].".pdf");

This is Ubuntu 12.04, up-to-date on patches, with default Apache settings.

Thanks so much.

7条回答
做自己的国王
2楼-- · 2019-02-02 21:43

I had almost the exact same problem. My code was working on my local dev machine - a Windows box - but then was failing on our production server - a Linux box

The problem was that the "classes" directory was lower case (\app\Vendor\dompdf\lib\php-font-lib\classes) which windows did not mind - but Linux being case-sensitive did!

Simply editing the following line in dompdf_config.inc.php solved the problem:

require_once(DOMPDF_LIB_DIR . "/php-font-lib/Classes/Font.php");

For consistency I renamed the directory with an uppercase "C" on the Windows box.

查看更多
何必那么认真
3楼-- · 2019-02-02 21:54
  • Go to https://github.com/PhenX/php-font-lib and download the library.
  • Create the dompdf/lib/php-font-lib/classes/ directory.
  • In the zip file, take the contents of the src/FontLib/ folder and paste that into your newly created directory.

That seemed to work for me.

查看更多
神经病院院长
4楼-- · 2019-02-02 22:01

If error is not corrected after doing what other answers suggest:

In dompdf_config.inc.php, change line 332 to point to the actual location of your Font.php  file.
mine was /php-font-lib/src/FontLib

There is no CLASSES folder that was mentioned there.

查看更多
时光不老,我们不散
5楼-- · 2019-02-02 22:04

If you are using composer to install dompdf, you need to put define("DOMPDF_ENABLE_AUTOLOAD", false); in dompdf_config.custom.inc.php. This will then allow composer to autoload the php-font-lib as it is already installed. (See this issue: https://github.com/dompdf/dompdf/issues/636)

If you are not using composer, see Mikepote's answer.

查看更多
戒情不戒烟
6楼-- · 2019-02-02 22:05

We will use dompdf in codeigniter BUT the file that I’v downloaded from GitHub doesn’t has all the files that we need. Is missing all the files of php-font-lib. So we had to download it and uploaded to the respective folder. So, to have dompdf working in codeigniter you might have to download it.

See here http://www.digitalwhores.net/codeigniter/codeigniter-dompdf-master-and-php-font-lib/

查看更多
Ridiculous、
7楼-- · 2019-02-02 22:05

The new version of dompdf doesn't work with 'composer install' or 'composer update'. It needs special versions of font libraries which might not always be the latest versions, so this might change in future. But you can find how to install it via dompdf's documentation. Dont know why the authors haven't hardcoded these font versions inside the composer.json, but anyways here is how to do it.

Currently the easiest and best way of using the library is via git (taken from the official docs)

git clone https://github.com/dompdf/dompdf.git
cd dompdf

git clone https://github.com/PhenX/php-font-lib.git lib/php-font-lib
cd lib/php-font-lib
git checkout 0.4
cd ..

git clone https://github.com/PhenX/php-svg-lib.git php-svg-lib
cd php-svg-lib
git checkout v0.1

Then you can just do

use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream( "/path-to-save-pdf-file/sample.pdf");
查看更多
登录 后发表回答