Is possible to embed fontawesome font in fpdf? - php

I'd like to use fontawesome in pdf. I generate my pdf using php library fpdf and font embedding. However I cannot make it works.
I use this tool to generate afm file: http://fpdf.fruit-lab.de/
But when I try to use fontawesome I always get white square instead of icons.
I use this syntax to add icon:
MultiCell(0,8,"\uF000 ",0,'C')

I cannot answer for fpdf since I have never used it. However, I do use mPDF and there I use fontawesome regularly - no issues at all. The only thing I have to ensure is that the content I output to the PDF document (mPDF takes this in the form of HTML markup) hast to be UTF8 encoded.
mPDF is very good so if you are at an early stage of your project you might just consider switching to it. Otherwise, it is worth exploring whether you too are not running into a UTF8 encoding issue.

I figured this out even though this thread is over a year old. It is possible to use font-awesome although you can only use up to 256 characters per subset. I created several character maps to encompass all of the glyphs as of font awesome version 4.3.0. You just use the map that contains the characters you're going to use or you can make three subsets of it. It's not necessarily as performant as other solutions, but fPDF is still much faster than some of the alternatives because it is lacking a lot of the more modern features like unicode support.
http://code.deweyartdesign.com/fpdf/makefont/fa1.map
http://code.deweyartdesign.com/fpdf/makefont/fa2.map
http://code.deweyartdesign.com/fpdf/makefont/fa3.map
First, you need to use ttf2pt1 to create the afm file for the font.
ttf2pt1 -a fontawesome-webfont.ttf fontawesome-webfont
I made three copies of the webfont to run through makefont.php and used each encoding on the corresponding font.
require "makefont.php";
makefont('fontawesome-webfont1.ttf','fa1.map');
makefont('fontawesome-webfont2.ttf','fa2.map');
makefont('fontawesome-webfont3.ttf','fa3.map');
To use them in fPDF, put the files generated in the font folder and add them like this:
$pdf->AddFont('FA1','',fontawesome-webfont1.php);
$pdf->AddFont('FA2','',fontawesome-webfont2.php);
$pdf->AddFont('FA3','',fontawesome-webfont3.php);
Then you use the character number to render the glyph for the corresponding subset of font-awesome. The three font maps above contain the character numbers to use :
$pdf->SetFont('FA1','',14);
$WineGlass = chr(32);
$pdf->Cell(40,10,$WineGlass);
I also made a character map generator that will show the character numbers below the glyphs.
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/pdf/fpdf.php');
// Establish / Get variables
function GETVAR($key, $default = null, $prefix = null, $suffix = null) {
return isset($_GET[$key]) ? $prefix . $_GET[$key] . $suffix : $prefix . $default . $suffix;
}
$font = GETVAR('font','fontawesome-webfont1','','.php');
$pdf = new FPDF('L','mm',array(268.33,415.3));
$pdf->AddPage();
$pdf->SetMargins(0,0,0);
$pdf->SetAutoPageBreak(0,0);
// add custom fonts
$pdf->AddFont('H','','helvetica.php');
$pdf->AddFont('FA','',$font);
$pdf->SetFillColor(200,200,200);
$pdf->SetXY(9,9);
for ($i = 32; $i <= 256; $i++) {
$y = $pdf->GetY();
$x = $pdf->GetX();
$pdf->SetX($x);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('FA','',14);
$pdf->Cell(12,12,chr($i),1,0,'C');
$pdf->SetXY($x,$y+12);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('H','',14);
$pdf->Cell(12,12,$i,1,0,'C',1);
$y = $pdf->GetY();
$x = $pdf->GetX();
$pdf->SetXY($x,$y-12);
if ($x > 400) {
$pdf->SetXY(9,$y+14);
}
if ($i == 328){
$pdf->AddPage();
}
}
$pdf->Output("charmap.pdf",'I');

You can re-create the font-awesome and re-map it to A-Z characters,
and that way, use it easily in fpdf.
// Import and prepare font for usage by engine:
$pdf->AddFont('fontawesome-123-full','','fontawesome-123-full.php');
...
// print FontAwesome's asterisk
$pdf->Text($x, $y, chr(0x21));
You can download the ready font and ready-to-use FPDF font files here:
http://mdb-blog.blogspot.com/2021/12/using-fontawesome-in-php-fpdf.html

Related

How to use PDF_get_option() method to get font name?

