Search code examples
phpdompdf

Dompdf unicode character (only on server)


I use dompdf to render my html pages to pdf. I prepared templates in php and used the codes below. It works fine on localhost, but fails (shows question marks instead of unicode chars) when I upload it on the server. And FontAwesome also fails to show icons.

generator.php

<?php
session_start();
ob_start();
//dompdf
require_once('../../dompdf/autoload.inc.php');
use Dompdf\Dompdf;
require_once('../pdf-template/template.php');
$template=ob_get_clean();
$dompdf=new Dompdf();
$dompdf->loadHtml($template);

//paper
$dompdf->setPaper('A4','portrait');

// render
$dompdf->render();
$pdf_content = $dompdf->output();
file_put_contents('test.pdf',$pdf_content);
echo "successful";
?>

template.php

<i class="fa fa-paw"></i>
<h2 style="font-family: DejaVu Sans; font-size: 22px">ığĞüÜşŞİöÖçÇ</h2>

Solution

  • I solved the problem. It is about the dompdf_font_family_cache.dist.php. Once it is triggered on localhost, it changes the folder according to my computer. So the font doesn't change on the server. To solve, The php files in dompdf/lib/fonts folder should be deleted and the clean "dompdf_font_family_cache.dist.php" you get from Github/DOMPDF should be uploaded.