3

I am using dompdf to generate PDFs from inputs. The code I use:

require_once (STYLESHEETPATH.'/dompdf/include/autoload.inc.php');
set_include_path(STYLESHEETPATH.'/dompdf');

require_once STYLESHEETPATH.'/dompdf/dompdf_config.inc.php';

$dompdf = new DOMPDF();

$html = '
<html>
 <body>
  <h1>Hello <i>'. $fullname.'</i> </h1>
  <p> your father is <b>'.$fathername.'</b>
 </body>
</html>';

$dompdf->load_html($html);
$dompdf->render();

$dompdf->stream("tck.pdf"); ?>

This works very fine and generates a pdf called tck.pdf

Now I want to style the fonts in the pdf so I've tried

require_once (STYLESHEETPATH.'/dompdf/include/autoload.inc.php');
set_include_path(STYLESHEETPATH.'/dompdf');

require_once STYLESHEETPATH.'/dompdf/dompdf_config.inc.php';

$dompdf = new DOMPDF();

$html = '
<html>
    <head>
        <style>
            @font-face {
              font-family: "Open Sans";
              font-style: normal;
              font-weight: 400;
              src: url(http://themes.googleusercontent.com/static/fonts/opensans/v8/cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf) format("truetype");
            }
        </style>
    </head>
 <body>
  <h1>Hello <i>'. $fullname.'</i> </h1>
  <p> your father is <b>'.$fathername.'</b>
 <br/>
  <span style="font-family:webfontregular;">bar code will be here</span>
    <div style="font-family:"Open Sans";">Open Sans</div>
 </body>
</html>';

$dompdf->load_html($html);
$dompdf->render();

$dompdf->stream("tck.pdf"); ?>

But it does not work and giving an error as follow :

Fatal error: Class 'Font' not found in /home4/q0i4l0g2/public_html/tck/wp-content/themes/kallyas-child/dompdf/include/font_metrics.cls.php on line 356

I've noticed that whenever I use "@font-face" I am getting this error. Why is that? How can I make it work?

As per the accepted answer in this question Dompdf and set different font-family this should work I think but it does not.

Community
  • 1
  • 1
Foolish Coder
  • 385
  • 5
  • 20
  • 1
    Possible duplicate of [DOMPDF - Class 'Font' not found](http://stackoverflow.com/questions/22226218/dompdf-class-font-not-found) – Chris Apr 08 '16 at 21:48
  • @Chris No it's not.. if the answers of that questions solved this issue there is no chance to get 4 upvotes for this question... – Foolish Coder Apr 11 '16 at 06:12
  • So you're saying you've tried downgrading php-font-lib? – Chris Apr 11 '16 at 11:14

1 Answers1

0

When using dompdf versions prior to 0.7.0 you should not include the autoloader directly. This is done as part of the setup process performed when you include the dompdf_config.inc.php file. That setup process defines a number of constants used by dompdf as well references the autloaders for dompdf and the various libraries is uses (including php-font-lib).

If the dompdf class is already available the startup script will exit without doing anything.

Also potentially relevant is how you got dompdf. If you did not download a packaged release (i.e. you downloaded the various libraries separately) make sure you grab php-font-lib 0.3.x, the version used by the dompdf 0.6 releases.

BrianS
  • 13,284
  • 15
  • 62
  • 125