Asked  2 Years ago    Answers:  5   Viewed   91 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.
Tuesday, November 29, 2022

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.

Friday, November 18, 2022

I got it working now! Thanks to this phpexcel to download

I changed the code to this:

// Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 formatn";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
// We'll be outputting an excel file
header('Content-type: application/');
header('Content-Disposition: attachment; filename="payroll.xlsx"');

I think this line:


solved my problem.

Thursday, November 3, 2022

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);
Tuesday, December 27, 2022

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.

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

Friday, September 23, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages