tFPDF生成一个空的pdf文件 [英] tFPDF generates an empty pdf file

查看:244
本文介绍了tFPDF生成一个空的pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tFPDF库在PHP中生成PDF文件.我从FPDF开始,除了UTF-8字符乱码以外,它都工作良好.经过一番搜索,我发现可以实现tFPDF在PDF文件中包含UTF-8字符.但是我所有的尝试都导致无法打开一个空文件-将-0 KB文件写入我的硬盘驱动器,并在双击该文件时显示文件格式有问题"消息.这是我的代码.我在错误日志中没有看到PHP错误,因此很难判断出问题所在.

I am trying to generate a PDF file in PHP using the tFPDF library. I started with FPDF and it worked fine except that the UTF-8 characters were garbled. After some searching, I found that one could implement tFPDF to include UTF-8 characters in a PDF file. But all my attempts have resulted in an empty file that cannot be opened--0 KB file written to my hard drive and "There's a problem with the file format" message when I double click it. Here is my code. I'm getting no PHP errors in the error log so it's a little difficult to tell what is going wrong.

protected function getPDFFileData($diagnosis_id)
{
    define('FPDF_FONTPATH', JPATH_COMPONENT.'/helpers/font/');
    require_once JPATH_COMPONENT.'/helpers/tfpdf.php';
    //require_once JPATH_COMPONENT.'/helpers/fpdf.php';

    $item = $this->getItem($diagnosis_id);

    $pdf = new tFPDF('P', 'mm', 'Letter');
    //$pdf = new FPDF('P', 'mm', 'Letter');
    $pdf->AddPage();

    $fontname = "dejavusans";
    $pdf->AddFont($fontname, '', 'DejaVuSans.ttf', true);
    $pdf->AddFont($fontname, 'B', 'DejaVuSans-Bold.ttf', true);
    $pdf->AddFont($fontname, 'I', 'DejaVuSans-Oblique.ttf', true);
    $pdf->SetFont($fontname, '', 10);

    /* $fontname = "helvetica";
    $pdf->AddFont($fontname, '', 'helvetica.php');
    $pdf->AddFont($fontname, 'B', 'helveticab.php');
    $pdf->AddFont($fontname, 'I', 'helveticai.php');
    $pdf->SetFont($fontname, '', 10); */

    $domain_header = "Domain".chr(160).($item->ordinal).".".chr(160).($item->domain_label);
    $pdf->Cell(90, 6, $domain_header, 0, 0, 'L');
    $class_header = "Class".chr(160).($item->domain_class_ordinal).".".chr(160).($item->class_label);
    $pdf->Cell(90, 6, $class_header, 0, 1, 'R');
    $pdf->Ln();
    $pdf->SetFont($fontname, '', 10);
    $domain_class_info = "Domain".chr(160).($item->ordinal).chr(160).chr(183).chr(160)."Class".chr(160).($item->domain_class_ordinal).chr(160).chr(183).chr(160)."Diagnosis Code".chr(160).($item->diagnosis_code);
    $pdf->Cell(40, 6, $domain_class_info);
    $pdf->Ln();
    $pdf->SetFont($fontname, '', 14);
    $pdf->Cell(40, 8, $item->diagnosis_label);
    $pdf->Ln();

    // More code that generates more text . . . 

    return $pdf->Output('S');
}

推荐答案

这最终对我有用.我必须删除对PHP函数chr(​​)的所有调用,并对ASCII集之外的字符使用特定的UTF-8语法(例如,"\ u2022"表示项目符号字符).我相信我找到了在这些字符上使用json_decode()函数以在stackoverflow上的某个位置呈现所需输出的技巧.我还发现我无法获得使用参数'S'调用Output()函数以返回字符串的结果,因此我使用filename参数将输出写入服务器上的文件.这是修改后的功能.

This is what ended up working for me. I had to delete all calls to the PHP function chr(), and use the specific UTF-8 syntax for characters outside the ASCII set (e.g., '\u2022' for a bullet character). I believe I found the tip to use the json_decode() function on these characters to render the desired output somewhere on stackoverflow. I also found that I could not get results calling the Output() function with parameter 'S' to return a string, so I used a filename parameter to write the output to a file on the server. Here is the revised function.

protected function getPDFFileData($diagnosis_id, $output_file_name)
{
    define('FPDF_FONTPATH', JPATH_COMPONENT.'/helpers/fpdf181/font/');
    require_once JPATH_COMPONENT.'/helpers/fpdf181/tfpdf.php';

    $item = $this->getItem($diagnosis_id);

    $pdf = new tFPDF('P', 'mm', 'Letter');
    $pdf->AddPage();

    $fontname = "DejaVu";
    $pdf->AddFont($fontname, '', 'DejaVuSans.ttf', true);
    $pdf->AddFont($fontname, 'B', 'DejaVuSans-Bold.ttf', true);
    $pdf->AddFont($fontname, 'I', 'DejaVuSans-Oblique.ttf', true);
    $pdf->SetFont($fontname, '', 10);

    $bullet_char_uni = '\u2022';
    $bullet_char = json_decode('"'.$bullet_char_uni.'"');

    $domain_header = "Domain ".($item->ordinal).". ".($item->domain_label);
    $pdf->Cell(90, 6, $domain_header, 0, 0, 'L');
    $class_header = "Class ".($item->domain_class_ordinal).". ".($item->class_label);
    $pdf->Cell(90, 6, $class_header, 0, 0, 'R');
    $pdf->Ln();
    $pdf->SetFont($fontname, '', 10);
    $diagnosis_code = "Domain ".($item->ordinal)." ".$bullet_char." Class ".($item->domain_class_ordinal)." ".$bullet_char." Diagnosis Code ".($item->diagnosis_code);
    $pdf->Cell(40, 6, $diagnosis_code, 0, 1, 'L');
    $pdf->Ln();
    $pdf->SetFont($fontname, '', 14);
    $pdf->Cell(40, 8, $item->diagnosis_label, 0, 1, 'L');

    // More code that generates more text . . . 

    $output = $pdf->Output($output_file_name);
    return $output; 
}

这篇关于tFPDF生成一个空的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