Custom Fonts for DOMPDF - php

I'm Using DOM PDF 0.6.0 Beta 2. I want to use custom fonts (Fonts: 'Segeo Print', 'Lucida Handwriting','Airplanes in the Night Sky') in PDF file.
I followed the guidelines to install and use fonts in my PHP Code, which is given here http://code.google.com/p/dompdf/wiki/CPDFUnicode
But I'm not able to get desire fonts in my PDF. You can find my code in this post. Please Let me know how I can resolve this issue.
<?php
require_once("dompdf_config.inc.php");
$html = "<html>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<style>
*{font-size:15px;}
div.ClJ{font: nightsky;}
</style>
</head>
<body>
<div class='ClJ'>This text is in DIV Element</div><br /><br />
</body>
</html>";
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
?>

go to your DOMPDF folder
copy your font's as .ttf (TrueType Font) or .otf (OpenType Font) into the DOMPDF's root
open your command line and run php load_font.php your_fonts_name ./your-normal.ttf ./your-bold.ttf ./your-bold-italic.ttf
DOMPDF now created Adobe Font Metrics and copied it to lib/fonts/* - you can now use it with font-family: your_fonts_name;

you can add css font :
#font-face {
font-family: new_font;
src: url('my_font.ttf');
}
and than
div.ClJ{
font-family: new_font;
}

If you want to install custom fonts to the server without a command-based interface
then you can do the following web-based model to install the custom fonts
Download ejaz.php from here
Put this file to the root of dompdf and follow the instruction written in it
no need for root or command/terminal access

Related

Create pdf which content html and gujarati text in Laravel

I have try to create pdf in Laravel.
Usebarryvdh/laravel-dompdf vendor package class.
My pdf content English and Gujarati text.
but gujarati content print as ?????? in pdf.
I have create pdf succesfully use this type of code
pdfview is my pdf php, html content file view
view()->share('data',$data);
$pdf = PDF::loadView('pdfview');
return $pdf->stream('pdfview.pdf');
// return $pdf->download('pdfview.pdf');
I have expected to pdf content as English and Gujarati text content.
Actual result in pdf Gujarati content print like ????
use wkhtmltopdf, make sure to install all unicode library related to Gujarati language. I tried it for sinhala in ubuntu and centos.
"????" is because the font in not there by default in barryvdh/laravel-dompdf package.
1.You need to download the fonts that supports gujrati.
2.After downloading, just put the the .ttf file to your font assets folder.
3.Then use the css #font-face to explicitly declare your font supporting the gujrati characters.
Just for example (using simhei.ttf). FYI, this is in my template.blade.php file
<style type="text/css">
#font-face {
font-family: SimHei;
src: url('{{base_path().'/public/report_assets/'}}fonts/simhei.ttf') format('truetype');
}
* {
font-family: SimHei;
}
</style>
<body>
//gujrati characters
.......
Thats it

#font-face or custom font does not work in PHP pdf generator class

I am using dompdf to generate PDFs from inputs.
The code I use:
require_once (STYLESHEETPATH.'/dompdf/include/autoload.inc.php');
set_include_path(STYLESHEETPATH.'/dompdf');
require_once STYLESHEETPATH.'/dompdf/dompdf_config.inc.php';
$dompdf = new DOMPDF();
$html = '
<html>
<body>
<h1>Hello <i>'. $fullname.'</i> </h1>
<p> your father is <b>'.$fathername.'</b>
</body>
</html>';
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("tck.pdf"); ?>
This works very fine and generates a pdf called tck.pdf
Now I want to style the fonts in the pdf so I've tried
require_once (STYLESHEETPATH.'/dompdf/include/autoload.inc.php');
set_include_path(STYLESHEETPATH.'/dompdf');
require_once STYLESHEETPATH.'/dompdf/dompdf_config.inc.php';
$dompdf = new DOMPDF();
$html = '
<html>
<head>
<style>
#font-face {
font-family: "Open Sans";
font-style: normal;
font-weight: 400;
src: url(http://themes.googleusercontent.com/static/fonts/opensans/v8/cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf) format("truetype");
}
</style>
</head>
<body>
<h1>Hello <i>'. $fullname.'</i> </h1>
<p> your father is <b>'.$fathername.'</b>
<br/>
<span style="font-family:webfontregular;">bar code will be here</span>
<div style="font-family:"Open Sans";">Open Sans</div>
</body>
</html>';
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("tck.pdf"); ?>
But it does not work and giving an error as follow :
Fatal error: Class 'Font' not found in /home4/q0i4l0g2/public_html/tck/wp-content/themes/kallyas-child/dompdf/include/font_metrics.cls.php on line 356
I've noticed that whenever I use "#font-face" I am getting this error.
Why is that?
How can I make it work?
As per the accepted answer in this question Dompdf and set different font-family this should work I think but it does not.
When using dompdf versions prior to 0.7.0 you should not include the autoloader directly. This is done as part of the setup process performed when you include the dompdf_config.inc.php file. That setup process defines a number of constants used by dompdf as well references the autloaders for dompdf and the various libraries is uses (including php-font-lib).
If the dompdf class is already available the startup script will exit without doing anything.
Also potentially relevant is how you got dompdf. If you did not download a packaged release (i.e. you downloaded the various libraries separately) make sure you grab php-font-lib 0.3.x, the version used by the dompdf 0.6 releases.

DOMPDF Chinese Character

I have problem that when I download the PDF it will download the font together in the PDF which will create a very big size of PDF (about 10mb+).
<?php
include "/dompdf_config.inc.php";
$html =
'<html><body><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
*{ font-family: simsun; }
</style>
<p>开发人员指南</p>
</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("hello.pdf"); ?>
You'll want to enable font subsetting.
In dompdf 0.6.2 or earlier set the DOMPDF_ENABLE_FONTSUBSETTING configuration constant to true. Configuration constants can be set in dompdf/dompdf_config.custom.inc.php.
In dompdf 0.7.0 or later after you instantiate dompdf you can configure this option using the set_option method: $dompdf->set_option('isFontSubsettingEnabled', true);.
follow the procedure to use chinless language support in laravel latest version
clone https://github.com/dompdf/utils
download font from i use simsun https://www.wfonts.com/font/simsun
3.1.keep those file in project root folder
3.2 add folder name font in storage folder
Run php load_font.php "Simsun" Simsun.ttf from your comment pomp
5.then use in html body { font-family: 'Simsun'; } for chines language
6.i attach image so that it will help you

CodeIgniter: how to pass css Bootstrap using dompdf

I am trying to generate a PDF file using codeIgniter and dompdf.
The script below creates the PDF file, but the css are not shown. The script below generates only the HTML structure.
public function create_pdf() {
$this->load->view('invoice');
$html = $this->output->get_output();
$this->load->library('dompdf_gen');
//Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$dompdf = new DOMPDF();
$this->dompdf->stream("welcome.pdf",array('Attachment'=>0));
}
How can I include following css in the script?
<link href="<?php echo base_url('assets/css/bootstrap.css');?>" rel="stylesheet" />
<link href="<?php echo base_url('assets/css/style.css');?>" rel="stylesheet"/ >
I use CodeIgniter and dompdf together and I include my CSS file by simply adding a link tag into the head of the view of I am printing i.e.
<link rel="stylesheet" type="text/css" href="<sitename>/full/path/to/print.css">
I do only link a basic CSS file in though so I would try a basic CSS file to start with (a file that just puts all the text in bold or something) and then if that works gradually add more rules as the issue could be your CSS files contain something which dompdf cannot handle.
You must have figured out this issue since it was asked X years ago.
But I found the solution via this github issue.
Maybe, someone who's looking for the issue, this might be useful.
Tried & Tested by me. 💹
Worked for me. 💹
Get the Instances of dompdf to your controller as
$dompdf = new Dompdf\Dompdf(['isRemoteEnabled' => true]);

I am trying to create malayalam pdfs using FPDF. font using meera.ttf It is working but is not working

I am trying to create malayalam pdfs using FPDF.
Font using meera.ttf
It is working but is not working correctly.
Result is like this ഇഗലീഷ്
I want the result ഇംഗ്ലീഷ്
codes***
$pdf = new tFPDF();
$pdf->AddPage();
// Add a Unicode font (uses UTF-8)
$pdf->AddFont('meera','','meera.ttf',true);
$pdf->SetFont('meera','',14);
// Load a UTF-8 string from a file and print it
$txt = file_get_contents('HelloWorld.txt');
$pdf->Write(8,$txt);
// Select a standard font (uses windows-1252)
$pdf->SetFont('meera','',14);
$pdf->Ln(10);
$pdf->Write(5,'The file size of this PDF is only 12 KB.');
$pdf->Output();
?>
HelloWorld.txt
ഇംഗ്ലീഷ്
i got answer, use mpdf,
fpdf and tcpdf not support.
go to
https://mail.google.com/mail/u/0/?shva=1#sent/143096e2010c4a58
download ful version of mPDF v5.7 Full installation - Download (.ZIP file 12.8MB)
run example /example29_multilingual_autofont.php
eg. localhost/MPDF57/examples/example29_multilingual_autofont.php
I found the Solution.
Just Download mpdf 6.0 and use the following Query
<?php
include('mpdf.php');
$html = '
<html>
<head>
<style>
body {background-image:url(image.jpg); background-image-resize:6;}
footer {page-break-after: always;}
</style>
</head>
<body>
<p >ഇംഗ്ലീഷ്</p>
<p style="font-family:Courier New">Malayalam</p>
</body>
</html>';
$mpdf=new mPDF('ml');
$mpdf->WriteHTML($html);
$mpdf->Output();
?>

Categories