Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrong symbol exported in fpdf... ñ as ñ ..?

Tags:

php

web

fpdf

fpdi

I dont know whats the problem but whenever I call the letter ñ from $_POST function it puts it in my fpdf as ñ .. any idea why?

like image 361
NORM Avatar asked Jan 09 '11 09:01

NORM


4 Answers

Your string has a wrong encoding. It's UTF-8 but I think you need Latin-1 or so..

You can use utf8_decode() on your strings:

echo utf8_decode("ñ"); // prints ñ
like image 101
Floern Avatar answered Oct 01 '22 11:10

Floern


I too have spent a fair amount of time trying to get this to work and thought the following explanation might be helpful for others.

No need to write your own complex functions if the only problem is a £/€ or similar character.

Here's what I have used (taken from this thread and others on the net):-

$pdf->SetY(220); // set the y co-ord before output //
$monthlyRent = utf8_decode("£" . number_format($monthlyRent, 2)); // 2 dec places //
// previously $monthlyRent is pulled from the MySql db as $array['_monthlyRent'];
$pdf->Write(5,'Gross monthly rent: ' . $monthlyRent); // outputs £980 or whatever. //

Works a treat but do remember to do the number_format($yourval, 2) in the same line as the utf8_decode(); or it screws up!

Hope this is helpful to some poor sod, who like me, has struggled for a few hours!

like image 44
Mr. Jones Avatar answered Oct 01 '22 09:10

Mr. Jones


The alternative solution would be to extend FPDF with Unicode (UTF-8) support. There is a patch for that: http://acko.net/node/56

like image 25
mario Avatar answered Oct 01 '22 09:10

mario


We had the problem once and using

utf8_decode("foo bar");

solved the problem (depends of course if the data is comming from the user, a database,...). Don't forget to deliver the file to the user in UTF-8 as well in the header

header("Content-type: application/pdf; charset=utf-8");
like image 21
DrColossos Avatar answered Oct 01 '22 09:10

DrColossos