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();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");

How can i solve? Thanks!

 Answers

13

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

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):

...
$html="
<html>
  <body>
    <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);
      } 
    </script>
";
...

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
 
borrible
answered 7 Months ago
72

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.

https://github.com/StudentAffairsUWM/xlsxtocsv

Wednesday, March 31, 2021
 
Dev
answered 7 Months ago
Dev
28

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
 
shin
answered 5 Months ago
54

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.

<?php

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

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->setFontSubsetting(true);

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

$pdf->AddPage();

$utf8text = '
<html><head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>
<p>தமிழ்</p>
</body></html>';

$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 :