TCPDF生成无效文件

时间:2014-08-05 09:47:15

标签: php codeigniter tcpdf

我正在使用tcpdf库在Codeigniter中生成pdf文件。但输出pdf文件无法由adobe reader读取。以下是我为生成pdf而添加的内容。

tcpdf();
    $from_date = $this->session->userdata['reports']['from_date'];
$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$obj_pdf->SetCreator(PDF_CREATOR);
$title = "Meida Report - ". date("F j, Y", strtotime($from_date))."";
$obj_pdf->SetTitle($title);
$obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $title, PDF_HEADER_STRING);
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$obj_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$obj_pdf->SetFont('helvetica', '', 9);
$obj_pdf->setFontSubsetting(false);
$obj_pdf->AddPage();
$content='';
$content .= "<table cellpadding=\"6\" >";
$content .= "<thead><tr><th>Publication Name</th><th>Page</th><th>Date</th><th>Title</th><th>Size</th><th>Cost</th></tr></thead><tbody>";
foreach ($reports as $data) 
{

    $content .= "<tr>";
    $content .= "<td>".$data->publication_name."</td>";
            $content .= "<td>".$data->page."</td>";
            $content .= "<td>".$data->pfile_date."</td>";
            $content .= "<td>".$data->file_title."</td>";
            $content .= "<td>".$data->size."</td>";
            $content .= "<td>".$data->full_page_ad_cost . " " . $data->currency."</td>";
            $content .= "</tr>";
}
$content .= "</tbody></table>";
ob_flush();     
ob_end_clean(); 
$obj_pdf->writeHTML($content, true, false, true, false, '');
$obj_pdf->Output('sales_report.pdf', 'I');

&GT;

我无法找到问题。这些代码有什么问题?

1 个答案:

答案 0 :(得分:0)

我有同样的问题并通过更改输出来解决:

在我之前:

class System{
public:
  System() : a(make_A()), b(*a) {} // use initializer list for constructor
private:
  // Fix order of members to initialize in correct order.
  std::unique_ptr<A> a;
  B b;
};

但我改为:

$pdf->Output("filename.pdf",'F'); 

//强制下载(或从桌面显示) =&GT;它有效。