mPDF - 从另一个函数中调用时不生成PDF

时间:2012-10-10 23:07:57

标签: php cakephp mpdf

我有一个mail_merge函数(使用cakephp),当直接从浏览器调用时,例如 domain.com/contacts/mail_merge将生成PDF并根据需要使用数据库映射字段将其保存到服务器。

然而,当我尝试从另一个函数调用此mail_merge函数时。例如$ this-> mail_merge($ cond,1); PDF不会生成。数据库字段仍然会到达mail_merge函数,因此不是这个问题。知道为什么会这样吗?我已经更新了下面的代码,现在包含一个简单生成的txt文件,其中包含我试图放入PDF的代码,这样就可以了解mPDF的一些内容,从函数调用时不会生成PDF。

由于

我的mail_merge功能如下:

// FUNCTION GENERATE MAIL MERGE - mPDF
// -------------------------------------------------------------->
function mail_merge($conditions=NULL, $mail_merge_id=1)
{
    // REMOVE THE STANDARD LAYOUT
    // ------------------------------------------------------>
    $this->layout = null;

    // GET THE CONTACTS
    // ------------------------------------------------------------->
    $contacts = $this->Contact->Card->find('all', array(
                                                  'limit' => 10, 
                                                  //'fields' => $fields,
                                                'contain' => array('Contact', 'Prospect', 'SaleDetail'),
                                                  'conditions' => $conditions
                                                  ));
    $this->set('contacts', $contacts);

    // GE THE CONTENTS
    // ------------------------------------------------------------>
    $this->loadModel('MailMerge');
    $content = $this->MailMerge->find('first', array(
                                                'conditions' => array('MailMerge.id' => $mail_merge_id),
                                                'fields' => array('MailMerge.description')
                                                ));
    $this->set('content', $content);

    // initializing mPDF
    // --------------------------------------------------------------------------->
    $this->Mpdf->init();

    // RENDER THE VIEW AND SET AS A VARIABLE TO WRITE THE HTML
    // --------------------------------------------------------------------------->
    $response = $this->render('mail_merge');
    $thebody = $response->body();
    $this->Mpdf->WriteHTML($thebody);

    // setting filename of output pdf file
    // --------------------------------------------------------------------------->
    $thefilename = "mail_merge_".date("d.m.Y_His").".pdf";
    $this->Mpdf->setFilename(APP. WEBROOT_DIR . "/files/csv_exports/" . $thefilename); 

    // setting output to I, D, F, S
    // --------------------------------------------------------------------------->
    $this->Mpdf->setOutput('F');

    // TEMP - CREATE TXT FILE ON THE SERVER
    // ------------------------------------------------------------------------>
    $thefilenametxt = "mail_merge_".date("d.m.Y_His").".txt";
    $ourFileHandle = fopen(APP. WEBROOT_DIR . "/files/csv_exports/" . $thefilenametxt, 'w');
    fwrite($ourFileHandle, $thebody); 
    fclose($ourFileHandle); 

    return $thefilename;

} // END MAIL MERGE

1 个答案:

答案 0 :(得分:1)

我发现该问题与组件的setOutput函数有关。

当我改变时:

$this->Mpdf->setFilename(APP. WEBROOT_DIR . "/files/csv_exports/" . $thefilename); 
$this->Mpdf->setOutput('F');

$this->Mpdf->Output(APP. WEBROOT_DIR . "/files/csv_exports/" . $thefilename, 'F');

它可以根据需要运作。