16

I'm having successful html-to-pdf conversions, but not with special characters.

Below is just a special character I'm trying to display, which displays in browsers on my Mac, when I put it simply inside an html document. (but not on my windows box)

<?php
require_once("../dompdf_config.inc.php");
$html = '&#8364;';
$dompdf = new DOMPDF(); $html = iconv('UTF-8','Windows-1250',$html);
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("contract.pdf");
exit(0);
?>

I keep getting a "?" (question mark) when the pdf is rendered. I know there's been lots of issues documented with regards to special characters, but I thought I'd give this a try, with the code I'm actually using.

If DomPdf isn't a recommended html-to-pdf conversion tool, I'll take any other recommendations!

2
  • Is the character you are trying for represented within the font you are using within the PDF?
    – Orbling
    Feb 27, 2011 at 21:52
  • 1
    I didn't know that was a factor. How would I find that out? Feb 27, 2011 at 22:08

5 Answers 5

18

I have experienced problems with DOMPDF when converting an UTF-8 html page. I simply solved the problem by adding

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

Between < head > tag. Maybe it could be an alternative if you set it with your encoding type.

IMPORTANT NOTE from comments below: don't use stream() and output() methods on the same pdf instance. If you do this wont work.

5
  • I lost so much time here, don't use stream() and output() methods on the same pdf instance. If you do this wont work.
    – dbr
    Mar 7, 2016 at 16:15
  • This symbol "<" is not getting printed in my Dom Pdf Please Help Mar 26, 2019 at 5:31
  • @JigneshMistry try using &lt; instead Mar 26, 2019 at 5:32
  • Cant add as data saved in database is as follows "Nasal Congestion with obstruction< night. Otherwise improved. Last Med...Kali carb 1M" so after obstruction nothing gets displayed in dompdf Mar 26, 2019 at 5:34
  • Replace with "obstruction&lt; night" Mar 26, 2019 at 9:15
10

after trying all solutions on the net. I could solve without modifying the dompdf. the problem was on the html content. I just had to add the correct and appropriate HTML structure and setting the font of course . Tested on v0.6.0 and v0.6.1. here I leave the code

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
  <meta http-equiv="Content-Type" content="charset=utf-8" />
  <style type="text/css">
    * {
      font-family: "DejaVu Sans Mono", monospace;
    }
  </style>
</head>

<body>your content ćčžšđ...</body>

</html>
8

DOMPDF Latin Turkish (Türkçe) char problem, my solution %100 Work.

Server requirenment control: Server requirenment control

Char 'dejavu sans mono' (Turkish support) OR:

Step 1: dompdf_config.inc.php edit to

mb_internal_encoding('UTF-8');
def("DOMPDF_UNICODE_ENABLED", true);

Step 2: lib/fonts/dompdf_font_family_cache.dist.php edit to add code:

'futural' => 
  array (
    'normal' => DOMPDF_FONT_DIR . 'FUTURAL',
    'bold' => DOMPDF_FONT_DIR . 'FUTURAL',
    'italic' => DOMPDF_FONT_DIR . 'FUTURAL',
    'bold_italic' => DOMPDF_FONT_DIR . 'FUTURAL',
  ),

Step 3: doqnload font files to copy lib/fonts folder. Download font files Link http://www.2shared.com/file/k6hdky_b/fonts.html

Step 4: Your code Edit example:

require_once("dompdf_config.inc.php");
$html='<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
    html{padding:-30px;}
    body { font-family: futural; }
</style>
</head><body>';
$html.='ı İ Ş ş ç Ç ö Ö ü Ü ğ Ğ ÿ þ ð ê ß ã Ù Ñ È »  ¿ İsa Şahintürk';
$html.='</body></html>';
if ( isset( $html ) ) {
    if ( get_magic_quotes_gpc() )
    $html = stripslashes($html);
    $old_limit = ini_set("memory_limit", "16M");
    $dompdf = new DOMPDF();
    $dompdf->load_html($html,'UTF-8');
    $dompdf->set_paper('a4', 'portrait');// or landscape
    $dompdf->render();
    $dompdf->stream("limitless.pdf");
exit(0);
}

End Finish PDF > exampleenter image description here http://limitsizbilgi.com/pdf/dompdf-chartest.pdf

1
  • My server was missing the mbstring extension. Thanks for the answer
    – Ejaz
    Oct 20, 2016 at 18:20
5

You must use another character set. For example dejavu Sans Mono. Add your code

<style>
*{
    font-family:"DeJaVu Sans Mono",monospace;
}
</style>

I also mentioned it in this video.

0
4

Anything prior to 0.6.x has limited support for characters outside iso-8859-1 encoding. The Euro is supported in 0.5.x by using the appropriate Windows ANSI character code (&#0128;), but otherwise you have to jump through some PDF encoding hoops.

The 0.6.0 release has better support for "special" characters. The default encoding is based on Windows ANSI (one of the few recognized by the PDF 1.3 spec). You can enable better character support by loading a Unicode-based font and enabling Unicode in dompdf and specifying that encoding in your document.

The following should work in dompdf 0.6.0 or greater:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
  <p>€</p>
</body>
</html>

(or to be lazy just use the euro entity &euro; in your test)

There is a document outlining the steps needed to enable Unicode support in DOMPDF. Plus read this answer for an overview of how to load fonts.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.