Can't import most TTF files to TCPDF - php

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

Related

Arabic Support for tFPDF (based on fpdf)

I am using tFPDF (linked below). I cannot find a way to render text right --> left for Arabic language. It renders fine but it is left to right. Below is the library I am using. I looked at source code and couldn't figure out how to render right to left.
http://www.fpdf.org/en/script/script92.php
Unfortunately it would be too hard to make the tFPDF library work for Arabic. This library only supports Arabic characters in their isolated form, instead of using their initial, medial or final form when they are in an initial, medial or final position in a word. See the document “Text Layout Requirements for the Arabic Script” by the W3C.
Furthermore it does not support true bidirectionality. Have a look at the document “Unicode Bidirectional Algorithm basics”, also by the W3C.
My suggestion is to use another library. The only non-commercial PHP library supporting RTL (right-to-left) scripts that I was able to find is TCPDF. The source code is on GitHub. It's based on the fpdf library just like tFPDF.
To try it out, all you have to do is
git clone https://github.com/tecnickcom/TCPDF.git
cd TCPDF/examples
# this is an example of an RTL script:
php example_018.php > example_018.pdf
open example_018.pdf
Starting from the example files I have put together the following example for Arabic (must be placed in the examples directory):
<?php
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Walter Tross');
$pdf->SetTitle('TCPDF Arabic Example');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'ar'; // 'ar' for Arabic
$lg['w_page'] = 'page';
$pdf->setLanguageArray($lg);
$pdf->SetFont('dejavusans', '', 12); // several fonts in TCPDF/fonts work
$pdf->AddPage();
$txt = <<<EOD
مرحبا يا عالم
hello world
EOD;
$pdf->setRTL(true); // optional here, depends on the desired BASE direction
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0); // 'C' for centered
$pdf->Output('hello_world_in_Arabic.pdf', 'I');
This is what is rendered at the center top of the resulting PDF:
For some reason that I don't have the time to investigate now you have to redirect the output to a file, like with example_018.pdf above, but I'm sure this can be solved. (A quick hack would be to use output buffering.)
Instead of including tcpdf_include.php, which in turn includes all that is needed and much more, you will have to find a reasonable place for what you really need and a reasonable way to include only that.
And, of course, you will have to study a bit the examples in order to put together the code that works for you.
P.S.: I have taken the translation of "hello world" from the Wikipedia entry for the قلب programming language.
use this package to reshape your arabic string
import arabic_reshaper # pip install arabic_reshaper
expression = r"^[a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z]"
if (re.search(expression, yourstring) is None):
pdf.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
pdf.set_font('DejaVu', '', 10)
arabic_string = arabic_reshaper.reshape(yourstring)
arabic_string = arabic_string[::-1]
w = pdf.get_string_width(arabic_string) + 6
pdf.cell(w, 9, arabic_string, 0, 1, 'L', 0)

mpdf can't add new font-family helvetica-neue-condensed-black

I'm using mPdf version 6 and want to add new custom font in it but it's not using new font family. What I did is:
mpdf\ttfonts folder
in config_fonts.php file added following code in fontdata:
"helvetica" => array(
'R' => "helvetica-neue-condensed-black-59233f88d5395.ttf",
),
where "helvetica-neue-condensed-black-59233f88d5395.ttf" is the name of the file.
Following is my mPdf code for creating pdf:
$mpdf = new \Mpdf(['mode' => 'c']);
$mpdf->SetFont('helvetica');
$mpdf->SetTitle('PDF title');
$mpdf->AddPage('P','','','','',8,8,5,5,10,10);
$mpdf->WriteHTML($this->html);
$mpdf->Output('test.pdf', 'd');
I checked whatever I write in SetFont nothing in Pdf changed..
As from the mpdf docs
Core non-embedded fontsPermalink
PDF files have certain standard fonts: Helvetica, Times and Courier in
the win-1252 character set, and Zapfdingbats and Symbol character
sets. These fonts should be available to any PDF reading program, and
do not need to be embedded in the PDF document.
Advantages: Small file size, fast processing, small memory usage.
Disadvantages: Limited choice of fonts for appearance. Will not
display characters which are not in the win-1252 Symbols, or Dingbats
codepages (suitable for most Western European languages).
To use core fonts only, use 'c' for the mode configuration key:
$mpdf = new \Mpdf\Mpdf(['mode' => 'c']);
Change the mode from c to something else.

Imagick annotateImage not showing correctly japanese text

I'm trying to print japanese text to the image.
My code:
$text = // some japanese text
$imagick = new IMagick();
// $imagick implementation
$imagickDraw = new ImagickDraw();
$imagickDraw->setFontSize(12);
$textFontMetrics = $imagick->queryFontMetrics($imagickDraw, $text);
$imagick->annotateImage($imagickDraw, ($imageWidth - $textFontMetrics['textWidth']) / 2, $imageHeight * 0.5, 0, $text);
// save imageBlob
When i check my generated image, instead of normal japanese text i just see '??'. Any ideas how to solve this problem?
Pretty sure this is a font related issue. Make sure you have a font capable of displaying Japanese characters, copy that font to your script's directory, and add the following:
$draw->setFont('fonts-japanese-gothic.ttf');
Where fonts-japanese-gothic.ttf is the name of your font. I tested it out on my local machine and that did the trick.

How to fix that adding a custom font in tcpdf results in dotted output?

I want to add a custom font. I converted an otf file to ttf, and load them via:
$std = \TCPDF_FONTS::addTTFfont($frutigerStd, 'TrueTypeUnicode', '', 96);
These command seem to do something as these values are set, as $std will have the value frutigerltstdcn.
And use set them, in my extended TCDP class via:
$this->SetFont($std);
Yet once I open my generated pdf, Adobe Reader will declare:
Cannot extract the embedded font 'AAAAAC+FrutigerLTStd-Cn'.
Some characters may not display or print correctly.
And true enough, the result is a dotted mess:
What am I missing or doing wrong?
It turned out that my font was erroneous. It was generated by converting an otf font to ttf, and even though I can use that generated font within MacOS, it has issues with TCDP. Once I got the font as an actual true type file, the issue was resolved.

Is possible to embed fontawesome font in fpdf?

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

Categories