您可以轻松地从原始FPDF切换,只需确保您也使用了上面链接或“我的代码”中的示例所示的unicode字体:<?php
//this is a UTF-8 file, we won't need any encode/decode/iconv workarounds
//define the path to the .ttf files you want to use
define('FPDF_FONTPATH',"../fonts/");
require('tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();
// Add Unicode fonts (.ttf files)
$fontName = 'Helvetica';
$pdf->AddFont($fontName,'','HelveticaNeue LightCond.ttf',true);
$pdf->AddFont($fontName,'B','HelveticaNeue MediumCond.ttf',true);
//now use the Unicode font in bold
$pdf->SetFont($fontName,'B',12);
//anything else is identical to the old FPDF, just use Write(),Cell(),MultiCell()...
//without any encoding trouble
$pdf->Cell(100,20, "Some UTF-8 String");
//...
?>
我认为使用它比到处散播utf8_decode()要优雅得多,直接在AddFont()中使用.ttf文件也是一个好处。
这里的任何其他答案都只是一种避免或解决问题的方法,对于一个最新的项目来说,避免使用UTF-8并不是一个真正的选择。
在FPDF的基础上也有类似mPDF或TCPDF(以及其他)的替代方案,但是提供高级功能,支持UTF-8,并且可以解释HTML代码(当然,由于没有直接的方法将HTML转换为PDF,因此受到限制)。
大多数FPDF代码都可以直接在这些库中使用,因此迁移代码非常容易。