html2pdf AddFont Roboto does not work - php

Hy there, I'm using html2pdf v3 to create a PDF.
That works pretty good. But now I wanted to use Google Fonts inside the PDF. So I downloaded Roboto-Regular.ttf and renamed it to roboto.ttf. Then I converted it to .php and .z (Encoding: cp1252): http://www.fpdf.org/makefont/index.php
Then I copied those two files inside the "fonts" folder and included the font:
$html2pdf = new HTML2PDF('P', 'A4', 'de', true, 'UTF-8', array(30,18,30,18));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->AddFont('roboto', 'normal', 'roboto.php');
$html2pdf->writeHTML($css.$content);
$html2pdf->Output($path.$pdf_name, 'F');
Unfortunately the Roboto Text has some pretty weird widths:
HTML:
<p>This is written with default Font Family</p>
<p style="font-family:roboto">This is written with Roboto</p>
Output:
The same happens when I set Roboto as default font $html2pdf->setDefaultFont('roboto');
Is there any solution for this?

I used now a different encoder: fonts.snm-portal.com Now it works.

Related

PHP ImageMagick - Draw Indic text using font file, not from system fonts

I am trying to draw Devanagari text on image using PHP ImageMagick.
ImageMagick renders text correctly if I use font available in the system, but not when loading a font from a file.
For exmaple if use
$imagick->setFont('Lohit Marathi');// installed font
works correctly but if I use
$imagick->setFont("fonts/lohit_mr.ttf");//load from file
Font does not loads and it renders using default font.
code I am trying is as follows
<?php
$imagick = new Imagick();
$imagick->setFont('ANY_SYSTEM_FONT') // works;
$imagick->setFont('somefont.ttf') // does not work;
?>
Can someone point out anything I am missing?

I Can't Find dompdf_config.inc.php or dompdf_config.custom.inc.php for setting "DOMPDF_UNICODE_ENABLED" true

