PHP DOMPDF html to PDF converter font rendering is

2019-04-16 16:30发布

问题:

I am using DOMPDF v.0.6.1 downloaded from github to convert HTML to PDF file. I am facing fontdisplay issue with while adding my custom font in configuration. dompdf_font_family_cache.dist.php

I used http://eclecticgeek.com/dompdf/load_font.php to generate Unicode TAMIL font(latha by microsoft font) .I have copied the fonts into respective folders.

But I see the fonts are not printing as it is.

My code:

<p style="font-family: tamil-latha, verdana, sans-serif;" >தமிழ்</p>

But what is printed in PDF is :

Both the values are not Same. There is some box is coming near the font and a dot on last character is missing..

Note: I have also added newly created font into dompdf_font_family_cache.dist.php

    'tamil-latha' => 
  array (
    'normal' => DOMPDF_FONT_DIR . 'latha',
    'bold' => DOMPDF_FONT_DIR . 'latha',
    'italic' => DOMPDF_FONT_DIR . 'latha',
    'bold_italic' => DOMPDF_FONT_DIR . 'latha',
  ),

I have raised the same question in github here : https://github.com/dompdf/dompdf/issues/838

Suggested Solution was: use dompdf v.0.6.2 But there is no such version available here : https://github.com/dompdf/dompdf Help me if you are able to get v.0.6.2

回答1:

Dompdf appears to have issues with UTF-8 encoding. I tried your sample with dompdf and I ran into the same issue you did. However, I also tried it with tcpdf (also free) it appears to work well.

Here is the code for your sample, and the full source can be downloaded here.

<?php

header('Content-type: text/html; charset=UTF-8') ;//chrome
require_once('tcpdf.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->setFontSubsetting(true);

$pdf->SetFont('freeserif', '', 12);

$pdf->AddPage();

$utf8text = '
<html><head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>
<p>தமிழ்</p>
</body></html>';

$pdf->writeHTML($utf8text, true, 0, true, true);

$pdf->Output('example_008.pdf', 'I');

?>


回答2:

I tried with tcpdf as suggested by josh. Now the previous word is coming fine but new word is not displayed properly.

$utf8text = '
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>     <body><p>தமிழ்      முந்திரி </p></body>        </html>';

Actual Answer in PDF is :