I have a problem with lithuanian letters like ąčęėįšųūž. For that reason I use tfpdf and font DejaVu. When I get content from txt file, everything is fine but I give content just simply, then I get "????". Header charset is set utf-8.
ob_start();
require('../fpd/tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();
// Add a Unicode font (uses UTF-8)
$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->SetFont('DejaVu','',10);
// Load a UTF-8 string from a file and print it
$txt = file_get_contents('../fpd/HelloWorld.txt');
$pdf->Write(8,$txt);
$pdf->Ln(10);
$txt1 = "ąčęėįšųž";
$pdf->Write(8,$txt1);
$pdf->Output('filename1.pdf', 'I');
ob_end_flush();
You do not need to set the header to UTF-8 but your editor have to be configured to use UTF-8. To verify that this is the issue, open the PHP file in e.g. Notepad++, click on "Encoding" and check the current encoding. You may convert it to UTF-8 (without BOM!!) in the same step and try again.
Related
I have an bosnian string $string = "Saglasno odredbama čl.19 Zakona o udruženjima i Statuta Udruženja računovođa i knjigovođa Srbije URIKS iz Beograda, ul."
I used "Write" function of fpdf http://www.fpdf.org/ but all "č" characters become to "è"
$pdf->Write (6, iconv('UTF-8', 'CP1250//TRANSLIT', "Saglasno odredbama čl.19 Zakona o udruženjima i Statuta Udruženja računovođa i knjigovođa Srbije URIKS iz Beograda, ul."));
How can I fix it? thank so much!
Something like this should work:
require_once 'fpdf181\fpdf.php';
require_once 'fpdf181\makefont\makefont.php';
MakeFont('C:\\Windows\\Fonts\\arial.ttf','cp1252');
$pdf = new FPDF();
$pdf->AddFont('Arial','','Arial.php');
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$pdf->Write(6,'Saglasno odredbama čl.19 Zakona o udruženjima i Statuta Udruženja računovođa i knjigovođa Srbije URIKS iz Beograda, ul.');
$pdf->Output();
Try to change the encoding to cp1250 or ISO-8859-2.
Source: http://www.fpdf.org/en/tutorial/tuto7.htm
If all this should fail then try to use UTF-8: http://www.fpdf.org/en/script/script92.php
Personally I would not use UTF-8 in my PDFs because the file size will big for me. I use font embedding in this case to avoid a big file size.
The FPDF class can produce documents in many languages other than the Western European ones: Central European, Cyrillic, Greek, Baltic and Thai, provided you own TrueType or Type1 fonts with the desired character set. UTF-8 support is also available.
For the UTF-8 support you have to use tFPDF which is based on FPDF. tFPDF accepts UTF-8 encoded text. Please read all this instructions and then download the ZIP file on the bottom from this site.
Convertation to UTF-8:
You do not need a convertation to UTF-8 if you use instructions above (from link) and load a UTF-8 string from a file like follows:
// this file file must be saved in UTF-8 before:
$str = file_get_contents('Text-saved-in-UTF-8.txt');
In other case:
You have to use mb_convert_encoding and not iconv. For Bosnian (Latin) this is 'iso-8859-2'.
In yor case for:
$mystring = "Saglasno odredbama čl.19 Zakona o udruženjima i Statuta Udruženja računovođa i knjigovođa Srbije URIKS iz Beograda, ul.";
You have to write:
$str = mb_convert_encoding($mystring, 'UTF-8', 'iso-8859-2');
Your PHP file must be saved in iso-8859-2 before (but not in UTF-8).
And then you use this in tFPDF.
Related answer from me: Serbian characters in FPDF
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);
How can I set a text file ENCODING (for instance UTF-8) in PHP?
Let me show you my problem. This is my code:
<?php
file_put_contents('test.txt', $data); // data is some non-English text with UTF-8 charset
?>
Output: اÙ!
fwrite() has the similar output.
But when I create the test.txt by notepad and set the charset UTF-8 the output is what I want.
I wanna set the charset in the PHP file.
Now this is my question: How to set text file encoding by PHP?
PHP does not apply an encoding when storing text in a file: it stores data exactly as it is laid out in the string.
You mention that you have problems opening the file in notepad.exe. That text editor is not very good at guessing the encoding of the file you are opening; if the text is encoded in UTF-8 you must choose to open it as UTF-8. Use another text editor if possible. Notepad++ is a popular replacement.
If you must use notepad.exe, as a last resort, write a Byte Order Mark to the file before you write anything else; this will make it recognize the file as UTF-8 while potentially making the file unusable for other purposes (see the Wikipedia article for details).
file_put_contents("file.txt", "\xEF\xBB\xBF" . $data);
You may try this using mb_convert_encoding
$data = mb_convert_encoding($data, 'UTF-8', 'auto');
file_put_contents('test.txt', $data);
Also check iconv.
Update : (try this and find the right encoding for your text)
foreach(mb_list_encodings() as $chr){
echo mb_convert_encoding($data, 'UTF-8', $chr)." : ".$chr."<br>";
}
Also, try this on GitHub.
Try:
file_put_contents('test.txt', utf8_encode($data));
You can create a function which converts a string array into a utf8 encoded string array and another to decode and write to a notepad file for you.
<?php
function utf8_string_encode(&$array){
$myencode = function(&$value,&$key){
if(is_string($value)){
$value = utf8_encode($value);
}
if(is_string($key)){
$key = utf8_encode($key);
}
if(is_array($value)){
utf8_string_encode($value);
}
};
array_walk($array,$func);
return $array;
}
?>
I'm trying to save HTML to a .html file,
This is working:
$html_file = "output.html";
$output_string="string with characters like ã or ì";
$fileHandle = fopen($html_file, 'w') or die("file could not be accessed/created");
fwrite($fileHandle, $output_string);
fclose($fileHandle);
When I check the output.html file, these special characters in my output_string are not read correctly.
My HTML file can't have a <head> tag with the charset information, this makes it work, but my output can't have any <html>, <head> or <body> tags.
I have tried stuff like
header('Content-type: text/plain; charset=utf-8');
I also tried utf8_encode() on the string before fwrite, but with no success so far.
If I read the output.html file in Notepad++ or Netbeans IDE, it shows the correct characters being saved, it's the browser that isn't them reading properly.
I'm pretty sure PHP is saving my file with the incorrect charset, because if I create HTML files in my computer with those special characters (even without any charset setting), these are read correctly.
Try to add a BOM (Byte Order Mark) to your file :
$output_string = "\xEF\xBB\xBF";
$output_string .= "string with characters like ã or ì";
$fileHandle = // ...
Yes, PHP is writing the file correctly, only the reading program doesn't know what character encoding it is and interprets the data with the wrong charset. If you cannot include meta information that convey the correct charset and if the file format itself (plain text) does not offer a way to specify the charset and if the reading application is not able to correctly guess the charset, then there's no solution.
Whatever editor you are using to write this code must have facility to set character-type as 'UTF-8'.
Set the character-type of the file in which you have written this code.
I am using an editor that allows to change the character encoding of file from the bottom. There must be something similar for the editor you are using.
If you need the string in UTF-8 regardless of the php-script-file-encoding (if it's a single-byte one), you should use the UTF-8 encoding of those characters:
$output_string = "string with characters like \xC3\xA3 or \xC3\x8C";