Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF font not embadding, showing "..." on adobe reader

Tags:

php

fonts

tcpdf

I added many fonts in TCPDF using this line of code

TCPDF_FONTS::addTTFfont('fonts/ArchitectsDaughter.ttf', 'TrueTypeUnicode', '', 96);
$pdf->AddFont("ArchitectsDaughter");

Many other font is working but, this one is not working. When i opening this pdf into reader, it shows error like this

cannot extract the embedded font 'ArchitectsDaughter'. some character may not display or print correctly.

I am importing svg file in pdf. Here is the SVG file which i inserting in pdf, and you can get PDF from here and here is the font file.

Here is full code how pdf will generates.

$fileName='export';
$uploadPath = Config::get('constants.paths.uploads.images.base').'/'.$fileName.'.svg';

$pdf = new TCPDF();

TCPDF_FONTS::addTTFfont(dirname(dirname(dirname(dirname(__FILE__)))).'/vendor/font-awesome/fonts/ArchitectsDaughter.ttf', 'TrueTypeUnicode', '', 96);
TCPDF_FONTS::addTTFfont(dirname(dirname(dirname(dirname(__FILE__)))).'/vendor/font-awesome/fonts/Archivor.ttf', 'TrueTypeUnicode', '', 96);

$pdf->AddFont("Archivor");
$pdf->AddFont("ArchitectsDaughter");

$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->ImageSVG($uploadPath, $x='', $y='', $w='', $h='', $link='', $align='', $palign='', $border=0, $fitonpage=true);
$filename = 'export.pdf';
$pdf->output($filename, 'D');
exit; 

Other fonts working ok for me. Don't know what happening with some fonts. What is the solution for this?

like image 458
Himanshu Bhardiya Avatar asked Oct 31 '22 09:10

Himanshu Bhardiya


2 Answers

According to documentation TCPDF_FONTS::addTTFfont() adds the provided font permanently to the fonts folder and returns its name. So there is no reason to add it every time, but it is necessary to use added font with correct name.

// ...

$pdf = new TCPDF();

$fontArchitectsDaughter = TCPDF_FONTS::addTTFfont(realpath(__DIR__ . '/../../../vendor/font-awesome/fonts/ArchitectsDaughter.ttf'), 'TrueTypeUnicode', '', 96);
$fontArchivor = TCPDF_FONTS::addTTFfont(realpath(__DIR__ . '/../../../vendor/font-awesome/fonts/Archivor.ttf'), 'TrueTypeUnicode', '', 96);

$pdf->AddFont($fontArchivor);
$pdf->AddFont($fontArchitectsDaughter);

// ...
like image 136
Gennadiy Litvinyuk Avatar answered Nov 15 '22 06:11

Gennadiy Litvinyuk


First set up the font via TCPDF_FONTS::addTTFfont() or by adding the necessary files in the fonts dir (convert the TTF file via a TCPDF font converter like http://fonts.snm-portal.com)

After that, activate the font:

$pdf->SetFont('FontAwesome','');

Then, write a unicode character with the writeHTML function, starting with &#x and ending with ; e.g.:  for the f0c9 (bars) icon (http://fontawesome.io/icon/bars/):

$pdf->writeHTML('');

like image 32
thijsonline Avatar answered Nov 15 '22 06:11

thijsonline