tFPDF generates an empty pdf file

浪子不回头ぞ 提交于 2020-05-29 13:04:48

问题


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');
}

回答1:


Adding a new font requires two steps:

  • Generation of the font definition file
  • Declaration of the font in the script

In your case, we need to generate the font definition file. For example, we can use the command line:

~/w/t/fpdf181> php makefont/makefont.php font/dejavu-sans/DejaVuSans.ttf cp1252
Font file compressed: DejaVuSans.z
Font definition file generated: DejaVuSans.php

Add font

$fontname = "DejaVuSans";
$pdf->AddFont($fontname, '', 'DejaVuSans.php', true);

http://www.fpdf.org/en/tutorial/tuto7.htm

[EDIT]

From your comment, you used the library http://www.fpdf.org/en/script/script92.php

1) Dejavu already added to tfpdf/font/unifont folder. And, the name should be

$fontname = 'DejaVu';

2) If we want to add our custom font folder, we need to override _SYSTEM_TTFONTS

// Optionally define the filesystem path to your system fonts
// otherwise tFPDF will use [path to tFPDF]/font/unifont/ directory
// define("_SYSTEM_TTFONTS", "C:/Windows/Fonts/");

tfpdf/tfpdf.php

 if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) 
 {
    $ttffilename = _SYSTEM_TTFONTS . $file ;
 }
 else {
    $ttffilename = $this->_getfontpath().'unifont/'.$file ;
 }

I created an sample test:

<?php

// Optionally define the filesystem path to your system fonts
// otherwise tFPDF will use [path to tFPDF]/font/unifont/ directory

//define("_SYSTEM_TTFONTS", "font/dejavu-sans/");

require "tfpdf.php";

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

//Example object data
/** @var StdClass $item */
$item = new StdClass();
$item->ordinal = 'Item Ordinal';
$item->domain_label = "Item Domain Label";
$item->domain_class_ordinal = "Item Domain Class Ordinal";
$item->class_label = 'Item Class Label';
$item->diagnosis_code = 'Item Diagnosis Code';
$item->diagnosis_label = 'Item Diagnosis Label';

// Add a Unicode font (uses UTF-8)
$fontname = 'DejaVu';
$pdf->AddFont($fontname,'','DejaVuSans.ttf',true);
$pdf->AddFont($fontname, 'I', 'DejaVuSans-Oblique.ttf', true);
$pdf->AddFont($fontname, 'B', 'DejaVuSans-Bold.ttf', true);
$pdf->SetFont('DejaVu','',14);

$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(10);
$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(10);
$pdf->SetFont($fontname, '', 14);
$pdf->Cell(40, 8, $item->diagnosis_label);
$pdf->Ln(10);

$pdf->Output('S');



回答2:


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; 
}


来源:https://stackoverflow.com/questions/51794930/tfpdf-generates-an-empty-pdf-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!