Custom Font issue in DomPDF latest version 0.8.2 - php

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

Related

Use custom font face in DomPDF

I'm working on a project that needs to convert an HTML page to PDF.
I am using dompdf library in my project, however, I'm having trouble with embedding custom fonts in the PDF preview. I was expecting pdfdom will automatically embed any attached fonts like it normally work in HTML. Or is there any configuration needed first?
Here's the HTML content
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
<link href='http://fonts.googleapis.com/css?family=Give+You+Glory&v2' rel='stylesheet' type='text/css'>
</head>
<body>
<h2>Give You Glory</h2>
<p style="font-family: 'Give You Glory', sans-serif;">
Grumpy wizards make toxic brew for the evil Queen and Jack
</p>
</body>
</html>
And here's how the library usage:
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'landscape');
$dompdf->loadHtml($html_content);
$dompdf->render();
$dompdf->stream("testing.pdf", array("Attachment"=>0));

DomPDF: iconv(): Wrong charset, conversion from `utf-8' to `us-ascii//TRANSLIT' is not allowed

Laravel Project: 7.6.1
DomPdf: 0.8.7
I've created a function to generate a pdf from a html file. This function is loaded at the click of a button.
First time, I put my css directly in my html page and there was no problem. But now, I would like to put it in an external file.
Now, when I click on the button, I've the error:
iconv(): Wrong charset, conversion from `utf-8' to `us-ascii//TRANSLIT' is not allowed
Head of my Html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Laravel PDF</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link href="./css/app.css" rel="stylesheet">
<script src="./js/chart/report.js" ></script>
<style>
#page { margin: 100px 25px; }
</style>
</head>
My controller
$pdf = PDF::loadView('report.pdf.index-pdf', compact('datas'));
$pdf->setPaper('A4', 'p');
return $pdf->download('report.pdf');
Somebody to help me?

dompdf dont load bootstrap css with barryvdh/laravel-dompdf

i use barryvdh/laravel-dompdf in laravel 5.5 and bootstrap css (renamed to pdf.css with small adjustments) to generate a report in pdf
this is my header from the report view calling the css:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="{{public_path('css/pdf.css') }}" rel="stylesheet" type="text/css" />
</head>
<body>
my controller:
public function infor(News $news)
{
...
PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);
$pdf = PDF::loadView('admin.news_events.pdf');
return $pdf->stream('pdfview.pdf');
}
Expected view (html5):
view generated in pdf :,(
How could I solve this?
You have to put all css file in style tag to launch your code
barryvdh/laravel-dompdf does not support boostrap yet.
Quoting the author:
I highly doubt that DomPDF can handle Bootstrap correctly.
If you need better pdf, try: https://github.com/barryvdh/laravel-snappy
That used Webkit to render pdf.
See more about the issue here
I had the same issue, all I did was create another blade file, copied bootstrap.css contents into it (between style tags) , then included it (the file with the css) in the file I wanted to print
Change your file path to this, if CSS and images are not inside the public directory.
<img src="{{ base_path().'/location/to/image/image.png' }}" />
<link rel="stylesheet" type="text/css" href="{{ base_path().'/location/css/bootstrap.min.css' }}">
You should use another version of bootstrap. because i tested it with bootstrap 3 and it's not working but it works with version 4 of the framework.

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?

UTF-8 words not display in laravel-dompdf

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

Categories