Use custom font face in DomPDF - php

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

Related

Passing blade layout to phpword

I'm using Laravel with phpword. So here i want to export html layout from blade file to word.
But when i trying to create it, the error that appearing :
DOMDocument::loadXML(): Opening and ending tag mismatch: link line 1 and head in Entity, line: 1
My code:
$content = view('docs.index')->render();
$dom = new DOMDocument();
$dom->loadHTML($content);
$dom->saveHTML();
$phpWord = new PhpWord();
Html::addHtml($phpWord->addSection(), $dom->saveHTML(), true);
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('doc_index_'.Carbon::now()->format('d-m-y h-i').'.docx');
return view('papers.show')->with('success');
Here is my blade file :
<!DOCTYPE html>
<html lang="en">
<head>
<title>Doc HTML</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css" />
</head>
<body>
<div>
<p>
Laravel is a web application framework with expressive, elegant syntax. <br />
We believe development must be an enjoyable and creative experience to be truly fulfilling. <br />
Laravel takes the pain out of development by easing common tasks used in many web projects. <br />
</p>
</div>
</body>
</html>
Printing my $content variable :
"""
<!DOCTYPE html>
<html lang="en">
<head>
<title>Doc HTML</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css" />
</head>
<body>
<div>
<p>
Laravel is a web application framework with expressive, elegant syntax. <br />
We believe development must be an enjoyable and creative experience to be truly fulfilling. <br />
Laravel takes the pain out of development by easing common tasks used in many web projects. <br />
</p>
</div>
</body>
</html>
"""
I was facing exact same issue and got fixed with below work arou
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=".rand().".doc");
header("Pragma: no-cache"); header("Expires: 0");
and then simply render your view.
E.g return view('view_name');
Your <meta> tag is technically not closed. To follow the spec for elements that ommit a closing tag, they must be suffixed with />. So just how you have <br /> and <link ... /> your meta tags should be <meta ... />.
The <!DOCTYPE html> part in the beginning of your template is causing that error. Regarding reading the style files, from the link to the function details provided there above in discussion PHPWord/Html.php you can see that addHtml will just use the contents of the body tag and ignore everything else.
Regarding the other question in the above discussion, I don't think that PHPWord supports any kind of style inclusion from css files - in this particular example, I'm not sure what would be the expected result in applying bootstrap responsive styling to word document content.

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

Html to pdf issue in html2pdf.it library

i am currently using html2pdf library in php for converting html page to pdf, it create pdf properly but google label in non clickable mode
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
Google
Google
</body>
</html>

Not able to generate pdf with logo

I'm not able to get my PDF printed if I add a logo in my template_header.php file.
If I enable include_once("template_header.php") it's not generating any PDF, and it reports this error message:
PHP Fatal error: Call to undefined function imagecreatetruecolor()
If I disable my header (i.e. above the include_once line) it's generating a PDF without a logo and containing text only.
I tried enabling below, but the same problem persists:
define("DOMPDF_ENABLE_REMOTE", true);
My dompdf ver. is dompdf_0-6-0_beta3.
Can anyone help me on this?
Below is the updated code
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$html = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>booking</title>
<link rel="stylesheet" href="style/style.css" type="text\css" media="screen" />
</head>
<body>
<div align="center" id="mainwrap">
<?php include_once("template_header.php")?>
</div>
</body>
</html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
Please check your GD library installation in php.
Run <?php phpinfo(); ?> in a php file and check for GD library then
Update -
Check your php.ini and look for extension=php_gd2.dll
If ; [commented] then un comment it and restart the services then.

Categories