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

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?

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);
// ...

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

Related

tcpdf custom font weird characters

I am trying to use a custom font in my tcpdf file.
$pdf->addTTFfont('../../../fonts/RedHatDisplay-Regular.ttf', '', '', 32);
addTTFfont does:
TCPDF_FONTS::addTTFfont($fontfile, $fonttype, $enc, $flags, $outpath, $platid, $encid, $addcbbox);
This is my Code.
It just gives me weird chars when creating the pdf. The thing is, if I use the RedHatDisplay-Italic.ttf file, it just works perfect. Do you have any ideas what I can do?
So I had the same issue with the Quicksand font. The issue was the font itself (or the resource where I got it from).
I tried multiple resources, because in the past I downloaded Google Fonts (like Lato) and they worked.
So for Quicksand the downloaded font from
https://google-webfonts-helper.herokuapp.com/fonts failed
https://www.fontmirror.com/quicksand worked
CAUTION It looks like if the font already exists, it won't be overwritten by TCPDF_FONTS::addTTFfont so you need to manually get rid of an existing font first (probably from previous adding in vendor/tecnickcom/tcpdf/fonts) .
Other than that, the following code just works fine:
Only call this code once to add the font
<?php
$fontnames = [];
$fontnames[] = TCPDF_FONTS::addTTFfont('fonts/Quicksand Bold.ttf');
$fontnames[] = TCPDF_FONTS::addTTFfont('fonts/Quicksand Regular.ttf');
// Make sure you use the real font name. Different file names may (or may not) produce different font names.
die(print_r($fontnames, 1));
This code then for generating the pdf
<?php
$pdf = new TCPDF('P', 'mm', 'A4');
$pdf->SetFont('quicksand'); // This is the fontname from above
$pdf->AddPage();
$txt = '0123456789 ABCDEFGHIJKLMNOPQRSTUVQXYZ';
// print a block of text using Write()
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);
$pdf->Output('test.pdf', 'I');
exit(1);

Embedded SVG in the PDF created using TCPDF turning grey scale

