我正在使用FPDI从Laravel5项目中的template.pdf文件生成PDF,但我在使用UTF-8名称时遇到了问题,因为FDPI本身不支持编码。
我做了很多搜索,tFPDF和TCPDF似乎是这个问题最常用的解决方案,尽管我似乎不能成功地将它们中的任何一个集成到我的laravel应用程序中。
我已经提到了这些链接,但没有运气:
带tFPDF的FPDI:https://www.setasign.com/products/fpdi/demos/legacy-v1/
带TCPDF的FPDI:https://www.setasign.com/products/fpdi/demos/tcpdf-demo/
这是我在laravel控制器中使用的PHP函数。
public static function generateCertificate($firstname, $fullname, $course_name, $level, $date, $output, $email = null, $certificate_link = null)
{
$fullname = stripslashes($fullname);
$fullname = 'Witaj świecie'; //Added to debug issue
//Start PDF Generation
$pdf = new \setasign\Fpdi\Fpdi();
//Set Template
$path = app()->publicpath().'/assets/templates/examination_level100_certificate.pdf';
$pageCount = $pdf->setSourceFile($path);
$template = $pdf->importPage(1);
$size = $pdf->getTemplateSize($template);
$pdf->addPage();
$pdf->useTemplate($template, null, null, null, null, true);
//Add the fonts
$pdf->AddFont('Myriad-pro', '', 'myriad-pro-regular.php');
$pdf->AddFont('Myriad-pro-semibold', '', 'myriad-pro-semibold.php');
//Set Title
$pdf->SetTitle($fullname.' - '.str_replace(' - ', ' ', $course_name).' - Certicifate');
//Add User Name
$pdf->SetFont('Arial', '', 40);
$pdf->SetTextColor(67, 156, 209);
$pdf->SetY(164);
$pdf->Cell(0, 20, $fullname, 0, 1, 'C');
$pdf->Output('I', str_slug($fullname.'-'.$course_name).'.pdf', true);
}发布于 2018-01-10 02:54:31
实际上,这是FPDI使用的TCPDF/FPDF问题(为了配合FPDI,我建议您使用FPDF而不是TCPDF)
首先,您需要添加包含您的编码的字体。更多信息:http://www.fpdf.org/en/tutorial/tuto7.htm
例如:
$pdf->AddFont('DejaVu','','DejaVuSansCondensed.php');
$pdf->SetFont('DejaVu', '', 10, '', false);其次,关于由FPDI使用的FPDF库:有可能的编码:
cp1250 (Central Europe)
cp1251 (Cyrillic)
cp1252 (Western Europe)
cp1253 (Greek)
cp1254 (Turkish)
cp1255 (Hebrew)
cp1257 (Baltic)
cp1258 (Vietnamese)
cp874 (Thai)
ISO-8859-1 (Western Europe)
ISO-8859-2 (Central Europe)
ISO-8859-4 (Baltic)
ISO-8859-5 (Cyrillic)
ISO-8859-7 (Greek)
ISO-8859-9 (Turkish)
ISO-8859-11 (Thai)
ISO-8859-15 (Western Europe)
ISO-8859-16 (Central Europe)
KOI8-R (Russian)
KOI8-U (Ukrainian)我发送给pdf的字符串是UTF-8格式的(我用mb_detect_encoding函数检查了它),需要在cp1250上进行转换。
第三,只需转换:
$str = iconv('UTF-8', 'cp1250', 'zazółcić gęślą jaźń');https://stackoverflow.com/questions/48174507
复制相似问题