2014-09-03 34 views
1

我为TCPDF示例创建了一个简单的包装类,我从网上下了一个包。 我的目标是连续调用该类中的方法,然后将PDF文件输出到我的项目中的某个目录。TCPDF - 调用输出方法看起来什么都不做

当我在一个blob中有一个版本并从一个页面调用它时,代码工作正常。将它放入课程后,似乎挂起/在Output()的调用周围没有任何操作。在netbeans中进行调试时,我无法进入它,并且没有错误出现。

如果是文件夹权限问题,我已在输出目录上运行chmod。

这里是类:

<?php 

define('IMAGE_DIR', '/home/user/NetBeansProjects/PDF_Quote/img/'); 
require_once('lib/tcpdf/tcpdf.php'); 

class PDF_Test extends TCPDF { 


    function __construct() { 
     parent::__construct(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
    } 

    public function set_document_info($creator = '', $author = '', $title = '', $subject = '', $keywords = '') 
    {    
     $creator = PDF_CREATOR; 
     $author = 'Author'; 
     $title = 'Title Example'; 
     $subject = 'Subject Example'; 
     $keywords = 'TCPDF, PDF, example, test, guide'; 

     // set document information 
     $this->SetCreator($creator); 
     $this->SetAuthor($author); 
     $this->SetTitle($title); 
     $this->SetSubject($subject); 
     $this->SetKeywords($keywords); 
    } 

    public function header($logo_img = '', $title_text = '', $addit_text = '') 
    { 
     $logo_img = IMAGE_DIR . 'headerimg.png'; 
     $title_text = 'the title'; 
     $addit_text = 'additional text'; 

     $this->SetHeaderData($logo_img, PDF_HEADER_LOGO_WIDTH, $title_text.' 001', $addit_text, array(0,64,255), array(0,64,128)); 
     $this->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
    } 

    public function footer($logo_img = '', $text = '') 
    { 
     $this->setFooterData(array(0,64,0), array(0,64,128)); 
     $this->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); 
    } 

    public function set_default_monospaced_font($font = '') 
    { 
     if (strlen($font) === 0) { $font = 'courier'; } 
     $this->SetDefaultMonospacedFont($font); 
    } 

    public function set_margins($margin_left = '', $margin_top = '', $margin_right = '', $margin_header = '', $margin_footer = '') 
    { 
     $margin_left = PDF_MARGIN_LEFT; $margin_top = PDF_MARGIN_TOP; $margin_right = PDF_MARGIN_RIGHT; 
     $margin_header = PDF_MARGIN_HEADER; $margin_foot = PDF_MARGIN_FOOTER; 

     $this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); 
     $this->SetHeaderMargin(PDF_MARGIN_HEADER); 
     $this->SetFooterMargin(PDF_MARGIN_FOOTER);    
    } 

    public function set_auto_page_break($set = True, $margin = '') 
    { 
     $set = True; 
     $margin = PDF_MARGIN_BOTTOM; 

     $this->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); 
    } 

    public function set_image_scale($img_scale_ratio = '') 
    { 
     $img_scale_ratio = PDF_IMAGE_SCALE_RATIO; 

     $this->setImageScale(PDF_IMAGE_SCALE_RATIO); 
    } 

    public function set_language_array() 
    { 
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { 
     require_once(dirname(__FILE__).'/lang/eng.php'); 
     $this->setLanguageArray($l); 
    } 
    } 

    public function set_font_subsetting($set = True) 
    { 
     $set = True; 

     $this->setFontSubsetting($set); 
    } 

    public function set_font($family = '', $style = '', $size = '', $fontfile = '', $subset = '', $out = True) 
    { 
     $family = 'dejavusans'; $size = '14'; 

     $this->SetFont($family, $style, $size, '', $out); 
    } 

    public function add_page() 
    { 
     $this->AddPage(); 
    } 

    // Can be html or just plain text 
    public function add_text_blob($html = '') 
    { 
     $html = '<h1>This is some stuff</h1><p style="background-color: green;">This is some content adhjdjasjd</p>'; 

     $this->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); 
    } 

    public function output($path, $file_name) 
    { 
     $path = dirname(__FILE__) . '/pdfcache/'; //'/home/user/NetBeansProjects/PDF_Quote/pdfcache/'; 
     $file_name = 'file_' . date('Y_m_d_H_i_s') . '.pdf'; 

     try 
     { 
      $fp = $path . 'example1.pdf'; //. $file_name; //'/home/user/NetBeansProjects/PDF_Quote/pdfcache/example_001.pdf'; 
      $this->Output($fp, 'F'); 
     } 
     catch (exception $ex) 
     { 
      return $ex; 
     }    
     return $path . $file_name; 
    } 
} 

?> 

这里是在另一个PHP文件调用类的代码:

require_once('PDF_Test.php'); 
$pdf = new PDF_Test(); 

$pdf->set_document_info(); 
$pdf->header(); 
$pdf->footer(); 
$pdf->set_default_monospaced_font(); 
$pdf->set_margins(); 
$pdf->set_auto_page_break(); 
$pdf->set_image_scale(); 
$pdf->set_language_array(); 
$pdf->set_font_subsetting(); 
$pdf->set_font(); 
$pdf->add_page(); 
$pdf->add_text_blob(); 
$pdf->output(); 

我没有设法找到我的谷歌上搜索任何类似的问题,但我是PHP新手,所以我不确定我是否忽略了任何明显的东西。

回答

1

我建议使用Output方法,它以PDF格式返回二进制字符串,所以你可以随心所欲地做任何事情。这将是$pdfdoc = $pdf->Output('', 'S')

这样,您仍然可以将文件放到您选择的路径中,但也可以直接将其转储到客户端,或将其附加到电子邮件中......并且您可以执行更好的错误处理,因为您拥有控制TCPDF产生的内容。

顺便说一句,当你在PHP中重写一个方法,并且你想调用父方法时,那么你必须使用parent::METHOD()。因为你实际上覆盖了与你的TCPDF的方法(即使你的是小写的)的header,footeroutput方法。

+0

我的低劣超载似乎是问题所在。只要我将“$ this->”替换为“parent ::”,无论我何时调用TCPDF方法,都应该尽可能地开始输出。谢谢! – dbr 2014-09-04 08:33:29

相关问题