29

i'm trying to convert some html to pdf. After some google search i find dompdf but when i try to convert i retrieve

PHP-font-lib must either be installed via composer or copied to lib/php-font-lib

This is what i'm trying to do:

require 'pdf/dompdf.php';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");

How can i solve? Thanks!

Fabien Ménager
  • 140,109
  • 3
  • 41
  • 60
Jayyrus
  • 12,961
  • 41
  • 132
  • 214

3 Answers3

48

Dompdf moved recently to Github, and we also added Composer support. For this, we removed the reference to the required external library php-font-lib.

Note: If you are using composer to install dompdf (and php-font-lib), you need to put define("DOMPDF_ENABLE_AUTOLOAD", false); in dompdf_config.custom.inc.php. This will then allow composer to autoload php-font-lib which is installed through dompdf's composer.json file. Otherwise, you may still get an error. (See this issue)

If you are not using composer and want to use the latest edition of dompdf with this library: (source)

  • Get the latest dompdf release from Github and extract it into a directory.
  • Download the library release also from Github
  • 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.
Community
  • 1
  • 1
Fabien Ménager
  • 140,109
  • 3
  • 41
  • 60
  • 29
    Please note that not everyone knows what composer or PHP-font-lib is. So adding some more information to that error message would be really helpfull. ;) – s.Daniel Nov 20 '12 at 13:47
  • Sorry for this, I should have been more verbose. – Fabien Ménager Nov 22 '12 at 21:33
  • Copied the php-font-lib from the above linked 0.6 beta 3 and it worked like a charm. Cheers :) – Jeemusu Jan 25 '13 at 03:21
  • 5
    This is still an issue I think. When not downloading via composer. It should be added to requirements info or installation info. – bottleboot Feb 25 '13 at 09:59
  • how to start from the scratch? I've made their script example works but I wanted to create my own. I haven't seen yet the tutorial for this, any link will help. Thanks – Ivo San Jun 21 '13 at 04:34
  • 3
    **Please notice** I ran into this answer but it didn't work for me. It seems that when the answer was written - the `php-font-lib` version was 0.2.2, using 0.4 won't solve it. Use 0.2.2 (https://github.com/PhenX/php-font-lib/releases/tag/0.2.2) – Ofir Baruch May 20 '15 at 13:01
  • Installing php-font-lib in Drupal 7 to be used by the print module 7.2.0 looks for version 0.2.2 file layout - 0.4 gives class not found – Chris Jun 22 '15 at 17:46
  • Also wanted to note that I had to download php-font-lib version 0.2.2 and extract the entire contents into /php-font-lib/ folder. – RobDigital Jul 02 '15 at 17:07
  • I had to change autoload.inc.php to change lib to vendor/phenx/ to the library paths. – Interlated Jan 02 '17 at 22:18
2

While my edit is being reviewed, I'll post the details here for the latest dompdf 0.6.1

If you are using composer to install dompdf (and php-font-lib), you need to put define("DOMPDF_ENABLE_AUTOLOAD", false); in dompdf_config.custom.inc.php. This will then allow composer to autoload php-font-lib which is installed through dompdf's composer.json file. Otherwise, you may still get an error. (See this Issue #636)

If you are not using composer and want to use the latest edition of dompdf, you will need to manually install php-font-lib: (https://stackoverflow.com/a/24505929/3854385)

  • Get the latest dompdf release from Github and extract it into a directory.
  • Download the library release also from Github
  • 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.
Community
  • 1
  • 1
Loren
  • 9,783
  • 4
  • 39
  • 49
0

Another trick, to avoid the change of automated downloaded files from Composer, is define the DOMPDF_ENABLE_AUTOLOAD and after reload the config file:

// Disable DOMPDF's internal autoloader if you are using Composer
define('DOMPDF_ENABLE_AUTOLOAD', false);
require_once CONFIG_DIR . 'vendor/dompdf/dompdf/dompdf_config.inc.php';

$dompdf = new \DOMPDF;
Fred Wuerges
  • 1,965
  • 2
  • 21
  • 42