Dompdf Unicode display as Question marks - php

I use dompdf for generate PDF. Unicode characters I use inside the document. In my Generated PDF document Unicode characters display as ???. My code are below.
$html='<!DOCTYPE html><html>';
$html.='<head><meta charset="utf-8"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style>
#font-face {
font-family: latha;
font-style: normal;
font-weight: 400;
src: url(http://eclecticgeek.com/dompdf/fonts/latha.ttf) format("true-type");
}body {
font-family: latha;
}
</style></head><body><h3>வணக்கம்</h3></body></html>';
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();

Related

Tamil Unicode not rendering DOMPDF

I download Bamini font and try to download pdf. I got ??????????????? for tamil words.
$html='<!DOCTYPE html><html>
<style>
#font-face {
font-family: "Baamini";
src: url("baamini.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}
body{
font-family: "Baamini";
}
</style>';
$html.='<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
$html.='<body><table style="border:1px solid;" width="100%"><tr><td colspan="2" align="center" style="border-bottom:1px solid;">';
$html.='<h3 style="font-family: Baamini;">சமூக நல அமைப்பு <h3>';
$html.='<h5>தொடர்பு எண் : ##########</h5></td></tr></table></body></html>';
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();

dompdf not generating .ufm and dompdf_font-family_cache.php file

I am trying to use custom font in dompdf while generating pdf file. As i found that custom font works only after generating .ufm and cache file.
Drupal php file
public function GeneratePdf() {
$dompdf = new Dompdf();
$fontDirectory = $host.'/'.$themepath . '/fonts';
$options->setChroot($fontDirectory);
$dompdf->setOptions($options);
ob_end_clean();
$dompdf->getFontMetrics()->registerFont(
['family' => 'montserrat', 'style' => 'normal', 'weight' => 'normal'],
$fontDirectory . '/Montserrat-Regular.ttf'
);
$dompdf->loadHtml($html);
$dompdf->render();
ob_end_clean();
}
Pdf generation HTML .twig file.
<html>
<head>
<style type="text/css">
#page {
size: auto;
margin: 0;
padding: 0;
}
#font-face {
font-family: montserrat;
src: url($themepath ~'/fonts/Montserrat-Regular.ttf') format('truetype'); font-style: normal;
}
</style>
</head>
<body style="margin:0; padding:0;">
<span style="font-family:'montserrat';color:#417bac; font-size:18px">PDF font testing</span>
</body>
</html>
Issue is after registering font through php function required files i.e .ufm and dompdf_font-family_cache.php not created in dompdf folder vendor\dompdf\dompdf\lib\fonts

Dompdf not supporting chinese languages

Chinese characters work fine in the HTML page, but when i try to covert HTML to PDF with below code, all Chinese characters got converted to question marks.
I tried to use all types of chinese font libraries but still no success. Please help me fix it.
$html = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type: application/pdf; charset=utf-8"/>
<link rel="stylesheet" href="style.css">
<div style="width:800px; height:970px; padding:20px; border: 10px solid #787878">
<style>
body {
body {font-family: "simsun"}
}
</style>
<div style="width:750px; height:915px; padding:20px; border: 5px solid #787878">
</br>
<div><img class="imgA1" src="logo.png" ></div>
<!--<img class="imgB1" src="logo.png">-->
</br></br></br></br>
<h6 style="font-size:16px !important; text-align:center;"> <b>'.$goal.'</b></h6><br/><br/>
<b>立約人</b></br></br>
甲方 : <span style="font-size:18px;"><b>'.$user[0]['user_name'].'<b> </span>
<br><br>
乙方 : <span style="font-size:18px;"><b>'.$judge_name.'</b></span><br/><br/>
<b>四. 本合約壹式貳份,雙方各執壹份為憑。</b><br/><br/>
裁判簽名___________________<br/><br/>s中華民國 月 日
</div>
</div></html>';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
/* Render the HTML as PDF */
$dompdf->render();
header('Content-Type: application/pdf; charset=utf-8');
header('Content-disposition: inline; filename="' . $no . '.pdf"', true);
/* Output the generated PDF to Browser */
$dompdf->stream();
#font-face {
font-family: 'Firefly Sung';
font-style: normal;
font-weight: 500;
src: url(http://eclecticgeek.com/dompdf/fonts/cjk/fireflysung.ttf)
format('truetype');
}
* {
font-family: Firefly Sung, DejaVu Sans, Verdana, Arial, sans-serif;
}

Bold Font for dompdf 0.7.0 beta

I'm using DOMPDF ver. 0.7.0 to make a pdf file from HTML, but I have problem when I need to use custom font (arial) with bold style. the bold font won't work after rendering.
here's the CSS style I'm using now:
#font-face {
font-family: 'font';
font-style: normal;
font-weight: normal;
src: url(dompdf/font/arial.ttf);
}
#font-face {
font-family: 'font';
font-style: normal;
font-weight: bold;
src: url(dompdf/font/arialbd.ttf);
}
body{
font-family: 'font';
}
and here's dompdf - php code:
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents('pdf/sptjm.pdf', $pdf)
here's the result in HTML:
and here's the result in PDF:
the pdf didn't show the bold type, please help. thanks in advance.
i have done it simply with using different font name:
#font-face {
font-family: 'font';
font-style: normal;
font-weight: normal;
src: url(dompdf/font/arial.ttf);
}
#font-face {
font-family: 'font2';
font-style: normal;
font-weight: bold;
src: url(dompdf/font/arialbd.ttf);
}
body{
font-family: 'font';
}
so when i need the bold font type, just simply call the other font name 'cuz basically it is different font source.

DomPDF generation for chinese characters

I am trying to generate a PDF that will contain Chinese characters using dompdf.
Here is my code:
require('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
mb_internal_encoding('UTF-8');
def("DOMPDF_UNICODE_ENABLED", true);
$html = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
*{ font-family: DejaVu Sans, font-size: 12px;}
</style> </head> <body>
忠烈祠
</body>
</html>';
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
$filename = 'a.pdf';
$path = $filename;
file_put_contents($path, $output);
The problem is the the generated PDF show only squares when i open it with chrome or adobe reader, but it looks ok in Mozilla firefox.
Any sugestions?
First, move def("DOMPDF_UNICODE_ENABLED", true); above the require since the def function included with dompdf only defines the constant if it doesn't exist. When you include dompdf_config.inc.php that constant will be set at that time. Also, use define instead.
Second, you need a different font. The DejaVu fonts included with dompdf do not support CJK. For an overview read my answer to Dompdf and set different font-family.
If you do not currently have a font you might try looking up Firefly Sung. Whatever font you decide to use it should be TTF in order for dompdf to use it.
Here's an example using Firefly Sung via the #font-face CSS rule, no installation required:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
#font-face {
font-family: 'Firefly Sung';
font-style: normal;
font-weight: 400;
src: url(http://eclecticgeek.com/dompdf/fonts/cjk/fireflysung.ttf) format('truetype');
}
* {
font-family: Firefly Sung, DejaVu Sans, sans-serif;
}
</style>
</head>
<body>
<div>忠烈祠</div>
</body>
</html>

Categories