问题
In my PHP project, I have a PDF on which I would like to output Russian text:
Это предложение на русском языке.
In my MySQL database I have a table with Russian data with utf8_unicode_ci
. Inside this table the Russian data is represented with unidentifiable chars, as follows:
Я дейÑтвую Ñкорее доверительно Ð
On my pdf, not the corresponding Cyrllic gets outputted, but these unidentifiable chars.
Here comes an excerpt of my FPDF code, please note the variable $myRussianSentence
at the end whose output is problematic:
$pdf=new FPDF();
$pdf->SetAutoPageBreak(false, 0);
$pdf->AddPage();
$pdf->SetTitle('My Title');
$pdf->SetAuthor('John Miller');
$pdf->SetLeftMargin(18);
$pdf->SetFont('times', 'B', 36);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(174, 45, 'MY PDF', "", 1, "C");
$pdf->Ln(5);
$pdf->Image($strAdDeckLink, $x = 40, $y = 60, $w = 131, $h = 129, $type = '', $link = 'http://www.example.com', $align = '', $resize = false, $dpi = 96, $palign = '', $border = 0);
$pdf->Ln(130);
$pdf->SetFont('times', 'B', 24);
$pdf->Cell(174, 26, 'This is some text', "", 1, "C");
$pdf->Ln(5);
$pdf->SetFont('times', 'B', 32);
$pdf->Cell(180, 26, $myRussianSentence, "", 1, "C");
Doing var_export()
on the variable $myRussianSentence
shows the sentence in proper Cyrllic chars. However, on the PDF it is outputted as unidentifable chars as found in my database.
How do I achieve that proper Russian gets displayed on my PDF?
回答1:
On FPDF it says:
The 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.
tFPDF is the version referred to that has UTF-8 support:
This class is a modified version of FPDF that adds UTF-8 support.
The code looks very similar:
require('tfpdf.php');
$pdf = new tFPDF();
You'll need to use a Unicode font:
To use a Unicode font in your script, pass the font file name as third parameter of AddFont() and true as fourth parameter. The font may be located either in the font/unifont directory or directly in the system font folder (in case the _SYSTEM_TTFONTS constant is defined). The package ships with the DejaVu font family.
// Add a Unicode font (uses UTF-8)
$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->SetFont('DejaVu','',14);
There are several other extensions of FPDF that offer UTF-8 support:
- mPDF
- UFPDF
- TCPDF
来源:https://stackoverflow.com/questions/34961452/fpdf-russian-text-is-not-outputted-properly