31

I'm Using DOM PDF 0.6.0 Beta 2. I want to use custom fonts (Fonts: 'Segeo Print', 'Lucida Handwriting','Airplanes in the Night Sky') in PDF file.

I followed the guidelines to install and use fonts in my PHP Code, which is given here http://code.google.com/p/dompdf/wiki/CPDFUnicode

But I'm not able to get desire fonts in my PDF. You can find my code in this post. Please Let me know how I can resolve this issue.

<?php     
    require_once("dompdf_config.inc.php");

   $html = "<html>
                <head>   
                    <meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
                    <style>
                        *{font-size:15px;}  
                        div.ClJ{font: nightsky;}   
                    </style>      
                </head>
                <body>            
                   <div class='ClJ'>This text is in DIV Element</div><br /><br />
                </body>
          </html>";

    $dompdf = new DOMPDF();    
    $dompdf->load_html($html);
    $dompdf->render();
    $pdf = $dompdf->output();
    $dompdf->stream("dompdf_out.pdf", array("Attachment" => false));

?>    
hakre
  • 193,403
  • 52
  • 435
  • 836
pinaldesai
  • 1,835
  • 2
  • 13
  • 22
  • If you're still on beta 2, try upgrading to beta 3. It has better support for custom fonts, now supports font-subetting, and has an easier font-loading mechanism (the how-to needs to be updated to reflect these changes). – BrianS Sep 25 '12 at 15:24
  • FYI, your code looks fine so it's probably an issue with the font itself (e.g. font didn't load correctly). – BrianS Sep 25 '12 at 15:28
  • 1
    Thank your the comment. Will try to upgrade to beta 3 lets see how its turn out. – pinaldesai Sep 28 '12 at 04:56

3 Answers3

33
  1. go to your DOMPDF folder
  2. copy your font's as .ttf (TrueType Font) or .otf (OpenType Font) into the DOMPDF's root
  3. open your command line and run
    php load_font.php your_fonts_name ./your-normal.ttf ./your-bold.ttf ./your-bold-italic.ttf
  4. DOMPDF now created Adobe Font Metrics and copied it to lib/fonts/* - you can now use it with
    font-family: your_fonts_name;
Ueli
  • 2,301
  • 5
  • 25
  • 29
  • @ueli : I dont did not instal but just copy pasted dompdf. how shall i do? – logan Jun 22 '14 at 18:48
  • 1
    I think there's a comma after the second filename that may cause error, the code should be: php load_font.php your_fonts_name ./your-normal.ttf ./your-bold.ttf ./your-bold-italic.ttf – Macumbaomuerte Oct 01 '14 at 12:07
  • 5
    This does not work for me. Pulled in fresh version from github (https://github.com/dompdf/dompdf/). When running the command, I get: `Warning: require_once(/Users/me/Downloads/dompdf-master/lib/php-font-lib/classes/Font.php): failed to open stream: No such file or directory in /Users/me/Downloads/dompdf-master/dompdf_config.inc.php on line 332` – FooBar Mar 17 '15 at 15:41
  • @Kannu why is it inappropriate? – Ueli Jul 13 '15 at 08:26
  • @Mattias and anybody else who pulls DOMPDF from github. You must initialise and update the submodules. This is why you got file not found error "git submodule init && git submodule update" – Chris Gibb Jan 28 '16 at 23:37
  • 7
    answer doesn't work for composer based insallatation of DomPDF. Also, this is not correct way to change core directory – Bhumi Shah Jan 02 '18 at 11:59
12

you can add css font :

@font-face {
    font-family: new_font;
    src: url('my_font.ttf');
}

and than

div.ClJ{
    font-family: new_font; 
}
chokrijobs
  • 761
  • 1
  • 6
  • 10
  • I don't think so this is accessory as I have already installed fonts for DOM PDF and definition of fonts are already available in dompdf_font_family_cache.dist file – pinaldesai Sep 25 '12 at 10:48
  • 1
    where should i keep this css part ? – logan Jun 22 '14 at 18:54
-1

If you want to install custom fonts to the server without a command-based interface then you can do the following web-based model to install the custom fonts

  1. Download ejaz.php from here
  2. Put this file to the root of dompdf and follow the instruction written in it

no need for root or command/terminal access

wscourge
  • 10,657
  • 14
  • 59
  • 80
Ejaz47
  • 145
  • 1
  • 12