function nvpdf_WrapString($my_pdf,$string,$width,$width_in_chars = "") {
// known problem: can only be called after the pdf document has been
// started, font size set, etc as it relies on the pdf being in the
// page scope to use pdf_stringwidth (see PDFLib manual p.83).
// returns an array: 1st level column
// 2nd level data string broken up into lines that fit the column
$string = str_replace('\n', "\n", $string);
$inlines = explode("\n",$string);
$outlines = array();
$font_size = PDF_get_option($my_pdf, "fontsize", "");
$font_name = pdf_get_parameter($my_pdf, "fontname", 0);
$font_id = PDF_load_font($my_pdf, $font_name, "host","");
I got a deprecation warning from php saying that I should use pdf_get_option() instead of pdf_get_parameter(). But how can I convert this pdf_get_parameter() to pdf_get_option()?
I need a method that will return me the font name of $mypdf.
Found the solve. pdf_get_option() wont give me the font name. All I have to do is use pdf_info_font() and then use pdf_get_string() to get the fontname.
nice that you could find the solution yourself.
Another quick note: If you have found a deprecated warning for a PDFlib function, it is recommended to replace it even if the functionality still works in the version you are using. This is because the deprecated function may no longer be available when updating.
The current PDFlib 9 and 10 packages include a PDFlib Migration Guide which describes all necessary changes in detail.
You can also find the latest PDFlib 10 Migration Guide on the PDFlib Web site pdflib.com.
for this particular question you would find the explanation in chapter 2.1 table 2.3:
deprecated parameter:
ascender, ascenderfaked, capheight, capheightfaked, descende,
descenderfaked, fontencoding, fontname,fontmaxcode, xheight, xheightfaked
deprecated since:
PDFlib 7
replacement method and option:
PDF_info_font( ) with same-named keywords; for the numerical
values fontsize=1 must be supplied;
parameter fontencoding: use keyword encoding
A sample usage is also available in the PDFlib cookbook "font/font_info".
/* Get the font name: use "api" to get the font name used by PDFlib.
* (use "acrobat" for the Acrobat font name or use "full" for
* retrieving the full font name)
*/
$p->fit_textline("fontname (api):", $x, $y-=$yoffset, "");
$info = $p->info_font($font, "fontname", "api");
if ($info > -1) {
$fontname = $p->get_string($info, "");
$p->fit_textline($fontname, $xindent, $y, "");
}

Can't import most TTF files to TCPDF

I have read all the tutorials there are to read about this topic, I simply can't make this work, neither do the example codes and online tools...
When I try to import most .ttf files into TCPDF fonts, the resulting php file goes something like this:
<?php
$type='TrueType';
$name='FontAwesome5Free-Solid';
$desc=array('Ascent'=>875,'Descent'=>-125,'CapHeight'=>875,'Flags'=>96,'FontBBox'=>'[5000 5000 -5000 -5000]','ItalicAngle'=>-50,'StemV'=>70,'MissingWidth'=>1000);
$up=-123;
$ut=49;
$dw=1000;
$cw=array(
0=>1000,1=>1000,2=>1000,3=>1000,4=>1000,5=>1000,6=>1000,7=>1000,8=>1000,9=>1000,
10=>1000,11=>1000,12=>1000,13=>1000,14=>1000,15=>1000,16=>1000,17=>1000,18=>1000,19=>1000,
20=>1000,21=>1000,22=>1000,23=>1000,24=>1000,25=>1000,26=>1000,27=>1000,28=>1000,29=>1000,
30=>1000,31=>1000,32=>1000,33=>1000,34=>1000,35=>1000,36=>1000,37=>1000,38=>1000,39=>1000,
40=>1000,41=>1000,42=>1000,43=>1000,44=>1000,45=>1000,46=>1000,47=>1000,48=>1000,49=>1000,
50=>1000,51=>1000,52=>1000,53=>1000,54=>1000,55=>1000,56=>1000,57=>1000,58=>1000,59=>1000,
60=>1000,61=>1000,62=>1000,63=>1000,64=>1000,65=>1000,66=>1000,67=>1000,68=>1000,69=>1000,
70=>1000,71=>1000,72=>1000,73=>1000,74=>1000,75=>1000,76=>1000,77=>1000,78=>1000,79=>1000,
80=>1000,81=>1000,82=>1000,83=>1000,84=>1000,85=>1000,86=>1000,87=>1000,88=>1000,89=>1000,
90=>1000,91=>1000,92=>1000,93=>1000,94=>1000,95=>1000,96=>1000,97=>1000,98=>1000,99=>1000,
100=>1000,101=>1000,102=>1000,103=>1000,104=>1000,105=>1000,106=>1000,107=>1000,108=>1000,109=>1000,
110=>1000,111=>1000,112=>1000,113=>1000,114=>1000,115=>1000,116=>1000,117=>1000,118=>1000,119=>1000,
120=>1000,121=>1000,122=>1000,123=>1000,124=>1000,125=>1000,126=>1000,127=>1000,128=>1000,129=>1000,
130=>1000,131=>1000,132=>1000,133=>1000,134=>1000,135=>1000,136=>1000,137=>1000,138=>1000,139=>1000,
140=>1000,141=>1000,142=>1000,143=>1000,144=>1000,145=>1000,146=>1000,147=>1000,148=>1000,149=>1000,
150=>1000,151=>1000,152=>1000,153=>1000,154=>1000,155=>1000,156=>1000,157=>1000,158=>1000,159=>1000,
160=>1000,161=>1000,162=>1000,163=>1000,164=>1000,165=>1000,166=>1000,167=>1000,168=>1000,169=>1000,
170=>1000,171=>1000,172=>1000,173=>1000,174=>1000,175=>1000,176=>1000,177=>1000,178=>1000,179=>1000,
180=>1000,181=>1000,182=>1000,183=>1000,184=>1000,185=>1000,186=>1000,187=>1000,188=>1000,189=>1000,
190=>1000,191=>1000,192=>1000,193=>1000,194=>1000,195=>1000,196=>1000,197=>1000,198=>1000,199=>1000,
200=>1000,201=>1000,202=>1000,203=>1000,204=>1000,205=>1000,206=>1000,207=>1000,208=>1000,209=>1000,
210=>1000,211=>1000,212=>1000,213=>1000,214=>1000,215=>1000,216=>1000,217=>1000,218=>1000,219=>1000,
220=>1000,221=>1000,222=>1000,223=>1000,224=>1000,225=>1000,226=>1000,227=>1000,228=>1000,229=>1000,
230=>1000,231=>1000,232=>1000,233=>1000,234=>1000,235=>1000,236=>1000,237=>1000,238=>1000,239=>1000,
240=>1000,241=>1000,242=>1000,243=>1000,244=>1000,245=>1000,246=>1000,247=>1000,248=>1000,249=>1000,
250=>1000,251=>1000,252=>1000,253=>1000,254=>1000,255=>1000);
$enc='cp1252';
$diff='';
$file='lol.z';
$originalsize=191836;
// --- EOF ---
Look that all characters are invalid, basically.
This is the result I get using my PHP source, or using online converters made for TCPDF (eg.: http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf or http://fonts.snm-portal.com/).
I am trying to add font-awesome's TTF files (eg. fa-solid-900.ttf) to my TCPDF, but I get this error.
I have no idea what to do now.
You need to import FontAwesome as a Unicode font. [Also noting: Characters 0-255 will have character widths of 1000 in your example because they don't exist in this font and 1000 is the width defined for your missing glyph width.] For your other fonts that are failing, try importing them as Unicode fonts as well or with a different encoding table.
//Ignore the warnings about undefined indexes on first import.
//There's no H or x glyphs in the font, so the warning is expected.
$fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf', 'TrueTypeUnicode', '', 4);
//Note: using empty string for $enc since this is a Unicode font
//Note: using 4 for $flags to signify a symbol font. 32 for text fonts.
$pdf->setFont($fa);
$pdf->writeHTMLCell(100,120,40,10,"");
//An example inlined with normal text:
$pdf->setFont('helvetica');
$pdf->writeHTMLCell(100,120,40,20,"Should be a gamepad: <span style=\"font-family:$fa;\"></span>");
Attached screenshot of test output results:
Test script:
<?php
//Update to your TCPDF path
include 'TCPDF/tcpdf.php';
$pdf = new TCPDF('P', 'mm','Letter', true, 'UTF-8', false, true);
$pdf->addPage();
$pdf->setFontSubsetting(true);
$fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf', 'TrueTypeUnicode', '', 4);
$testtext = "Font name: {$fa}<br>".
"Should be a gamepad: <span style=\"font-family:$fa;\"></span>";
$pdf->writeHTMLCell(100,10,10,10,$testtext);
$pdf->Output(dirname(__FILE__).'/fasolid-test.pdf', 'F');

Unicode font broken when merge images & text using php

I tried to merge text with images, but my text not rendering correctly after merge.
See my screenshot:
The Red mark ( আমি তোমায় ভালবাসি ) text format is correct while I normally show the variable. Font Link: http://www.fonts2u.com/solaimanlipi.font
But while I merge text with image, its broken or not render correctly.
Here is my full code:
<?php
mb_language('uni');
mb_internal_encoding('UTF-8');
$username="";
if(isset($_POST['user'])){
$username = $_POST['user'];
$username = mb_convert_encoding($username, 'HTML-ENTITIES',"UTF-8");
$username = html_entity_decode($username,ENT_NOQUOTES, "ISO-8859-1");
}
$im = imagecreatefromjpeg('image.jpg');
//The numbers are the RGB values of the color you want to use
$black = ImageColorAllocate($im, 255, 255, 255);
//The canvas's (0,0) position is the upper left corner
//So this is how far down and to the right the text should start
$start_x = 50;
$start_y = 80;
$font = 'SolaimanLipi.ttf';
Imagettftext($im, 50, 0, $start_x, $start_y, $black, $font, "$username");
//Creates the jpeg image and sends it to the browser
//100 is the jpeg quality percentage
Imagejpeg($im, 'result.jpg', 100);
echo "<div style='color:red;font-size:60px;'>".$username."</font><br /><img src='result.jpg' height='500' />"
?>
Note 1: SolaimanLipi.ttf is Bangla (Unicode) Font.
Note 2: Bangla (Unicode) font not rendering correctly in tcpdf This Stack & Stack solution will help you to understand my problem & give me a solution.
Please someone help me to get out this problem. Thanks in advance.
[[Previously I asked the same question and no one answer. So I re-post this and deleted the old one. I hope this time someone help me.]]
Bengali is one of a number of scripts that cannot be rendered simply by putting one character after the next. The shapes of characters change and merge depending on their neighbours.
To render this correctly, you need a set of features known as Complex Text Layout. On text rendering systems that do not support CTL, you'll get the equivalent of each letter being rendered alone, so আমি comes out like আ ম ি.
The GD library used by PHP's imagettftext function does not support Complex Text Layout. You'd have to use some higher-level rendering library such as Pango. This is a bit of a pain; if you can achieve your aims by rendering the text over the image as part of HTML/CSS/SVG that would certainly be easier.
(BTW writing $username out to the HTML without doing htmlspecialchars() is an XSS security hole, and writing-then-serving the JPEG file is unsafe as it will break if two requests come in at the same time. You should also serve your page/form as UTF-8 by adding <meta charset="utf-8"/> so you don't have to do the crazy dance with mb_convert_encoding/html_entity_decode).

Is there a way to tell whether a font supports a given character in Imagick?

I'm using Imagick to generate simple logos, which are just text on a background.
I'm usually looping through all available fonts, to present the user with a choice of different renderings for every font (one image per font).
The problem is, some fonts don't support the ASCII characters (I think they've been designed for a given language only). And I guess that some of the fonts which support ASCII characters, will fail with non-ASCII characters as well.
Anyway, I end up with images such as these:
Is there a programmatic way in Imagick to tell whether a given font supports all the characters in a given string?
That would help me filter out those fonts which do not support the text the user typed in, and avoid displaying any garbage images such as the ones above.
I don't know a way using imagemagik, but you could use the php-font-parser library from here:
https://github.com/Pomax/PHP-Font-Parser
Specifically, you can parse a font for each letter in your required string and check the return value:
$fonts = array("myfont.ttf");
/**
* For this test, we'll print the header information for the
* loaded font, and try to find the letter "g".
*/
$letter = "g";
$json = false;
while($json === false && count($fonts)>0) {
$font = new OTTTFont(array_pop($fonts));
echo "font header data:\n" . $font->toString() . "\n";
$data = $font->get_glyph($letter);
if($data!==false) {
$json = $data->toJSON(); }}
if($json===false) { die("the letter '$letter' could not be found!"); }
echo "glyph information for '$letter':\n" . $json;
Above code comes from the font parser projects fonttest.php class:
https://github.com/Pomax/PHP-Font-Parser/blob/master/fonttest.php

Zend PDF word wrapping

Does Zend_Pdf already support automatic wrapping of text?
For example I want to create a textblock and fill it with text. This is a very important feature and unless Zend Pdf does not have it I am not able to switch from FPDF.
Also I think a Framework that says it has PDF support should be able to offer their developers this kind of feature.
Or am I missing something here?
Indeed Zend_Pdf is not mature enough to compare with the regular Zend_YouNameIt component.
In my oppinion Zend_Pdf should not be part of Zend. It is very limited and can't be used in most cases because one or the other feature is missing.
As for the feature you are missing, you would have to check the Manual or read the code but in general I advise to use something else for PDF generation until Zend_Pdf is mature enough.
I think the best solution would be for Zend_Pdf to be a wrapper for some kind of command line tool.
couple of years later with Zend 1.11 this is working for me in a pdf class function
/**
* puts text box to a page
*
* #param integer $offset_x
* #param integer $offset_y
*/
public function drawTextArea($text, $pos_x, $pos_y, $height, $length = 0, $offset_x = 0, $offset_y = 0) {
$x = $pos_x + $offset_x;
$y = $pos_y + $offset_y;);
if ($length != 0) {
$text = wordwrap($text, $length, "\n", false);
}
$token = strtok($text, "\n");
while ($token != false) {
$this->page->drawText($token, $x, $y);
$token = strtok("\n");
$y -= $height;
}
}
if someone likes it use it
Zend pdf is not supporting automatic word wrap.
I can recommend you to use dompdf instead. It allows you to define the page in html and render it into a pdf. Wordwrap will be handled fine!
It even allows for page breaks, so multi-page pdfs are possible.

Categories