99

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

2
  • If you want to use more languages you need UTF8, so you may use tFPDF. Have a look at the composer package.
    – robsch
    Apr 25, 2018 at 10:13
  • Look for Daan's answer for the best answer!
    – Zim84
    Mar 24 at 20:25

20 Answers 20

138

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

12
  • 29
    I don't think this will help. Your answer does explain how to generate a PDF with ISO-8859-1 or windows-1252 encoding, but these encodings will not work for non-latin languages. Not to mention outputting multi-language (multi-script) texts. Feb 17, 2015 at 12:25
  • 5
    @Rafiq: dont use the "old" FPDF but the newer UTF8 Version tFPDF as postet in my Answer.
    – Tarsis
    May 7, 2015 at 15:03
  • 1
    Note: When using ISO-8859-1, the € character will not work (there is no Euro sign in the charset, use ISO-8859-15 instead).
    – BurninLeo
    Dec 30, 2015 at 20:42
  • 1
    @BurninLeo: ISO-8859-15 is not much better, all those Encodings have the same limit of Characters, so -15 has € but therefore is missing ´ or ½. The only real solution is to use UTF-8 instead of avoiding it - as shown in my answer.
    – Tarsis
    Jan 19, 2016 at 14:52
  • 3
    tFPDF does support unicode but it has not been updated in 3 years, where as FPDF has been updated more recently making tFPDF outdated. Please keep that in mind when using tFPDF.
    – Agilis
    Apr 1, 2016 at 14:16
47

You have two options to achieve real UTF-8 support:

1. Use official UTF-8 Version tFPDF

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.

2. Use an alternative Project like mPDF or TCPDF

There are also alternatives that I would prefer like mPDF or TCPDF (and others) wich base on FPDF but offer advanced functions, have UTF-8 Support, can interpret HTML Code (limited of course as there is no direct way to convert HTML to PDF) and are under active developement. 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

https://github.com/tecnickcom/tcpdf

9
  • 1
    None of the iconv and decode solutions works for the more exceptional characters (♠♥☺äκόσμος). But if you just replace your fpdf.php with the tpdf.php file, it all just starts working and your files get smaller as an extra bonus. Great fix.
    – Sebastian
    May 5, 2015 at 11:48
  • 1
    Are FPDF and tFPDF separate branches? Does anyone know the reason why is tFPDF hidden so well on the FPDF page? Are there any drawbacks, one should know before switching?
    – BurninLeo
    Jan 19, 2016 at 21:28
  • 1
    I am nut sure why they didnt make it the default FPDF Version, but it is linked directly on the main page fpdf.org and as mentioned there the features of tFPDF were originally developed for mPDF. I have used tFPDF for a lot of PDFs in a productive environment and i think besides the UTF-8 and font changes it is 100% the same. Never had any issues.
    – Tarsis
    Jan 20, 2016 at 13:54
  • 1
    tFPDF is a script, or an extension of FPDF. It is considered outdated (Updated 3 years ago) because FPDF has been updated more recently. The author of tFPDF does not keep it maintained and the GitHub associated with tFPDF has not been touched. See: github.com/rev42/tfpdf
    – Agilis
    Apr 1, 2016 at 14:19
  • As long as you dont need more functions i dont see a Problem in that, btw the last (minor) Update was only 4 Months ago - still better than not supporting UTF-8. Either way i would preffer TCPDF or mPDF, which both base on FPDF but give advanced functions and also support HTML Code.
    – Tarsis
    Apr 13, 2016 at 7:31
43

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

6
  • 1
    Perfect, this is still working or needed after updating FPDF to a version that supports PHP7 to show special characters correctely.
    – Aren
    Dec 9, 2016 at 8:14
  • 2
    This command work for me, $txt = iconv('utf-8', 'cp1252', $txt); Thks Sep 16, 2019 at 13:25
  • 2
    Just amazing simpel solution. Sad it is tucked away in a obscure place on stackoverflow :) Apr 25, 2020 at 20:00
  • This changed something with my spacing - some lines break earlier now. But at least it seems to work - before Turkish letter didn't work at all for me, now at least I see something.
    – Cold_Class
    Sep 13, 2020 at 11:39
  • It's 2024 and still the best answer for me after updating my old XAMPP. @Cold_class - any sideffects other than that?
    – Zim84
    Mar 24 at 20:24
