UTF-8 words not display in laravel-dompdf - php

I write a simple code for rendering chinese words in pdf. But the chinese words does not appear.
This is my code.
Route::get('/pdf', function () {
$html = '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<p style="font-family: firefly, DejaVu Sans, sans-serif;">献给母亲的爱</p>
</body>
</html>';
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML($html);
return $pdf->stream();
});
I'm using
1) PHP 7.1.1
2) Laravel 5.3
I inserted the meta into the header and change the font. What might gone wrong here?please help thanks.

Turn out that default font provided within dompdf does not support chinese character. You have to insert your own custom fonts in order to display the chinese words. Refer here Insert Custom Font on how to insert and use custom fonts in dompdf.

The better way is to use this package:
composer require vatttan/apdf
Documents

Related

How to make fonts in DOMPDF library

I am using the "DOMPDF" library with Laravel to create a PDF. It needs to be in Arabic with a font of dejavu sans mono or dejavu sans.
But, I have trouble with the fonts Times-Roman and Helvetica.
<head>
<meta charset="utf-8">
</head>
<style>
* { font-family:"times-roman" ,normal; }
</style>
Can anyone please help?

Custom Font issue in DomPDF latest version 0.8.2

I am using composer installation of DomPDF. so i can't use custom font. I have used font-face option but font is not applying in PDF.
$dompdfOptions->set('fontDir', CSSPATH);// for default fonts
$dompdfOptions->set('fontDir', FONTSPATH);// for custom fonts
$dompdfOptions->set('defaultMediaType', 'all');
$dompdfOptions->set('isFontSubsettingEnabled', true);
$dompdf = new Dompdf($dompdfOptions);
but font is not apply.
if i following style:
"font-family: Georgia;" // Georgia is example
not working but if i use
"font: 24px Georgia;"
font is working but if i add font styles like italic/bold, again it is not working.
Please let me know what is issue in this.
Thanks
I have created demo to use Custom Fonts in DomPDF.
<?php
$html = '<!DOCTYPE html>
<html lang="en" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<title>DUMMY DOM PDF</title>
<style>
#import url("https://fonts.googleapis.com/css?family=Joti+One");
.joti-font {font-family: "Joti One", cursive;}
</style>
</head>
<body>
<div class="joti-font">This is Test PDF</div>
</body>
</html>';
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("codex",array("Attachment"=>0));
?>
Use css and it worked. we need to use font instead of font-family in css.
#font-face{
font-family: myFontName;
src:url('http:://yourwebsite.com/myFontName.ttf');
}
p{
font: myFontName;
}
For your custom font issue, mpdf is better than DomPDF and others. it has following features
add new fonts
Full Unicode support
Complex scripts support
3.1 Right-to-left languages (Hebrew, Arabic etc.)Permalink
3.2 Indic languages, Lao, Tibetan etc
3.3 Vertical writing
TrueType Collections
Unicode Supplementary Planes
How to add new fonts with mpdf
Add your font directory to fontDir configuration parameter or by
calling $mpdf->AddFontDirectory() method
Define the font file details in the fontData parameter array
Access the font by specifying it in your HTML code as the CSS
font-family
Specifying languages for the font by defining custom
Mpdf\Language\LanguageToFontInterface and
Mpdf\Language\ScriptToLanguage implementation

dompdf working fine with non-latin languages but for Arabic, the words are broken not proper

I am using the dompdf library with CodeIgniter PHP and in my application, I have to convert the HTML to PDF through DOMPDF but I am facing issue while the HTML content contains the Arabic text.
Have you included meta tag ?
Please write this line at top of your view file if not there.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

UTF-8 is not work in vsmoraes/laravel-pdf 1.0.1

I have just work with laravel 5, and now I want to use dompdf to work with laravel 5 too. Surely, I can use it to convert my html to pdf successfully. But the problem I encounter is about UTF-8. I can only get English language, but not my own language.
HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<p>លីណា</p>
</body>
Controller:
public function pdf()
{
$html = view('pdfs.pdf')->render();
return PDF::load($html)->filename('lina.pdf')->show();
}
dompdf font family cache
'khmer-os' =>
array (
'bold' => DOMPDF_DIR . '/lib/fonts/Battambang-Bold',
'normal' => DOMPDF_DIR . '/lib/fonts/Battambang',
),
I guess I need to loan new font in dompdf->lib->font, but I don't know how to do that. Or you have other ways to make UTF-8 work with this.
Thank you for your review.

Handling Cyrillic Encoding

I have a small problem, I'm using a .php page to show Russian characters. Now, I do this in two different ways. The text I take from the database shows fine, but if I show text like this
<h2>привет </h2> it doesn't show it at all. One thing worth mentioning is that I include my navigation file require_once "navigation.php" where I declare all the meta data etc.
So from navigation.php:
<meta charset="Cyrillic(Windows-1251)">
Solutions I've tried so far:
Adding encoding to .htaccess
Using php to output header encoding
Using Cyrillic(Windows-1251), UTF8, UTF16, iso-8859-5
Saving the file itself as UTF8
And I'm out of ideas. What I don't understand is how echoing the text from database works, but the HTML version doesn't.
Here this should work
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-5">
<h2>привет </h2>
So the problem was somewhere in the JS. For this project, I was given complete HTML works, just to hook PHP to it. The HTML, among other things, had this:
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/cufon-replace.js"></script>
I have no idea what it does except it makes fonts smaller (why use JS for that is beyond me). So simple removal of that left me with a bit bigger font size, but working Cyrillic characters.
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

Categories