dompdf special character showing question mark?

2019-07-28 21:42发布

问题:

I have used dompdf 0.5.1 for generating PDF files. But the special characters are not properly showing.

For example, .

It is showing something like – “ in the generated PDF file.

I used UTF-8 encoding like <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> in the HTML page which is rendered by the dompdf.

I also have used the encoding before sending it to dompdf, like $dompdf->load_html(utf8_decode($html));.

But I get ? marks instead of the above characters.

How do I solve the above problem?

回答1:

Dompdf 0.5.1 has limited support for characters that aren't supported by Windows ANSI encoding. If you need to support these characters you should update to, at the very least, Dompdf 0.6.2. Though I'd recommend using Dompdf 0.7.0 if you can.

You'll need to supply a font that supports your characters (see the Unicode How-To), but so long as you're not trying to render CJK characters you can probably rely on the included DejaVu fonts.

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <style>
    * { font-family: DejaVu Sans, sans-serif; }
  </style>
</head>
<body>
  <p>—</p>
</body>
</html>

Also, you should never use utf8_decode() as it will destructively convert to iso-8859-1 encoding. By destructively I mean that it will change characters it can't directly convert to iso-8859-1 in question marks (?).