I'm using TCPDF to create a PDF file. However, some of the SVG files that I have and would like to use in this PDF turns into grey/ black in the output. If I open those SVG files in the browser or Adobe Illustrator it looks correct.
Code that I have used to create the PDF:
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->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set some language-dependent strings (optional)
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 10);
$pdf->AddPage();
$pdf->ImageSVG($file='images/star.svg', $x=5, $y=5, $w='200', $h=200, $link='', $align='', $palign='', $border=0, $fitonpage=false);
$pdf->Output('example_012.pdf', 'I');
However, another SVG works absolutely fine. I have attached both the SVG file and PDF attachment.
SVG that don't work in PDF: https://www.dropbox.com/s/2kbj2200cw4hhrm/star.svg?dl=0
Output PDF:
https://www.dropbox.com/s/nn8pfpdmzx66a50/mypdf-star.pdf?dl=0
SVG that works correctly in PDF
https://www.dropbox.com/s/fsr6f763n39wflt/tux.svg?dl=0
Output PDF:
https://www.dropbox.com/s/5hs4jo3dz3knyod/mypdf-tux.pdf?dl=0
One of several SVG features that TCPDF doesn't support is setting style characteristics via class names. For example, your star SVG's fill colors won't be used because they are set with a class.
For this particular case, export from Illustrator with inline styles or presentation attributes and it should work.
[Edit: As an addendum, there are numerous ways for an SVG to break in TCPDF. In general, simpler is better. TCPDF's SVG parser is reasonably capable, but far from complete, so some breaking is expected and this is why the option to rasterize with ImageMagick is avaiable.]

How do you add custom fonts in TCPDF?

I would like to add a custom font to a pdf I'm generating using TCPDF. I might be missing something but the docs seem to be out dated. They are referencing the addTTFfont() function but I think it's been deprecated and no longer exists in the latest version of TCPDF.
I read that I need to convert the ttf file and put it in the fonts folder so I ran:
php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf
and it generated these files which are now in the fonts folder:
rumpelstiltskinwebfont.ctg.z
rumpelstiltskinwebfont.z
rumpelstiltskinwebfont.php
Then I tried to add the font:
$pdf->addFont('rumpelstiltskin');
$pdf->SetFont('rumpelstiltskin', '', 14, '', false);
but I'm getting an error:
TCPDF ERROR: Could not include font definition file: rumpelstiltskin
I figured out my issue, I was almost there.
Here is a step by step:
First convert your font using the tcpdf_addfont.php tool font in the TCPDF tools folder:
php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf
This will generate the required files and put them in the TCPDF fonts folder. Check the fonts folder and copy the name of the font, in my case it was rumpelstiltskinwebfont.
In your code set the font using the font file's name and write a line of text:
$pdf->SetFont('rumpelstiltskinwebfont', '', 14, '', false);
$pdf->Write(0, 'Fill text', '', 0, '', true, 0, false, false, 0);
That's it. Hope this helps someone. :)
Got this answer in another question and resolved for me. You just need to use the first parameter, the path of the font file. Worked with TTF and OTF fonts.
It generates a name string to use with $pdf->SetFont($fontname, '', $font_size);
Hope it helps.
The latest TCPDF version automatically convert fonts into TCPDF format using the addTTFfont() method. For example:
// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic.ttf', 'TrueTypeUnicode', '', 96);
// use the font
$pdf->SetFont($fontname, '', 14, '', false);
For further information and examples, please check the TCPDF Fonts documentation page.
NOTE: Once the font has been converted, TCPDF no longer requires the TTF file or the above call to addTTFfont()!
You can convert ttf fonts with http://fonts.palettize.me and then put uncompressed result in fonts folder into tcpdf class. Then you can add it with $pdf->SetFont('rumpelstiltskinwebfont', '', 14); using the name of the file
Since TCPDF (version 6.2.6) you can use TCPDF_FONTS::addTTFfont directly instead of AddFont.
TCPDF makes three files out of the ttf and puts them into the directory "font" of TCPDF. Once these have been created, they can be used for all future PDFs.
Example:
First (for simplicity) I created a folder in the font folder of TCPDF with the name of the new font, in my example "arialuni" and copied my ttf of this font into it.
If I want to create my font files in a single separate file that does not use Composer autoload, I have to include TCPDF once and also specify the correct path to my ttf.
require_once('vendor/tecnickcom/tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$fontname = TCPDF_FONTS::addTTFfont('vendor/tecnickcom/tcpdf/fonts/arialuni/arialuni.ttf', 'TrueTypeUnicode', '', 32);

Adding images over PDF using FPDI and TCPDF

I'm trying to add images on top of an existing PDF.
The pdf contains a blank grid, I am writing a script that will insert images on top of the PDF and output a new modified PDF file.
I am using FPDI and TCPDF libraries to load and edit the PDF file.
Any image or text i try to add using Write(); and Image(); functions does not appear anywhere in the Output file.
<?php
// defining encoding and linking libraries
header('Content-Type: text/html; charset=utf-8');
require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php');
// Creating new page with PDF as a background
$pdf = new FPDI();
$pdf->setSourceFile("resources/worksheet_template.pdf");
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
// $pdf->MultiCell(0, 5,'$pdf', 0, 'J', 0, 2, '', '', true, 0, false);
$pdf->Image('resources/lantern.png', 50, 50, 100, '', '', 'http://www.tcpdf.org', '', false, 300);
ob_clean();
$pdf->Output('WorksheetTest.pdf', 'I');
?>
This script works without problems! Expecting that all resources are accessible. The MultiCell call looks something special but as you'd used single quotes at least the string $pdf will appear in the resulting document. Furthermore the header() is obsolete too. Make sure that your PHP file is saved without a BOM to get rid of the ob_clean() call, too.
Generally it should be a good practice to define a font and size before writing any text but it seems that TCPDF handles this internally by a standard font for you.
Also make sure that you are using the latest version of both FPDI and TCPDF.

Yii TCPDF missing embed font in Corel Draw X6

I'm using this extension in Yii:
http://www.yiiframework.com/extension/tcpdf/
I would like to write to a pdf file with an external font, for example this:
http://www.1001fonts.com/open-sans-font.html
Here is my code:
$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8');
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Some Body");
$pdf->SetTitle("TCPDF Example 002");
$pdf->SetSubject("TCPDF Tutorial");
$pdf->SetKeywords("TCPDF, PDF, example, test, guide");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
$fontRegular = $pdf->addTTFfont(Yii::getPathOfAlias('webroot') . '/OpenSans-Regular.ttf');
$pdf->SetFont($fontRegular, "", 30);
$pdf->Cell(0,10,"Example",1,1,'C');
$pdf->Output("example_002.pdf", "I");
It works, I can read the pdf many programs, except Corel Draw 6X. When I'm opening the pdf it starts with a message of "missing fonts" and I can't replace them any other kind of fonts. Can anyone tell me how should I add the ttf font, or embed it into the pdf?
Thanks in advance!
So the problem is for Corel Draw . You have to Install the font.
1)Download your ttf font from here
2)Extract it and copy the font to C:\Windows\Fonts
3) Restart Corel Draw and make sure the new font is available by creating a text field and selecting the font.
Reference

Categories