duanfu3884 2016-01-23 08:46
浏览 27

FPDF:俄语文本输出不正确

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条回答 默认 最新

  • douxia1988 2016-01-23 09:33
    关注

    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:

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改