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:
To
it worked as needed.