How to handle special characters in FPDF - php

I am currently using FPDF to create pdf's but realized that the FPDF class doesn't seem to be able to handle special characters, like tilde's for example. I know the strings coming from my database are UTF-8, but these characters get stripped out anyway. I've tried changing the character set, like this:
$myString= iconv('UTF-8', 'windows-1252', $someString);
But, still nothing. Is there any other solutions, other than using tFPDF? I've made some substantial changes to the original FPDF class and don't want to have to redo it all.
thanks
jason
EDIT
When I use FPDF and try to print something like this:
$this->SetFont( 'Arial', 'B', 19 );
$this->SetLineWidth(1);
$this->Line(10,10,290 ,10);
$this->Cell(300,15,iconv("UTF-8", "CP1250//TRANSLIT",'Días, Miércoles, Sábado,miércoles, Año'),0,1,'C');
And it prints out:
Días, Miércoles, Sábado,miércoles, A~no

Checkout the extension of FPDF/HTML2PDF called mPDF that allows Unicode fonts.
http://www.mpdf1.com/mpdf/index.php

Related

converting special characters with html_entity_decode not working

I've got a php site where I export some data into a pdf file. The site uses fpdf to create the pdf file but some special characters doesn't show corretly in the created pdf file.
The tricky thing is that some special characters are printed correctly while others aren't. The default_charset of the site is "iso-8859-1" and the php files are coded in "ANSI". I also printed the array with the info (that I get from the database) and it's ok. So, I guess it's something with the fpdf? But It's strange that the same special character is printed corretly in some places and incorrectly in others.
Thank you :)
EDIT:
I've found out it's in this piece of code that the strings get bad encoded:
foreach ($this->relatorioData['Roteiro'] as $label => $value) {
$this->Cell(55, 4, html_entity_decode($label), 0, 0, 'L', false);
$this->Cell(95, 4, html_entity_decode($value), 0, 1, 'L', false);
}
The strings are alright until the html_entity_decode where only some of them loose the special characters. Do you know what could do this? Should I use other method?
I was running into problems like this with FPDF, too. I searched all over before finding that this is actually addressed right in the FPDF FAQ (question/answer 3 as of this writing). There are a number of potential solutions in there depending on one's needs. The options cited there are:
Use PHP's utf8_decode() function
$str = utf8_decode($str);
Use PHP's iconv() function
$str = iconv('UTF-8', 'windows-1252', $str);
Those who need characters "outside windows-1252" are directed to a tutorial on adding new fonts and encodings, or tFPDF
Well, I've found my answer. I needed to change this:
html_entity_decode($text)
To this:
html_entity_decode($label, ENT_COMPAT, 'ISO-8859-1')
This way all my stringd were decoded right.

Polish characters aren't shown when using Helvetica

