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');
Related
I am trying to export to PDF using FPDF and TCPDF php library. I found that the emojis like 😁 😀 💃🏻 ❤️ 🥳 where not converted. Only ️️some rectangle box there in generated pdf. I also tried tfpdf.
$text = "There is my text 😁 , 😀 and emojis 💃🏻 ❤️ 🥳";
require('tfpdf/tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();
//Add a Unicode font (uses UTF-8)
$pdf->AddFont('Segoe UI Symbol','','seguisym.ttf',true); // DejaVuSans.ttf
$pdf->SetFont('Segoe UI Symbol','',12);
$pdf->Write(8,$text);
$pdf->Output();
I also tried different font. But didn't work for me. Can any one help me in this regard?
Sadly fPDF, TCPDF nor tFPDF can't print those characters. Issue is, these characters are not part of BMP, they are expressed with surrogate pairs, meaning they behave like multiple characters in UTF-16 (because of that one emoticon is printed as 2 rectangle boxes, not one) and also they have codepoint above 65535. However all mentioned PDF libraries relies on codepoint index being <= 65535 as well as TFontFile class reading TTF files.
You would also need to add TTF file having complete set of Unicode charset, or at least emoticons. Most fonts does not have it. This brings another issue for PDF library, which would probably need to have support for fallback font, which will be used when codepoint is not found in main font (for example you want to print text in Gotham, but since that does not include emoji, use other font for them). Btw for example emoji font "Noto Color Emoji" has 23 MB TTF file. So it gets big easily.
Anyway, all of the above can be added to PDF libraries, but it will require some effort. I am planning to do it for my needs as well sometimes. I think it will take roughly 1 man day.
Alternativelly, you might try something more robust like mPDF, but that library is huge, slow and require complete rewrite of your fPDF code. Also can't guarantee it can print emojis as well.
I'm writing a PDF exporter for a Slovak web page. My DB is UTF-8 encoding.
Some characters from the DB are converted correctly, some are not, here is the example:
input from DB: ôňúäéíáýážťčššľĽŠČĎŽŇÁÍÚĹŤÉŽŹÝ
output in PDF: ônúäéíáýážtcššlLŠCDŽNÁÍÚLTÉŽ´ZÝ
font used: Helvetica
Basic code for PDF write:
$pdf = new Fpdi('P', 'mm', 'A4');
...
$pdf->SetX(14);
$pdf->write(40, iconv('UTF-8', 'windows-1252//TRANSLIT//IGNORE', $invoiceDetails->getCompanyName()));
...
// return output for preview
return $pdf->Output('I');
I've tried at least 10 encodings but none of them was able to give me all characters.
Thank you for your help.
The standard fonts in FPDF only support cp1252 (aka windows-1252) encoding. So changing the encoding of your text to any other encoding doesn't change a thing.
You should prepare a special font with an ISO-8859-2 encoding and convert your text to this encoding before passing it to the methods of FPDF. The whole font generation process is described here.
I'm generating some order confirmations using FPDF library, which works great. But now I need some Chinese characters in the document. I have searching for many hours by now and have found no workable solution. Is it possible supporting Chinese characters using FPDF or is there an alternative to FPDF which will support it?
All the characters are mixed with regular text and are stored in a MySQL database. The PDF document only show unicode when printing the Chinese characters.
There exist Chinese encoding formats like Big5 or GBK. However, more likely than not, the text you are trying to input is in Unicode. There exists tFPDF, which provides Unicode support. I will test printing the following traditional hanzi: 中國. This means China, for those reading who do not know.
// Remember to copy msjh.ttf to [path to tFPDF]/font/unifont/ directory
// Initialize tFPDF
require('tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();
// Add a Unicode font like MSJH
$pdf->AddFont('MSJH','','msjh.ttf',true);
$pdf->SetFont('MSJH','',42);
// Output Chinese string to PDF
$pdf->Text(12,42,"中國");
// Output PDF document
$pdf->Output();
Another alternative is the Code200365k TTF
$pdf->AddFont('Code200365k','','Code200365k.ttf',true);
I use it for Chinese characters only as follows:
if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $my_value)){
$pdf->SetFont('Code200365k','',12);
}
$pdf->Cell(130,9,$my_value,1,1,'L');
$pdf->SetFont('Arial','',11);
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
Im developing a PDF generation script with PHP, using FPDF library, its all fine for text and images, but when I put corrency symbols like Pound or Euro, it is giving some special symbols instead, I could solver the same in normal pages by setting character encoding of the webpage, but not sure how to set character encoding for a PDF document.
You are running into encoding - decoding issues.. Add this line on top of your script.
<?php
header('Content-Type: text/html;charset=utf-8');
followed by your FDPF generation code.