FPDF utf-8 encoding (HOW-TO)

Does anyone know how to set the encoding in the FPDF package in utf-8? Or at least ISO-8859-7 (Greek), which support Greek characters?

Basically, I want to create a pdf file containing Greek characters.

Any suggestions would help. George

+69
unicode utf-8 character-encoding fpdf
Jun 13 2018-11-11T00:
source share
18 answers

Do not use UTF-8 encoding. Standard FPDF fonts use ISO-8859-1 or Windows-1252. You can convert to ISO-8859-1 with utf8_decode() : $str = utf8_decode($str); But some characters, such as the euro, will not be translated correctly. If the iconv extension is available, the correct way to do this is: $str = iconv('UTF-8', 'windows-1252', $str);

+99
Jan 30 2018-12-12T00:
source share

There is also an official version of FPDF UTF-8 called tFPDF http://www.fpdf.org/en/script/script92.php

You can easily switch from the original FPDF, just make sure that you also use the Unicode font, as shown in the example in the link above or in 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 also up.

Any other answer here is just a way to avoid or get around the problem, and avoiding UTF-8 is not a real option for an updated project.

There are also alternatives, such as mPDF or TCPDF (and others), which are based on FPDF but offer advanced features that support UTF-8 and can interpret HTML code (of course, since there is no direct way to convert HTML to PDF). FPDF can be used directly in these libraries, so it's pretty easy to port code.

https://github.com/mpdf/mpdf http://www.tcpdf.org/

+32
03 Feb '14 at
source share

there is a really simple solution to this problem.

In the fpdf.php file, go to the line that says:

 if($txt!=='') { 

This is line 648 in my fpdf version. Paste 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, as well as for Greek special characters. Otherwise, simply replace cp1252 with the appropriate alphabet that you need. Here you can see all the supported characters: http://en.wikipedia.org/wiki/Windows-1252

I saw a solution here: http://fudforum.org/forum/index.php?t=msg&goto=167345 Please use my sample code above, as the original author forgot to insert a dash between utf and 8.

Hope this was helpful.

Daan

+16
04 Feb '14 at
source share

First you need to generate a font. You must use the MakeFont utility included in the FPDF package. I used on Linux this is a slightly 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 the generated files to the font directory of my website and used this:

 $pdf->Cell(80,70, iconv('UTF-8', 'ISO-8859-2', 'Buňka jedna'),1); 

(I worked on the table.) It worked in my language (Buňka jedna - Czech for Cell one). Czech belongs to the central European languages, also ISO-8859-2. Unfortunately, the FPDF user is forced to lose the benefits of UTF-8 encoding. You cannot get this in your PDF file:

 Městečko Fruens Bøge 

The Danish letter ø becomes ř in ISO-8859-2.

Solution suggestion: You need to get the Greek font, generate the font using the correct encoding (ISO-8859-7) and use iconv with the same target encoding as the font was generated with.

+9
Apr 10 2018-12-12T00:
source share

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

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

+4
Jul 26 '13 at 15:56
source share

This answer did not work for me, I also needed to run html decoding in a string. Cm

 iconv('UTF-8', 'windows-1252', html_entity_decode($str)); 

Details go to emfi from html_entity_decode to FPDF (with tFPDF extension)

+4
Nov 05 '13 at 5:24
source share

You can create a class for the FPDF extension 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); } 

}

+4
Feb 07 '17 at 8:07
source share

None of the above solutions will work.

Try the following:

 function filter_html($value){ $value = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8'); return $value; } 
+3
Mar 25 '16 at 12:40
source share

I wanted to answer this for those who did not switch to TFPDF for any reason (integration with the card, etc.).

Go to: http://www.fpdf.org/makefont/index.php

Use a .ttf compatible font for the language you want to use. Be sure to select the encoding number that suits your language. Download the files and paste them into your current FPDF font directory.

Use this to activate the new font: $pdf->AddFont($font_name,'','Your_Font_Here.php');

Then you can usually use $pdf->SetFont .

In the font itself, use iconv to convert to UTF-8. So, if you use Hebrew, you would do iconv('UTF-8', 'windows-1255', $first_name) .

Replace the Windows encoding number to encode your language.

From right to left, a quick fix does something like strrev(iconv('UTF-8', 'windows-1255', $first_name)) .

+3
Oct 26 '17 at 14:16
source share

For posterity.

How I managed to add Russian to fpdf on my Linux machine:

1) Go to http://www.fpdf.org/makefont/ and convert your ttf font (e.g. AerialRegular.ttf) into 2 files using ISO- 8859-5 : AerialRegular.php and AerialRegular.z

2) Put these 2 files in the 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
Oct 26 '16 at 16:26
source share

Instead of this solution, iconv:

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

You can use the following:

 $str = mb_convert_encoding($str, "UTF-8", "Windows-1252"); 

See: How to convert Windows-1252 characters to values ​​in php?

+1
Jul 25 '18 at 10:08
source share

just edit the function cell in the fpdf.php file, find 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 { ,

 $txt = utf8_decode($txt); 

save the file and you're done, accents and utf8 encoding will work :)

+1
Feb 28 '19 at 16:15
source share

There the extension for FPDF is called UFDPF http://acko.net/blog/ufpdf-unicode-utf-8-extension-for-fpdf/

But, imho, it is better to use mpdf if you can change the class.

0
Jun 05 '14 at 22:19
source share

You can apply this function in your text:

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

thank

0
Aug 25 '15 at 23:28
source share

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

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

Thus, accented characters are correctly added to my PDF file, do not ask me why, but it works.

0
Dec 11 '15 at
source share

Not sure if this will be done for the Greeks, but I had the same problem for the Brazilian Portuguese characters and I decided to use html objects. I had basically two cases:

  • A string may contain UTF-8 characters.

To do this, I first encoded it to html objects using htmlentities() , and then decrypted them to iso-8859-1 . Example:

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

For this, I just left the htmlentities() call. Example:

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

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

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

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

Hope this helps.

0
Feb 05 '16 at 20:31
source share

I know this question is old, but I think that my answer will help those who did not find a solution in other answers. So my problem was that I could not display Croatian characters in my PDF file. Firstly, I used FPDF, but I think it does not support Unicode. Finally, the solution to my problem is tFPDF, which is a version of FPDF that supports Unicode. This is an 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
May 7 '16 at 7:59
source share

How do I create a PDF that supports Chinese, Japanese, Russian, etc.?

php-html-to-pdf-converter.jpg

(snapshots of code below)

I would like to provide: a brief information about the problem, a solution, a GitHub project with working code, and an online example with the expected result of the PDF.

This problem:

  1. As Tarsis stated, swap FPDFs with TFPDFs.
  2. In fact, you need a font that supports the UTF-8 characters that you use.

    IE just 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 empty.

    Google has a font ( Noto font) that contains all languages, and its size is 20 MB, which usually depends on the size of your text. So, you can understand why many fonts just do not cover every language.

Decision:

I use the rounded-mgenplus-20140828.ttf and ZCOOL_QingKe_HuangYou.ttf font sets for Japanese and Chinese, which are open source and can be found in many open source projects. In tFPDF itself or in its new inherited class, for example in the class HTMLtoPDF extends tFPDF {...} , you will do this ...

 $this->AddFont('japanese', '', 'rounded-mgenplus-20140828.ttf', true); $this->SetFont('japanese', '', 14); $this->Write(14, '日本語'); 

Nothing more to be!

GitHub code package:

https://github.com/HoldOffHunger/php-html-to-pdf

Working, online demonstration of the Japanese language:

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

0
Jun 03 '19 at 14:28
source share



All Articles