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

如何在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/

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集