我有一个 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