FPDF utf-8 encoding (HOW-TO)
Does anybody know how to set the encoding in FPDF package to UTF-8? Or at least to ISO-8859-7 (Greek) that supports Greek characters?
Basically I want to create a PDF file containing Greek characters.
Any suggestions would help. George
Don't use UTF-8 encoding. Standard FPDF fonts use ISO-8859-1 or Windows-1252. It is possible to perform a conversion to ISO-8859-1 with utf8_decode()
:
$str = utf8_decode($str);
But some characters such as Euro won't be translated correctly. If the iconv extension is available, the right way to do it is the following:
$str = iconv('UTF-8', 'windows-1252', $str);
There also is a official UTF-8 Version of FPDF called tFPDF http://www.fpdf.org/en/script/script92.php
You can easyly switch from the original FPDF, just make sure you also use a unicode Font as shown in the example in the above link or my code:
<?php
//this is a UTF-8 file, we won't need any encode/decode/iconv workarounds
//define the path to the .ttf files you want to use
define('FPDF_FONTPATH',"../fonts/");
require('tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();
// Add Unicode fonts (.ttf files)
$fontName = 'Helvetica';
$pdf->AddFont($fontName,'','HelveticaNeue LightCond.ttf',true);
$pdf->AddFont($fontName,'B','HelveticaNeue MediumCond.ttf',true);
//now use the Unicode font in bold
$pdf->SetFont($fontName,'B',12);
//anything else is identical to the old FPDF, just use Write(),Cell(),MultiCell()...
//without any encoding trouble
$pdf->Cell(100,20, "Some UTF-8 String");
//...
?>
I think its much more elegant to use this instead of spaming utf8_decode() everywhere and the ability to use .ttf files directly in AddFont() is an upside too.
Any other answer here is just a way to avoid or work around the problem, and avoiding UTF-8 is no real option for an up to date project.
There are also alternatives like mPDF or TCPDF (and others) wich base on FPDF but offer advanced functions, have UTF-8 Support and can interpret HTML Code (limited of course as there is no direct way to convert HTML to PDF). Most of the FPDF code can be used directly in those librarys, so its pretty easy to migrate the code.
https://github.com/mpdf/mpdf http://www.tcpdf.org/
there is a really simple solution for this problem.
In the file fpdf.php go to the line that says:
if($txt!=='')
{
It is line 648 in my version of fpdf. Insert the following line of code:
$txt = iconv('utf-8', 'cp1252', $txt);
(above the line of code)
if($align=='R')
This works for all German special characters and should also work for Greek special characters. Otherwise simply replace cp1252 with the respective alphabet you require. You can see all supported characters here: http://en.wikipedia.org/wiki/Windows-1252
I saw the solution here: http://fudforum.org/forum/index.php?t=msg&goto=167345 Please use my example code above, as the original author forgot to insert a dash between utf and 8.
Hope the above was helpful.
Daan
You need to generate a font first. You must use the MakeFont
utility included within the FPDF package. I used on Linux this a bit extended script from the demo:
<?php
// Generation of font definition file for tutorial 7
require('../makefont/makefont.php');
$dir = opendir('/usr/share/fonts/truetype/ttf-dejavu/');
while (($relativeName = readdir($dir)) !== false) {
if ($relativeName == '..' || $relativeName == '.')
continue;
MakeFont("/usr/share/fonts/truetype/ttf-dejavu/$relativeName",'ISO-8859-2');
}
?>
Then I copied generated files to the font
directory of my web and used this:
$pdf->Cell(80,70, iconv('UTF-8', 'ISO-8859-2', 'Buňka jedna'),1);
(I was working on a table.) That worked for my language (Buňka jedna is czech for Cell one). Czech language belongs to central european languages, also ISO-8859-2. Regrettably the user of FPDF is forced to lost advantages of UTF-8 encoding. You cannot get this in your PDF:
Městečko Fruens Bøge
Danish letter ø
becomes ř
in ISO-8859-2.
Suggestion of solution: You need to get a Greek font, generate the font using proper encoding (ISO-8859-7) and use iconv
with the same target encoding as the one the font has been generated with.