Friday, May 18, 2012

How to change "font-family" in dompdf script

In latest web development industry document conversion from one format to other format is very important. Sometimes we need to generate spread sheet files, some times we need to generate word files and some times PDF files. In this topic I will be talking about PDF file conversion.

For generating PDF files from the given data, there are a number of scripts and modules written in php and different languages. DomPDF is one of them written in php. DomPDF is fabulous in its features. It allows direct conversion of HTML files to PDF files. But there are some limitations like some times it never generates the exact copy of that HTML. Some times font-family doesn't apply correctly as we apply in HTML file. I had the same problem last days when I was using DomPDF for my PDF generation. I goggled the web and found some solutions. And on the basis of those many solutions and posts, I wrote a summarized article focusing on this problem.

It need some simple steps to follow,

  1. Download archive from approved answer. Extract files /dompdf/lib/fonts/times*.*.
  2. Copy them in your dompdf fonts folder.
  3. Edit dompdf_font_family_cache.dist.php with snippet 1.
  4. In CSS use font-family: times;.

Snippet 1:

 /* ... */  
 'times' => array (  
   'normal' => DOMPDF_FONT_DIR . 'times',  
   'bold' => DOMPDF_FONT_DIR . 'timesbd',  
   'italic' => DOMPDF_FONT_DIR . 'timesi',  
   'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'  
 ),  
 'times-roman' => array (  
   'normal' => DOMPDF_FONT_DIR . 'times',  
   'bold' => DOMPDF_FONT_DIR . 'timesbd',  
   'italic' => DOMPDF_FONT_DIR . 'timesi',  
   'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'  
 ),  
 /* ... */  

If you want to use your own TTF font (say, Arial.ttf):
  1. Run: ttf2afm -o Arial.afm Arial.ttf. (I did it in Ubuntu.)
  2. Run: ttf2ufm -a -F Arial.ttf. (you can use /dompdf/lib/ttf2ufm/bin/ttf2ufm.exe.)
  3. Copy Arial.* files in /dompdf/lib/fonts/.
  4. Add to dompdf_font_family_cache.dist.php snippet 2.
  5. In CSS use font-family: arial;.

Snippet 2:

 /* ... */  
 'arial' => array (  
   'normal' => DOMPDF_FONT_DIR . 'Arial',  
   'bold' => DOMPDF_FONT_DIR . 'Arial',  
   'italic' => DOMPDF_FONT_DIR . 'Arial',  
   'bold_italic' => DOMPDF_FONT_DIR . 'Arial'  
 )  
 /* ... */  

 Please feel free to post you comments and suggestions.

Thanks
 

8 comments:

  1. Really when someone doesn't understand afterward its up to other users that they will help, so here it occurs.
    my website - reklama internetowa

    ReplyDelete
  2. I really love your site.. Great colors & theme. Did you
    create this amazing site yourself? Please reply back as I’m attempting to
    create my own blog and would love to find out where you
    got this from or just what the theme is called. Appreciate it!

    instant payday loans
    Feel free to visit my web blog :: payday loans reviews

    ReplyDelete
  3. This is a topic that's near to my heart... Take care! Exactly where are your contact details though? best raspberry ketone
    My website ... raspberry ketone

    ReplyDelete
  4. Hi there, yeah this post is actually pleasant
    and I have learned lot of things from it concerning blogging.

    thanks.
    Here is my website - Vergleichen auf kfzversicherungsvergleich1.org

    ReplyDelete
  5. This website was... how do I say it? Relevant!! Finally I have found something that helped me.

    Kudos! espn
    Also visit my blog post tv live

    ReplyDelete
  6. Good site you have got here.. It’s hard to find high quality writing like yours nowadays.
    I truly appreciate people like you! Take care!! tv live
    my page > watch movies online

    ReplyDelete
  7. Hello, thanks for this blog it recently helped me much.
    The issue is that i have tried adding font family "Agency FB" several times using this guide but i really dont know where do i have made mistakes. Please can you help me with this please b'se i have my project which needs reports in pdf in such fonts ("Agency FB")

    ReplyDelete
  8. Thanks a lot for the snippets :)

    ReplyDelete

Please feel free to post your comments. If anyone has a good article or good thing to share, just send me that with your name to asadmehmoodstar@gmail.com. and if anyone want so receive updates regarding my blog, he can subscribe to my weekly newsletter on "Subscribe to our mailing list" section.

Thanks