dua55014 2018-01-09 18:26
浏览 856

如何在laravel项目中使用TCPDF和FPDI

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.

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.

I have already referred to these links with no luck:

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

This is the PHP function I am using inside my laravel controller.

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);
}
  • 写回答

1条回答 默认 最新

  • doubaomao9304 2018-01-09 18:54
    关注

    In fact this is TCPDF/FPDF question which is used by FPDI (to cooperate with FPDI I recommend you FPDF instead of TCPDF)

    First of all you need to add font that has your encoding. More: 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:

    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.

    Third, just convert:

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

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

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!