mPDF - 从另一个函数调用时不生成PDF [英] mPDF - No PDF generated when called from within another function
问题描述
我有一个mail_merge函数(使用cakephp),当直接从浏览器调用时。
domain.com/contacts/mail_merge将生成PDF并将其保存到数据库映射字段所需的服务器。
然而,当我尝试调用这个mail_merge函数从另一个函数。例如$ this-> mail_merge($ cond,1); PDF不会生成。数据库字段仍然来到mail_merge函数,所以这不是这个问题。任何想法为什么这可能发生?我已经更新了我的代码下面,现在包括一个简单的生成的txt文件与我想要放在PDF上的代码,这样工作,所以有一些关于mPDF的函数调用时不会生成PDF。 p>
感谢
我的mail_merge函数如下:
//功能生成邮件合并 - mPDF
// -------------------------- ------------------------------------>
function mail_merge($ conditions = NULL,$ mail_merge_id = 1)
{
//删除标准布局
// ------------ ------------------------------------------>
$ this-> layout = null;
//获取联系人
// ------------------------------- ------------------------------>
$ contacts = $ this-> Contact-> Card-> find('all',array(
'limit'=> 10,
//'fields' > $ fields,
'contains'=> array('Contact','Prospect','SaleDetail'),
'conditions'=> $ conditions
)
$ this-> set('contacts',$ contacts);
// GE目录
// ------------------------------- ----------------------------->
$ this-> loadModel('MailMerge');
$ content = $ this-> MailMerge-> find('first',array(
'conditions'=> array('MailMerge.id'=> $ mail_merge_id),
'fields'=> array('MailMerge.description')
));
$ this-> set('content',$ content);
//初始化mPDF
// -------------------------------- ------------------------------------------->
$ this-> Mpdf-> init();
//渲染视图并将其设置为写入HTML
// ---------------------- -------------------------------------------------- --->
$ response = $ this-> render('mail_merge');
$ thebody = $ response-> body();
$ this-> Mpdf-> WriteHTML($ thebody);
//设置输出的文件名pdf文件
// ---------------------------- ----------------------------------------------->
$ thefilename =mail_merge _。date(d.m.Y_His)。。pdf;
$ this-> Mpdf-> setFilename(APP。WEBROOT_DIR。/ files / csv_exports /。$ thefilename);
//将输出设置为I,D,F,S
// ------------------------ -------------------------------------------------- - >
$ this-> Mpdf-> setOutput('F');
// TEMP - 在服务器上创建TXT文件
// -------------------------- ---------------------------------------------->
$ 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
我发现组件的setOutput函数引起的问题。
当我改变时:
$ this-> Mpdf-> setFilename(APP。WEBROOT_DIR。/ files / csv_exports /。
$ this-> Mpdf-> setOutput('F');
至
$ this-> Mpdf-> Output(APP。WEBROOT_DIR。/ files / csv_exports /。$ thefilename,'F');
它根据需要工作。
I have a mail_merge function (using cakephp) which when called directly from the browser e.g. domain.com/contacts/mail_merge will generate the PDF and save it to the server as needed with the database mapped fields.
When however I try to call this mail_merge function from another function. e.g. $this->mail_merge($cond, 1); The PDF won't generate. The database fields are still coming though to the mail_merge function so it's not this issue. Any idea why this might be happening? I have updated my code below to now include a simple generated txt file with the code I am trying to put onto the PDF and this works so there is simply something about mPDF that won't generate a PDF when called from a function.
Thanks
My mail_merge function is as follows:
// 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
I discovered the issue lied with the setOutput function of the component.
When I changed:
$this->Mpdf->setFilename(APP. WEBROOT_DIR . "/files/csv_exports/" . $thefilename);
$this->Mpdf->setOutput('F');
To
$this->Mpdf->Output(APP. WEBROOT_DIR . "/files/csv_exports/" . $thefilename, 'F');
it worked as needed.
这篇关于mPDF - 从另一个函数调用时不生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!