Skip to content

Instantly share code, notes, and snippets.

@gale93
Created July 9, 2018 10:31
Show Gist options
  • Save gale93/78306ec438698032de747f4d99604419 to your computer and use it in GitHub Desktop.
Save gale93/78306ec438698032de747f4d99604419 to your computer and use it in GitHub Desktop.
FPDF - Script for special characters
<?php
require('fpdf.php');
class TextNormalizerFPDF extends FPDF
{
function __construct()
{
parent::__construct();
}
function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
{
parent::MultiCell($w, $h, $this->normalize($txt), $border, $align, $fill);
}
function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
{
parent::Cell($w, $h, $this->normalize($txt), $border, $ln, $align, $fill, $link);
}
function Write($h, $txt, $link='')
{
parent::Write($h, $this->normalize($txt), $link);
}
function Text($x, $y, $txt)
{
parent::Text($x, $y, $this->normalize($txt));
}
protected function normalize($word)
{
// Thanks to: http://stackoverflow.com/questions/3514076/special-characters-in-fpdf-with-php
$word = str_replace("@","%40",$word);
$word = str_replace("`","%60",$word);
$word = str_replace("¢","%A2",$word);
$word = str_replace("£","%A3",$word);
$word = str_replace("¥","%A5",$word);
$word = str_replace("|","%A6",$word);
$word = str_replace("«","%AB",$word);
$word = str_replace("¬","%AC",$word);
$word = str_replace("¯","%AD",$word);
$word = str_replace("º","%B0",$word);
$word = str_replace("±","%B1",$word);
$word = str_replace("ª","%B2",$word);
$word = str_replace("µ","%B5",$word);
$word = str_replace("»","%BB",$word);
$word = str_replace("¼","%BC",$word);
$word = str_replace("½","%BD",$word);
$word = str_replace("¿","%BF",$word);
$word = str_replace("À","%C0",$word);
$word = str_replace("Á","%C1",$word);
$word = str_replace("Â","%C2",$word);
$word = str_replace("Ã","%C3",$word);
$word = str_replace("Ä","%C4",$word);
$word = str_replace("Å","%C5",$word);
$word = str_replace("Æ","%C6",$word);
$word = str_replace("Ç","%C7",$word);
$word = str_replace("È","%C8",$word);
$word = str_replace("É","%C9",$word);
$word = str_replace("Ê","%CA",$word);
$word = str_replace("Ë","%CB",$word);
$word = str_replace("Ì","%CC",$word);
$word = str_replace("Í","%CD",$word);
$word = str_replace("Î","%CE",$word);
$word = str_replace("Ï","%CF",$word);
$word = str_replace("Ð","%D0",$word);
$word = str_replace("Ñ","%D1",$word);
$word = str_replace("Ò","%D2",$word);
$word = str_replace("Ó","%D3",$word);
$word = str_replace("Ô","%D4",$word);
$word = str_replace("Õ","%D5",$word);
$word = str_replace("Ö","%D6",$word);
$word = str_replace("Ø","%D8",$word);
$word = str_replace("Ù","%D9",$word);
$word = str_replace("Ú","%DA",$word);
$word = str_replace("Û","%DB",$word);
$word = str_replace("Ü","%DC",$word);
$word = str_replace("Ý","%DD",$word);
$word = str_replace("Þ","%DE",$word);
$word = str_replace("ß","%DF",$word);
$word = str_replace("à","%E0",$word);
$word = str_replace("á","%E1",$word);
$word = str_replace("â","%E2",$word);
$word = str_replace("ã","%E3",$word);
$word = str_replace("ä","%E4",$word);
$word = str_replace("å","%E5",$word);
$word = str_replace("æ","%E6",$word);
$word = str_replace("ç","%E7",$word);
$word = str_replace("è","%E8",$word);
$word = str_replace("é","%E9",$word);
$word = str_replace("ê","%EA",$word);
$word = str_replace("ë","%EB",$word);
$word = str_replace("ì","%EC",$word);
$word = str_replace("í","%ED",$word);
$word = str_replace("î","%EE",$word);
$word = str_replace("ï","%EF",$word);
$word = str_replace("ð","%F0",$word);
$word = str_replace("ñ","%F1",$word);
$word = str_replace("ò","%F2",$word);
$word = str_replace("ó","%F3",$word);
$word = str_replace("ô","%F4",$word);
$word = str_replace("õ","%F5",$word);
$word = str_replace("ö","%F6",$word);
$word = str_replace("÷","%F7",$word);
$word = str_replace("ø","%F8",$word);
$word = str_replace("ù","%F9",$word);
$word = str_replace("ú","%FA",$word);
$word = str_replace("û","%FB",$word);
$word = str_replace("ü","%FC",$word);
$word = str_replace("ý","%FD",$word);
$word = str_replace("þ","%FE",$word);
$word = str_replace("ÿ","%FF",$word);
return urldecode($word);
}
}
$pdf = new TextNormalizerFPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$pdf->Cell(40,10,"Oggi è Giovedì e sono ±30°C ");
$pdf->Output();
?>
@wiljeonline
Copy link

You're awesome, thanks!

@pikbadaluk
Copy link

Ciao, ho un problema con fpdf. l'ho sempre usato ed andava bene con l'inglese, ma quest'anno mi trovo a dover fare documenti in francese ed in inglese,in doppia lingua e tutti i caratteri francesi vengono alterati. Ho provato da solo a inserire il codice di http://stackoverflow.com/questions/3514076/special-characters-in-fpdf-with-php
a cui anche tu ti richiami
ma non sono riuscito a farlo funzionare. Puoi aiutarmi?

@ramakrishnaj
Copy link

I faced issue with €™ any idea how to fix ?

@ramakrishnaj
Copy link

I faced issue with €™ any idea how to fix ?

worked with this iconv("UTF-8", "CP1250//TRANSLIT",$title)

@XARP616
Copy link

XARP616 commented Jul 11, 2022

You can find the character enconding in this webpage.
€ will translate to %80, for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment