Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
828 views
in Technique[技术] by (71.8m points)

php - DOMPDF problem with Cyrillic characters

I am using the DOMPDF library to create an invoice in PDF. This document can be in French, Russian or English, but I am having trouble printing Russian characters.

First, I tried to use UTF-8 encoding and placed the meta tag in the head of the HTML page to be converted:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

But that didn't work.

Then I inserted this meta tag inside the BODY tag, and it helped solve the problem with French characters.

But Russian characters still don't work. I have also tried to convert Russian characters into HTML entities, but that too does not work.

I use R&OS CPDF class, not PDFLib as a backend.

Can anyone help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In accepted answer link is broken and it contained old version of DOMPDF.

To work with unicode symbols in DOMPDF 0.6 you have two alternatives: use existed fonts or create your own font.

  • Use existed font (applied for DOMPDF 0.6):
  1. Download archive and extract.
  2. Copy extracted files in your dompdf fonts folder /dompdf/lib/fonts/.
  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. (I did it in Windows using exe from UFPDF, but I guess 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'
)
/* ... */

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...