How to make DOMPDF support unicode font step by step?

18,556

Solution 1

  1. Enable the mbstring extension
  2. Find a font in .ttf format with characters that support your given language
  3. Generate a .afm file for DOMPDF:
    1. Using the command line, cd into the directory which contains load_font.php, then run php load_font.php FontName /path/to/font.ttf
    2. Using the online tool, fill out the form and download the zip it gives you. Copy the files from this zip into dompdf/lib/fonts. If you've done this before, add relevant lines to dompdf_font_family_cache, otherwise simply rename the dompdf_font_family_cache.sample to dompdf_font_family_cache
  4. Enable DOMPDF's Unicode mode, by making sure the line

    define("DOMPDF_UNICODE_ENABLED", true);
    

    is set in dompdf_config.inc.php. If it reads false instead of true, change that.

Solution 2

There is a wiki page for this.

The next beta (0.6 beta 3), to be released soon, has a font installer that doesn't require the command line (neither any compilation). You already can use it by checking out the SVN trunk. This beta also supports @font-face.

Solution 3

As of version 0.7, dompdf supports and has unicode enabled by default. (therefore there is no need for configuration).

Not all dompdf fonts support unicode though. I used font-family: DejaVu Sans with success.

To install a custom font or learn more about it you can read the official unicode how to.

Solution 4

Download load_font.php and place in ur project root

View load_font.php

add $fontDir = "storage/fonts";

This will show where the fonts are added.

download your font at eg: PREETI and place at your project root

run in command line php load_font.php PREETI PREETI.ttf

then you font will be installed to storage/fonts directory

now go to the pdf file and add

<style>
    body {
        font-family: PREETI;
        color: #1c2221;
    }
</style>
Share:
18,556
Piseth Sok
Author by

Piseth Sok

I like NodeJS. I like Express. I like ReactJS. I like PHP and MySQL. I like Laravel. I like Arduino Programming.

Updated on June 05, 2022

Comments

  • Piseth Sok
    Piseth Sok almost 2 years

    I used DOMPDF library and now I have problem with content with unicode language. and I found this one dompdf help but I don't understand about this. can anyone tell the detail of this? thanks

  • Piseth Sok
    Piseth Sok over 12 years
    I got message: Unable to locate the ttf2afm / ttf2pt1 executable (checked /home/piseth/dev/example/pdf_php/lib/ttf2ufm/ttf2ufm-src/ttf‌​2pt1).
  • Piseth Sok
    Piseth Sok over 12 years
    The SVN trunk not work I have def("DOMPDF_UNICODE_ENABLED", true); change this also but still not work.
  • Saša Šijak
    Saša Šijak over 12 years
    Sounds like you need to install tt2afm or tt2pt1. Try ttf2pt1.sourceforge.net
  • BrianS
    BrianS over 12 years
    @user970587 it would help to see a sample of the code you're attempting to render (and more detail about what you've tried)
  • deem
    deem almost 11 years
    I tried your instruction, but it doesn't work. I read in another topic that I should add css about font family to the print.tpl and it really works then.
  • Mr. Tomar
    Mr. Tomar about 8 years
    I followed the above instruction, but not work for me.
  • Mr. Tomar
    Mr. Tomar about 8 years
    when i generate the pdf form html which contain burmese language( நான் என் முதலாளி வீட்டில் ஜன்னல்கள் வெளிப்புறம் சுத்தம் செய்ய தேவையில்லை என்று புரிந்துகொள்ளுங்கள் . ) then in pdf its show ???????????????????????????????????????????????
  • Vinoth Kumar
    Vinoth Kumar over 2 years
    Any possibilities to display tamil content by using dompdf. I have used dompdf in codeigniter and trying to display tamil content but it showing 1.??? ????? ?????? ??????? any one can help me on this