I make a PDF file from PHP.
I use FPDF library. I don't have polish letters in result PDF.
This is my code:
$pdf = new FPDF();
$pdf->AddPage();
$pdf->AddFont('helvetica','',TEMPLATEPATH.'/fonts/helvetica.php');
$pdf->SetFont('helvetica','');
$text = 'WITAJ ŻÓŁĘDZIU';
$pdf->Write(5,$text);
I use this code (I tried to use iconv but it didn't work). My result is:
"Witaj ¿ó³êdziu". What should I do?
Does your PHP script uses UTF-8 ?
FPDF does not natively handle UTF-8, that may explains why you get weird characters.
You can use TCPDF (http://www.tcpdf.org) which handles perfectly UTF-8.
The migration from FPDF to TCPDF is quite easy since it uses the same methods (same methods names, same arguments) as FPDF.
Have you tried UTF-8 encoding $text?
Set your second last line of code to:
$text = utf8_encode('WITAJ ŻÓŁĘDZIU');

TCPDF - missing Swedish characters

I'm using TCPDF in Codeigniter to generate PDF file.
I have link from 'view' to 'controller' function that contains parameters. One parameter is name = 'Högskolan'.
When I get this parameter in controller and display in PDF sometimes it's displayed like 'Högskolan', sometimes like 'Hgskolan'(missing swedish character). This issue happens only in IE (sometimes - not always).
Also there are differences when I save this file with File->Save as and File->Save.
With first option the file is saved as 'Hgskolan.pdf', with the second as 'Högskolan.pdf'.
What can caused these issues? Any idea?
Thanks.
I would suggest doing something like this: Convert accented characters to their plain ascii equivalents
You will find it works better to output without the accents (provided it doesn't substantially change the word of course).
As anttir suggested it's probably a browser specific issue or system issue not liking the characters. Can you test output on another browser or another platform to isolate the issue.
I'm not 100% sure if it's TCPDF tripping you up there, or the browser. You can test that with something like Fiddler (http://fiddler2.com/) or Charles (http://www.charlesproxy.com/) [both debugging proxies].

can't display ¥ with TCPDF, but other kanji are okay

I'm using TCPDF to create PDFs that include Japanese characters. Using the TrueType font ArialUni, most characters are displayed correctly, except the yen symbol shows up as a square box instead of ¥.
Here's a snippet of the resulting PDF using ArialUni:
So I tried another font. Here's the same section of the resulting PDF using GT200001:
And here's the same section using Helvetica:
Here's the same section using GNU's FreeSans:
I would like that second line to show up as "(渋谷猿, ¥8,000)"
I'm not surprised that Helvetica and Freesans cannot render the kanji correctly, but I cannot fathom why the other two fonts can render the kanji, but not the yen symbol, which is much more common.
The web server creating the PDFs is LAMP running Ubuntu. I'm viewing the PDFs on OS X with Chrome (using its in-browser view). I've also tried downloading the PDFs with Firefox and displaying in Preview. I get essentially the same results: ArialUni and GT200001 don't display the yen symbol, while Helvetica and Freesans don't display the kanji (but do display the yen symbol).
I know I can use different fonts for different lines/cells of the PDF, but the kanji and yen symbol are on the same line.
How can I get the kanji and yen symbol to display in a single line using TCPDF?
Near the top of my PDF code, I load the font using TCPDF's addTTFfont();
$this->font = $this->addTTFfont(K_PATH_FONTS.'arialuni.ttf', 'TrueTypeUnicode', '', 32);
Here's the code I'm using to write the section of the PDF.
$pdf->SetFont('arialuni','',10);
$pdf->MultiCell(105, $remarks_height, $remarks, 'B', 'L', false, 0, '', '', true, 1, false, true, $remarks_height, 'T');
In this wikipedia article you can read some more about this character.
Basically, there are 2 different ways of writing this Japanese Kanji, as it happens with some other symbols. From accepted answer "I was using ¥, not ¥", we can see that he was using the 'occidental' or Unicode ¥ symbol, when he actually wanted to use the double-width character ¥.
Oh dear, I figured it out.
I was using ¥, not ¥. Sorry for being confused!!
What I have noticed with TCPDF is, it's all about fonts.
I tried to use it for 15 totally different languages with different writing style too and only font I am using is Arial UNICODE MS.
TCPDF has function which will convert that font into TCPDF friendly files, which are arialuni.ctg.z(70k), arialuni.php(447k), and arialuni.z (14M).
the functions are,
http://www.tcpdf.org/fonts.php
$fontname = $pdf->addTTFfont('/path-to-font/ARIALUNICODE.TTF', 'TrueTypeUnicode', '', 32);

Producing symbols from HTML characters in FPDF

I have a government client that requires the legal 'section symbol' (§) in their documents. When creating the documents in a web page, this symbol is created with § or §.
I can not figure out how to get either of these to work in a pdf document created with FPDF. I have tried the iconv and utf8_decode methods, but neither have worked. Any ideas?
You're looking for html_entity_decode().
There is an easier approach to do this:
You can generate a section symbol using FPDF with using ascii code: chr(167).
It's good practice to make this a constant. At the top of your script, add:
define('SECTION',chr(167));
Now you can use SECTION in your script to print the euro symbol.
For example:
echo SECTION.'1.1';
Will output: §1.1
Note: This will also work for other symbols with chr(ascii), where ascii is the ascii code of the symbol. You can find an overview of ascii codes on this page: http://www.atwebresults.com/ascii-codes.php?type=2

Categories