I use dompdf to save a html page as pdf by php. I use Persian characters in my html page (actually php page) but when i'm trying to save it as pdf, the export just looked like '?????' :( .I have searched all the net and I found a configuration for unicode characters https://github.com/dompdf/dompdf/wiki/UnicodeHowTo#configure-dompdf-for-unicode-support in "dompdf_config.inc.php" or "dompdf_config.custom.inc.php" file. but the problem is I CAN'T FIND such file in all of my dompdf folder and in all of my file system. Please somebody tell me where it is or what I must do. something else is that I have to use dompdf because of its fantastic CSS compatibility. Thanks.
This is Export.
http://i.stack.imgur.com/nYAzW.png
This is my code.
require("dompdf/autoload.inc.php");
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml("<html><head>
<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">
<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/scorestyle.css\">
</head>
<body><div>
<div id=\"showscorediv\"><table><tbody><tr class=\"scoresubject\"><th colspan=\"2\">کارنامه ارزیابی</th></tr><tr class=\"scorecategory\"><th>شاخص ها</th><th>امیتاز ها</th></tr><tr><td><span class=\"level_0\">شاخص های کیفی</span></td><td class=\"scoretd\">62</td></tr><tr><td><span class=\"level_1\">شاخص های مرتبط با تیم کاری</span></td><td class=\"scoretd\">10</td></tr><tr><td><span class=\"level_1\">شاخص های مرتبط با محصول</span></td><td class=\"scoretd\">28</td></tr><tr><td><span class=\"level_1\">شاخص های مرتبط با بازار</span></td><td class=\"scoretd\">24</td></tr><tr><td><span class=\"level_0\">شاخص های کمی</span></td><td class=\"scoretd\">60</td></tr><tr><td><span class=\"level_1\">شاخص های تولیدی</span></td><td class=\"scoretd\">20</td></tr><tr><td><span class=\"level_1\">شاخص های درآمدی</span></td><td class=\"scoretd\">14</td></tr><tr><td><span class=\"level_1\">شاخص های هزینه ای</span></td><td class=\"scoretd\">26</td></tr><tr class=\"scoresubject\"><th>امتیاز کل</th><th>122</th></tr></tbody></table>
</div>
</div></body></html>");
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();
You appear to be using dompdf 0.7.0, which no longer uses the dompdf_config.inc.php configuration file. Unicode support is always enabled with this release.
To display the characters used in your sample code you need to ensure that:
You are supplying a font that supports these characters (that appears to be the case).
That dompdf has both read and write capability to the temporary directory, font directory, and font cache directory. You can set these using $dompdf->set_option('option', 'value'); (where option would be tempDir, fontDir, or fontCache).
The font is accessible to dompdf and is in TTF format.
You are correctly styling your content to use your font. Hard to know without seeing your CSS.
FYI, you can't just drop the TTF/UFM in your font directory. dompdf has to record information about the font in order to use it. Also, the AFM metrics file won't work in this case anyway because that indicates Windows ANSI encoding on the font. Windows ANSI encoding does not support the characters in your sample. Dompdf uses the UFM metrics format for Unicode support.
The utilities that were included with previous versions of dompdf are no longer included with 0.7.0. So long as you meet the requirements of using the #font-face declaration you don't need any external utilities. If needed, however, you can find a compatible version of the load_font.php script in the dompdf-utils project.
Since you are using 0.7.0 (which was just released) a lot of info on the Internet may be out of date, so you may want to read up on how to use it:
dompdf README
dompdf 0.7.0 release notes
dompdf wiki (pay attention to any versioning information)
Lastly, dompdf includes a font (DejaVu) that can support your characters. Try adding the following to your stylesheet so that you have a fallback in case your custom font doesn't work:
* { font-family: BZar_0, DejaVu Sans, sans-serif; }
I just added dompdf_config.inc.php and dompdf_config.custom.inc.php files from dompdf 0.6.2 version, besides include dir with autoload.inc.php and functions.inc.php to my dompdf 0.8.* and it works.
To set any DOMPDF option you have to define a named constant.
All available options can be found here: http://pxd.me/dompdf/www/setup.php
To enable Persian characters you basically have to enable unicode support.
define("DOMPDF_UNICODE_ENABLED", true);
You may want to set DOMPDF_FONT_DIR and DOMPDF_DEFAULT_FONT as well, just to make sure that the font you're using supports unicode characters.

How to add TTF font to html2pdf PHP program

I'm trying to use HTML2PDF 4.03 with this code:
<?php
$content = "..."; # my HTML code
require_once(dirname(__FILE__).'/html2pdf_v4.03/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','en', true, 'utf-8', array(15,20,15,20) );
# here I'm trying to add my arial.ttf
$html2pdf->pdf->AddTTFFont('arial.ttf');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
?>
Now the program die with this:
PHP Fatal error: Call to undefined method HTML2PDF_myPdf::AddTTFFont()
How can I add TTF font to my PDF file?
I have managed to add 1 custom font to my setup using the following method.
First convert the .ttf file to 3 separate files (.php .z and .ufm) using the following
font converter
Place the 3 files that are generated by this system into the fonts folder in TCPDF.
Now you can set the default font for your PDF using the following command
$html2pdf->setDefaultFont("the_name_you_called_your_font");
This was fairly simple to get working, I am having issues using 2 seperate fonts though via this method. I'll figure it out though
To expand on the selected answer (by o11y_75) when you convert your fonts, you need to use a specific name to include also the bold and italic variants.
That way, you only add one font definition like this
$html2pdf->AddFont('opensans', 'normal', 'opensans.php');
$html2pdf->setDefaultFont('opensans');
When you convert the fonts, name them, for example, like these:
default: opensans
bold: opensansb
italic: opensansi
bold italic: opensansbi
notice that behind the original name, you add b, i and bi on each case.
I have found no documentation on this issue, but I followed the nomenclature found on the fonts that already came with TCPDF and it worked.
If you want to add multiple fonts, just use :
$html2pdf->addFont('opensansregular', '', 'opensansregular');
$html2pdf->addFont('opensansbold', '', 'opensansbold');
I would suggest that you don't use special chars with the font converter specified above.
Then in your CSS simply type :
<style type="text/css">
<!--
.uppercase {
text-transform: uppercase;
}
* {
font-family: opensansregular;
}
h1, h2, h3, strong {
font-family: opensansbold;
}
-->
</style>
HTML2PDF works internally with TCPDF.
TCPDF has its own object since version 6.2.6 to create the fonts needed for HTML2PDF: TCPDF_FONTS
I have solved this as follows:
I searched for TCPDF in the vendor directory and found the Fonts directory there.
Then I created my own separate PDF script and used it once to create the necessary font files from the ttf.
usage: $fontname = TCPDF_FONTS::addTTFfont('vendor/tecnickcom/tcpdf/fonts/arialuni/arialuni.ttf', 'TrueTypeUnicode', '', 32);
For more details see: https://stackoverflow.com/a/70337995/2320007

binding font files with PDF document in TCPDF

Im scripting to generate a PDF file using TCPDF library, all I want now is to add font files into the document, I could add them using "SetFont" like
$pdf->SetFont('myfont', '', 10);
and its when I execute this script, the document in browser is rendering fonts perfectly, if I save it and open in another machine then fonts are working with, default fallback font is applying.
I did check it by going to File->Properties->Fonts in Adobe reader, font file is listed there, but text is missing the font.
Any idea will be helpful.
You will probably want to embed the font using $pdf->AddFont().
A longer description of the differences between SetFont(), AddFont() and AddTTFFont() here:
TCPDF, "Could not include font definition file" with OpenType fonts

Making text bold or larger in dompdf

I just started using dompdf v0.6.0beta3 and the dompdf Codeigniter helper with Codeigniter. I have copied the Arial font family tff files from Windows 7 font folder to dompdf's /lib/fonts folder.
Problem: When I select a text using CSS and apply the font-weight: bold property, on the HTML output it is indeed bold, but after converting to PDF via dompdf, the text is no longer in bold!
font-size: 24px does not work, all text in the pdf are the same sizes. And the only font being used in the pdf appears to be Times New Roman!
How can I make my text bold and change its size and font in the pdf?
PHP (Controller)
function pdf() {
$this->load->model('resume_model');
$results = $this->resume_model->get_resume_details($user_id);
$this->load->helper(array('dompdf', 'file'));
$html = $this->load->view('resume_pdf', $results, true);
pdf_create($html, 'filename');
}
Is your stylesheet external to your HTML content? If so you may just have a path problem. The plugin appears to use $dompdf->load_html() to load the document. DOMPDF has no knowledge of your website when used in this way and will work off the local file system. What this means for you is that file paths are relative to the currently-executing file. If the path is absolute (e.g. /css/main.css) then DOMPDF will look for this file off the root of the file system. Instead of looking for the file at /wwwroot/content/css/main.css it will look for /css/main.css.
The quickest fix, if this is your problem, would be to add a full URL, including domain, to your file references (e.g. http://example.com/css/main.css).

Categories