Asked  7 Months ago    Answers:  5   Viewed   34 times

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();

How can i solve? Thanks!



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 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.
Wednesday, March 31, 2021
answered 7 Months ago

You are running your inline script as part of the PHP page instead of passing it to dompdf. Your code could be written as follows (truncated to the relevant section):

    <script type='text/php'>
      if ( isset($pdf) ) { 
        $font = Font_Metrics::get_font('helvetica', 'normal');
        $size = 9;
        $y = $pdf->get_height() - 24;
        $x = $pdf->get_width() - 15 - Font_Metrics::get_text_width('1/1', $font, $size);
        $pdf->page_text($x, $y, '{PAGE_NUM}/{PAGE_COUNT}', $font, $size);

Note that inline script must currently appear inside the BODY element. Otherwise it will be ignored during document processing.

There are other ways to achieve what you want as well.

Wednesday, March 31, 2021
answered 7 Months ago

So like we had thought we had some illegal characters. What my coworker who was working on this with me did was find a XLSX to CSV converter and rewrote a little bit and add it in as a composer package. If anyone else runs into this issue the repo is here. I'll try and add some docs soon.

Wednesday, March 31, 2021
answered 7 Months ago

You have no delimiter. Enclose the pattern in /

preg_match('/name="dsh" id="dsh" value="(.*?)"/', 'name="dsh" id="dsh" value="123"', $matches);

For patterns that include / on their own, it is advisable to use a different delimiter like ~ or # to avoid escaping:

// Delimited with # instead of /
preg_match('#name="dsh" id="dsh" value="(.*?)"#', 'name="dsh" id="dsh" value="123"', $matches);
Saturday, May 29, 2021
answered 5 Months ago

Dompdf appears to have issues with UTF-8 encoding. I tried your sample with dompdf and I ran into the same issue you did. However, I also tried it with tcpdf (also free) it appears to work well.

enter image description here

Here is the code for your sample, and the full source can be downloaded here.


header('Content-type: text/html; charset=UTF-8') ;//chrome

// create new PDF document


$pdf->SetFont('freeserif', '', 12);


$utf8text = '
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>

$pdf->writeHTML($utf8text, true, 0, true, true);

$pdf->Output('example_008.pdf', 'I');

Sunday, August 22, 2021
Martijn Pieters
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :