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
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
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.
How do I create PDF's in FPDF that support Chinese, Japanese, Russian, etc.?

(snapshots of code in use below)
I'd like to provide: a summary of the problem, the solution, a github project with the working code, and an online example with the expected, resultant PDF.
The Problem :
You actually need a font that supports the UTF-8 characters you are using.
I.E., merely using Helvetica and trying to display Japanese will not work. If you use Font Forge, or some other font tool, you can scroll to the Chinese characters of the font, and see that they are blank.
Google has a font (Noto font) that contains all languages, and it is 20mb, which is usually several factors the size of your text. So, you can see why many fonts simply won't cover every single language.
The Solution :
I'm using rounded-mgenplus-20140828.ttf and ZCOOL_QingKe_HuangYou.ttf font packs for Japanese and Chinese, which are open source and can be found in many open source projects. In tFPDF itself, or a new inheriting class of it, like class HTMLtoPDF extends tFPDF {...}, you'll do this...
$this->AddFont('japanese', '', 'rounded-mgenplus-20140828.ttf', true);
$this->SetFont('japanese', '', 14);
$this->Write(14, '日本語');
Should be nothing more to it!
Code Package on GitHub :
https://github.com/HoldOffHunger/php-html-to-pdf
Working, Online Demo of Japanese :
This answer didn't work for me, I needed to run html decode on the string also. See
iconv('UTF-8', 'windows-1252', html_entity_decode($str));
Props go to emfi from html_entity_decode in FPDF(using tFPDF extention)
just edit the function cell in the fpdf.php file, look for the line that looks like this
function cell ($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = false, $link = '')
{
after finding the line
write after the {,
$txt = utf8_decode($txt);
save the file and ready, the accents and the utf8 encoding will be working :)
I wanted to answer this for anyone who hasn't switched over to TFPDF for whatever reason (framework integration, etc.)
Go to: http://www.fpdf.org/makefont/index.php
Use a .ttf compatible font for the language you want to use. Make sure to choose the encoding number that is correct for your language. Download the files and paste them in your current FPDF font directory.
Use this to activate the new font: $pdf->AddFont($font_name,'','Your_Font_Here.php');
Then you can use $pdf->SetFont normally.
On the font itself, use iconv to convert to UTF-8. So if for example you're using Hebrew, you would do iconv('UTF-8', 'windows-1255', $first_name).
Substitute the windows encoding number for your language encoding.
For right to left, a quick fix is doing something like strrev(iconv('UTF-8', 'windows-1255', $first_name)).
For offsprings.
How I managed to add russian language to fpdf on my Linux machine:
1) Go to http://www.fpdf.org/makefont/ and convert your ttf font(for example AerialRegular.ttf) into 2 files using ISO-8859-5 encoding: AerialRegular.php and AerialRegular.z
2) Put these 2 files into fpdf/font directory
3) Use it in your code:
$pdf = new \FPDI();
$pdf->AddFont('ArialMT','','ArialRegular.php');
$pdf->AddPage();
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 0, 0, 211, 297); //width and height in mms
$pdf->SetFont('ArialMT','',35);
$pdf->SetTextColor(255,0,0);
$fullName = iconv('UTF-8', 'ISO-8859-5', 'Алексей');
$pdf->SetXY(60, 54);
$pdf->Write(0, $fullName);
Instead of this iconv solution:
$str = iconv('UTF-8', 'windows-1252', $str);
You could use the following:
$str = mb_convert_encoding($str, "UTF-8", "Windows-1252");
See: How to convert Windows-1252 characters to values in php?
Like many said here:
$yourtext = iconv('UTF-8', 'windows-1252', $yourtext);
BUT! with an '//Ignore' after the windows-1252 or in my case CP1252, like this:
iconv("UTF-8", "CP1252//IGNORE", $row['project_name'])
This one worked for me, I hope it works for you!