10

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.

8

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 :

  1. As stated by Tarsis, swap FPDF to TFPDF.
  2. 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 :

https://www.earthfluent.com/privacy.pdf?language=ja

6

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)

0
5

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 :)

4

There is an extension of FPDF called mPDF that allows Unicode fonts.

http://www.mpdf1.com/mpdf/index.php

1
  • 4
    Link seems to be dead
    – robsch
    Apr 25, 2018 at 10:02
4

None of the above solutions are going to work.

Try this:

function filter_html($value){
    $value = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
    return $value;
}
4

You can make a class to extend FPDF and add this:

class utfFPDF extends FPDF {

function Cell($w, $h=0, $txt="", $border=0, $ln=0, $align='', $fill=false, $link='')
{
    if (!empty($txt)){
        if (mb_detect_encoding($txt, 'UTF-8', false)){
            $txt = iconv('UTF-8', 'ISO-8859-5', $txt);

        }
    }
    parent::Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);

} 

}

0
4

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

2

You can apply this function on your text :

 $yourtext = iconv('UTF-8', 'windows-1252', $yourtext);

Thanks

1
  • This has worked for me to display the characters ± and in my FPDF generated PDF. Nov 3, 2022 at 14:44
2

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!

1

Not sure if it will do for Greek, but I had the same issue for Brazilian Portuguese characters and my solution was to use html entities. I had basically two cases:

  1. String may contain UTF-8 characters.

For these, I first encoded it to html entities with htmlentities() and then decoded them to iso-8859-1. Example:

$s = html_entity_decode(htmlentities($my_variable_text), ENT_COMPAT | ENT_HTML401, 'iso-8859-1');
  1. Fixed string with html entities:

For these, I just left htmlentities() call out. Example:

$s = html_entity_decode("Treasurer/Tr&eacute;sorier", ENT_COMPAT | ENT_HTML401, 'iso-8859-1');

Then I passed $s to FPDF, like in this example:

$pdf->Cell(100, 20, $s, 0, 0, 'L');

Note: ENT_COMPAT | ENT_HTML401 is the standard value for parameter #2, as in http://php.net/manual/en/function.html-entity-decode.php

Hope that helps.

1

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);
1

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?

0
0

There's an extention to FPDF called UFDPF http://acko.net/blog/ufpdf-unicode-utf-8-extension-for-fpdf/

But, imho, it's better to use mpdf if you're it's possible for you to change class.

0

I use FPDF for ASP, and the iconv function is not available. It seems strange, by I solved the UTF-8 problem by adding a fake image (an 1x1px jpeg) to the pdf, just after the AddPage() function:

pdf.Image "images/fpdf.jpg",0,0,1

In this way, accented characters are correctly added to my pdf, don't ask me why but it works.

0

I know that this question is old but I think my answer would help those who haven't found solution in other answers. So, my problem was that I couldn't display croatian characters in my PDF. Firstly, I used FPDF but, I think, it does not support Unicode. Finally, what solved my problem is tFPDF which is the version of FPDF that supports Unicode. This is the example that worked for me:

require('tFPDF/tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();
$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->AddFont('DejaVu', 'B', 'DejaVuSansCondensed-Bold.ttf', true);

$pdf->SetFont('DejaVu','',14);

$txt = 'čćžšđČĆŽŠĐ';
$pdf->Write(8,$txt);

$pdf->Output();
0

For me none of the answered worked. I just wanted to print some symbols. So this was my code which worked.

$pdf->AddFont('Symbol','','symbol.php');
$pdf->SetFont('Symbol','',35);
$pdf->Write(10,chr(229));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.