Tamil Unicode not rendering DOMPDF - php

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();

Related

Dompdf rendering spaces incorrectly with custom font

I'm using Dompdf v2.0.1 to make a report. I'm using a custom font from Google Fonts (Josefin Sans) but the spaces are rendered incorrectly.
But when rendering the page as HTML, spaces are displayed correctly.
Below I'll paste the HTML and PHP code I'm using to generate the PDF.
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght#300;400;700&display=swap" rel="stylesheet">
<title>Relatório de atendimentos</title>
<style>
#page {
margin: 16px;
padding: 0;
}
body {
font-family: 'Josefin Sans', sans-serif;
font-size: .8em;
}
table {
width: 100%;
border-collapse: collapse;
border: none;
}
td, th {
padding: .3em;
}
td {
border: solid 1px #000;
font-weight: 300;
}
.title {
font-size: 1.2em;
color: #FFF;
text-align: center;
background-color: #363435;
border-color: #363435;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th class="title">Relatório detalhado de tickets</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<b>GERADO POR: </b> sadsadsad
</td>
</tr>
</tbody>
</table>
</body>
</html>
<?php
declare(strict_types=1);
namespace App\Actions\Report;
use Dompdf\Dompdf;
class GenerateReport
{
public static function run(array $data): string
{
return GenerateReportString::get($data);
$pdf = new Dompdf([
'isRemoteEnabled' => true,
'isJavascriptEnabled' => false,
'dpi' => 200,
]);
$pdf->setPaper('A4', 'landscape');
$pdf->loadHtml(
GenerateReportString::get($data)
);
$pdf->render();
return $pdf->output();
}
}
I tried setting the charset to UTF-8 in the HTTP header (Content-Type: application/pdf; charset=UTF-8), but the result is the same.
This is due to a font subsetting bug in the underlying php-font-lib library, see issue 2445. Not all fonts are impacted, but for those that are you can temporarily work around the issue by disabling subsetting.
$dompdf = new Dompdf(["IsFontSubsettingEnabled" => false]);
Note: This isn't a great solution if the font is large since it will impact the overall PDF size.

Dompdf Unicode display as Question marks

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();

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.

Categories