Im using latest TCPDF version(5.9). But have some strange problems with encoding. I need Lithuanian language symbols like: ąčęėįšųūž. But get only few of it. Other remain like ?????
So what should I do ? I use default times font(it comes with TCPDF download).
Any help would be appreciated.
TCPDF is quite tricky with utf8. Best way to achieve what you want is to embed the font in generated PDF file itself. You can use freeserif font from the TCPDF package, it contains all the utf8 symbols, shows absolutely any character of any language, but adds ~700kb to the output file. That's probably the easiest way to get symbols you need if file size doesn't matter.
You could also make your own font to embed, containing the characters you need. That's probably the best solution, keeping it universal and small in size, but is more complex.
Alternatively, you can relay on core fonts, which are taken from the system, and if not found, replaced by a substitute. This makes output file extremely light, but adds the necessity of font subsetting to obtain exotic chars. Personally I haven't had a success with this, so I still think embedding font is the best solution, which also happens to be more universal..
there is a font included in the CPDF core fonts - dejavusans, it shows all the lithuanian characters. Just add the following:
$pdf->setHeaderFont(Array('dejavusans', '', 10, '', false));
$pdf->setFooterFont(Array('dejavusans', '', 8, '', false));
$pdf->SetFont('dejavusans', '', 10, '', false);
Set the $unicode parameter on the TCPDF constructor to false and the $encoding parameter to 'ISO-8859-1' or some other character map.
This will help you:
Default for UTF-8 unicode:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
Example of constructor for European charset:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
Set font to freeserif it will work.
I tested.
$pdf->SetFont('freeserif', '', 14, '', true);
Just discovered this same situation when trying to render Romanian text using the default Helvetica font. In doing some investigation I found that the tcpdf library treats it's default fonts (referred to as "core" fonts) as Latin1 characters so even if you tell it to use UTF-8 encoding and set the unicode flag, it will literally translate your text to Latin1 equivalents prior to rendering. The default behavior of the library is, if it finds a Latin1 equivalent, to translate each character that it can find an equivalent for otherwise it translates the character as '?'.
This can be found inside the TCPDF class in the following method chain:
Write() -> Cell() -> getCellCode() -> _escapetext().
Inside of _escapetext() you can see it is checking for $this->isunicode then checking the selected font to see if it's type is core|TrueType|Type1. If it is, it will take the string an "latinize" it for you by way of the UTF8ToLatin1() method. This is where the '?' translations are taking place.
My recommendation would be to use a custom unicode font (like Deja Vu Sans) that is similar to the default font you are after. That worked for me in my current situation.
To use TCPDF with special characters like ฿, 포 or others
you need to use a unicode font:
download the font here:
ftp://ftp.fu-berlin.de/unix/X11/multimedia/MPlayer/contrib/fonts/arialuni.ttf.bz2
create a test pdf file and load this font into TCPDF
example:
$fontname = $pdf->addTTFfont('/var/www/app/images/fonts/arialuni.ttf', 'TrueTypeUnicode', '', 32);
this will create the fonts like:
application/libraries/tcpdf/fonts/arialuni.ctg.z
application/libraries/tcpdf/fonts/arialuni.php
application/libraries/tcpdf/fonts/arialuni.z
now you can set the new font with :
$pdf->SetFont('arialuni', '', 10.5);
and now you can use special unicode characters like ฿ and more....
Source :
http://myridia.com/dev_posts/view/852
You u have problem to read character like Karnātaka from database and display like this karn?taka I mean "?" which we don't want then do following things :
Define charset for the connection (mysql_set_charset()):
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con) or die(mysql_error());
mysql_set_charset('utf8',$con);
Use $pdf->SetFont('DejaVuSerif', '', 10); instead of $pdf->SetFont('helvetica', 'B', 12);
For TCPDF Library of the PHP read character like Rājasthān instead of R?jasth?n from database
IIRC, you can define an encoding when you create a new font, as described here. Otherwise, you have to use the encoding that was defined when the font was created. It sounds like the fonts that ship with TCPDF all use WinAnsiEncoding... a.k.a. code page 1252.
Clunky, but effective.
With default TCPDF package tested dejavusans and freeserif and both fonts works with lithuanian characters. I also typed few russian characters and they worked too. I used this code to test it:
$this->pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$this->pdf->AddPage();
$this->pdf->SetFont('dejavusans', 'B', 20); // UTF8 fonts with lithuanian support: freeserif, dejavusans
$this->pdf->Write(0, 'ąžūčšęėųįĄŽŪČŠĘĖŲĮ Превед Кросавчег!', '', 0, 'C', true, 0, false, false, 0);
With dejavusans font it worked fine for both Russian and Latvian letters.
With me it was a font problem. I used the font timesand my local multibyte chras wouldn't show up properly. When I changed it to freeserif they were working normally :)
For this use the following code of the parameter TCPDF constructor
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
It will help you.
$fontname = $pdf->addTTFfont('C:\xampp\htdocs\copyshop\fonts\07-TH-Sarabun-PSK\THSarabun.ttf', 'TrueTypeUnicode', '', 32);
$pdf->SetFont($fontname, '', 16,'',FALSE); //Working
I had the same issue with Romanian characters and the problem wasn't the encoding, LC_CTYPE or other setting from TCPDF, but the font I used.
I mention that I used TWIG templating with Courier font.
You can try to change your font to freeserif
change the font to show normally ₹ and Lithuanian symbols
$pdf->SetFont('cid0cs', '', 12);
To faced this issue and I had to use dejavusans font which is a UTF-8 Unicode font. This help fix the encoding issue when render UTF-8 words like ąčęėįšųūž.
However, I was using helvetica font which is standard ASCII font, and changing document's font to dejavusans make it ugly.
To fix this I set two fonts to be loaded in the document and specified a part of the document to use dejavusans font
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
/*
......
set margins and other options
......
*/
// Set font
// dejavusans is a UTF-8 Unicode font,
// if you only need to print standard ASCII chars,
// you can use core fonts like helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 12, '', true);
$pdf->SetFont('helvetica', '', 12, '', true);
$html = '<div style="font-family: helvetica;">' . $clientName . '</div><div style="font-family: dejavusans;">' . $clientAddress . '</div>';
// Print text using writeHTMLCell()
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
$pdf->Output('invoice.pdf', 'I');
Related
I need to work on using open source/free font with TCPDF. Specifically Noto or Code2000. But it doesn't work well, and displays other font instead. Basically I want to have nice font, works with multiple language, and support bold and italic.
I did Google about this, but no luck.
Tried the following method:
TCPDF font converter.
ttf2ufm (https://github.com/PhenX/ttf2ufm)
Did anyone ever succeed? Are there another workaround on this?
Thanks!
I had the same problem.
make sure you have these 2 lines set
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$lg['a_meta_charset'] = 'UTF-8';
$pdf->setLanguageArray($lg);
$pdf->SetFont('dejavusans', '', 8);
dejavusans Font has the required data for several languages.
I am currently using FPDF to generate PDFs in PHP. Occasionally, these PDFs must include unicode icons like hearts, diamonds, spades, etc. Here's an example:
"This is a string ♡♡"
However, I cannot get those symbols to display. I've tried mb_convert_encoding, iconv, changing the font and none of them are giving me the result I need. Chinese, Japanese and Korean text are all displaying without issue; the problem seems to be related only to symbols.
Any ideas of how to display these in FPDF? Thanks.
I'm using TCPDF for generating PDF files
$UnicodeString='♡♡♡♡♡♡♡♡♡♡';
$fileNameHere='UnicodeFile.pdf';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setFontSubsetting(true);
$pdf->SetFont('dejavusans', '', 20, '', true);
$pdf->AddPage();
$pdf->writeHTMLCell(0, 0, '', '', $UnicodeString, 0, 1, 0, true, 'L', true);
$pdf->Output($fileNameHere,'F');
I actually just ended up porting all the FPDF-dependent code to mPDF. Took a while, but ultimately better than trying to hack something together that works with FPDFs limited capabilities.
Thanks for the responses.
I am using tcpdf for writing PDF. I am facing weird issue with it. I am not able to write Unicode Hindi Character. 'तुम्हारे' like for this character its writing as ?????. Though I have taken UTF-8 as input.
$tcpdf = new XTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$tcpdf->addTTFfont(K_PATH_FONTS . 'ARIALUNI.ttf', 'TrueTypeUnicode', '', 32);
Second line has created .z,.ctg,.php files. BUt still my text is outputted as ???
How can I write it for Hindi Unicode Characters.
I started using this website instead of the ->addTTFfont feature: http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf
I found this to be a much more reliable way of adding fonts to TCPDF. It will create the necessary files to drop in the folder. These are the files that would be created through the addTTFfont method.
Instructions:
Visit the website found above
Convert the .ttf file through this tool
Save the files it gives you right to the tcpdf/fonts folder.
I also search for hindi font in TCPDF and found solution at last. we have to a file from : https://code.google.com/p/pyfpdf/downloads/detail?name=fpdf_unicode_font_pack.zip&can=2&q=
we don't have to include whole files. just include lohit_hi.ttf file in your fonts fonlder
and then
$pdf->addTTFfont(K_PATH_FONTS . 'lohit_ta.ttf', 'TrueTypeUnicode', '', 32);
$pdf->SetFont('lohit_hi', '', 10 ,'true');
Is it possible to have Polish letters in TCPDF generated PDF => ążźśćłó? I used font which work in FPDF library named arial_ce, but with no result:
$fontname = $pdf->addTTFfont('tcpdf/tcpdf/fonts/arial_ce.ttf', 'TrueType', '', 96);
$pdf->SetFont($fontname, '', 14, '', true);
the same result was in using DejaVuSansCondensed font.
Delete the arial_ce.z, arial_ce.ctg.z and arial_ce.php files from your TCPDF font folder, and then change
$fontname = $pdf->addTTFfont('tcpdf/tcpdf/fonts/arial_ce.ttf', 'TrueType', '', 96);
to this:
$fontname = $pdf->addTTFfont('tcpdf/tcpdf/fonts/arial_ce.ttf');
(addTTffont doesn't overwrite an already existing font, even if you change parameters, that's why I mentioned deleting the ones already created.)
The defaults work better for this font. After doing this, I was able to use polish characters in PDFs made by TCPDF.
Edit: Oh, and of course, TCPDF should be using the same character-set that your source string is in. In my case that would be UTF-8.
Can somebody tell me what i am doing wrong? I need Arial font in my pdf generated by TCPDF.
First I've tried to use that :
1) I got Arial from windows fonts caltalog and put it in TCPDF directory.
2) Next I wrote in script :
$fontname = $pdf->addTTFfont('../lib/tcpdf/arial.ttf', '', '', 32);
After that in tcpdf/fonts appears 3 files (arial.ctg.z , arial.php and arial.z). I thought that everything was ok but if in TCPDF i use this font by:
$pdf->SetFont('arial', '', 16);
Font in document is indeed arial but without polish specific sings (ąęłżńź)
I've tried also prepare font by yourself :
I downloaded ttf2afm and makefontuni.php script then in command line I wrote this:
ttf2ufm -a arial.ttf
php -q makefontuni.php arial.ttf arial.ufm
that command gave me also 3 files (arial.ctg.z , arial.php and arial.z) but final situation is the same like before.
I've admit that all data writing to pdf is in UTF-8 and TCPDF construct is like this :
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'utf-8', false);
I don't know what am i doing wrong. :(
If you want to use a custom font use this tool
https://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf
when you get the generated files just move them to the /fonts directory and with the same name they have, set the font-name attribute.
No need to add any font, use the Dejavu Sans Font which has all the UTF-8 characters built-in and the TCPDF has it already..
$pdf->SetFont('dejavusans', '', 14, '', true);
http://www.tcpdf.org/examples/example_001.phps
$fontname = TCPDF_FONTS::addTTFfont(FCPATH.'/assets/css/fonts/arial-unicode-ms.ttf');
This is what I use to include custom font to TCPDF. You need only .ttf file of the font. Add it in folder on your choice on the server and run this code once. ( I run it first time on export ) Then you can comment this row and the font would be there.
In order to add it to the exporter you should add it as font with:
$pdf->addFont('your-font-name', '', 10, '', false);
And if you want it to be default:
$pdf->setFont('your-font-name', '', 10, '', false);
If you don't know what is the actual name of the font to use in PDF:
echo $fontname;
This would give you the specific name of the font to use in the exported file.
After you run that command once the TCPDF creates all the file needed in its font folder and it is ready to use from now on.
If you want to re-add the same font (modified) you need to delete your font files in tcpdf/fonts/your-font-name. and run this command again to re-add them.
Hm, are you sure your Arial has all UTF-8 characters?
I followed instructions here http://www.tcpdf.org/fonts.php
and it worked. What I had problems was that I was only able to add Regular Font - as soon as I added Bold or Italic and then change from one another, all characters turned to dots.
So at the moment I'm only using my Regular font and for Bold I use 'dejavusans' (thanks to Miro).
My code:
$fontname = $pdf->addTTFfont('/lib/tcpdf/fonts/myfont-Regular.ttf','TrueTypeUnicode','');
$pdf->SetFont($fontname, '', 8, '', true);
I think it will help you to fix the character issue.
$pdf->SetFont('freeserif', '', 12);
Above font-family which supports the UTF-8 characters.
I had the same error i was able to fixed it putting the next, after de line:
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,
true, 'UTF-8', false);
$fontname = $pdf->addTTFfont('tcpdf/fonts/arial.ttf', '', '', 32);
$pdf->SetFont('arial', '', 16);`
Is better if you convert the font to ctg.z/z/php files.
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('jameelnoorinastaleeq', 'BI', 20);