简体   繁体   English

如何在Laravel项目中将FPDI与TCPDF一起使用

[英]How to use TCPDF with FPDI in a laravel project

I am using FPDI to generate PDFs from a template.pdf file in a Laravel 5 project, but I am having issues with UTF-8 names since FDPI does not support the encoding natively. 我正在使用FPDI从Laravel 5项目中的template.pdf文件生成PDF,但是由于FDPI本身不支持编码,所以UTF-8名称存在问题。

I did a lot of searching and tFPDF and TCPDF seem to be the most used solutions for this issue, though I cannot seem to successfully integrate any of them in my laravel application. 我做了很多搜索,但tFPDFTCPDF似乎是该问题最常用的解决方案,尽管我似乎无法成功地将它们中的任何一个集成到我的laravel应用程序中。

I have already referred to these links with no luck: 我已经很幸运地提到了这些链接:

FPDI with tFPDF: https://www.setasign.com/products/fpdi/demos/legacy-v1/ 带有tFPDF的FPDI: https ://www.setasign.com/products/fpdi/demos/legacy-v1/
FPDI with TCPDF: https://www.setasign.com/products/fpdi/demos/tcpdf-demo/ 带有TCPDF的FPDI: https ://www.setasign.com/products/fpdi/demos/tcpdf-demo/

This is the PHP function I am using inside my laravel controller. 这是我在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);
}

In fact this is TCPDF/FPDF question which is used by FPDI (to cooperate with FPDI I recommend you FPDF instead of TCPDF) 实际上,这是FPDI使用的TCPDF / FPDF问题(与FPDI合作,我建议您使用FPDF而不是TCPDF)

First of all you need to add font that has your encoding. 首先,您需要添加具有编码的字体。 More: http://www.fpdf.org/en/tutorial/tuto7.htm 更多: http : //www.fpdf.org/en/tutorial/tuto7.htm

For example: 例如:

$pdf->AddFont('DejaVu','','DejaVuSansCondensed.php');
$pdf->SetFont('DejaVu', '', 10, '', false);

Second, In regard to FPDF library that is user by FPDI: There possible encodings: 第二,关于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)

The string that was sent by me to pdf was in UTF-8 (I checked it by mb_detect_encoding function) and there was need to convert on cp1250. 我发送给pdf的字符串是UTF-8(我通过mb_detect_encoding函数进行了检查),并且需要在cp1250上进行转换。

Third, just convert: 第三,只需转换:

$str = iconv('UTF-8', 'cp1250', 'zazółcić gęślą jaźń');

Also useful: https://www.setasign.com/products/fpdi/manual/ 也有用: https : //www.setasign.com/products/fpdi/manual/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM