通过CakePDF通过电子邮件发送PDF

时间:2012-09-05 15:37:39

标签: cakephp

我正在使用CakePHP 2,CakeEmail和CakePDF插件

我正在尝试根据视图生成PDF并将其附加到电子邮件中。我已经阅读了阅读我的文件但仍然没有更聪明。我是否必须在服务器上实际创建文件?

这是我的代码到目前为止(电子邮件已发送,但没有PDF)。

// Build PDF as attachment
        $CakePdf = new CakePdf();
        $CakePdf->template('email_view', 'default');
        //get the pdf string returned
        $pdf = $CakePdf->output();

        // SMTP Options
        $this->Email->smtpOptions = array(

            // Only to be used internally
            'port'=>'25',
            'timeout'=>'30',
            'host' => '111.111.111.111',
            'username'=>'',
            'password'=>''
         );
        $this->Email->template = 'newExpenseClaim';
        $this->Email->from    = 'Expense Tracker <james@test.com>';
        $this->Email->to      = 'james@test.com'; 
        $this->Email->subject = 'New Expense Claim Submitted - Please Review';
        $this->Email->sendAs = 'both';
        $this->Email->delivery = 'smtp';

        $this->Email->attachments = $pdf;

        // Set username & url in email
        $this->set('user', $fu['User']['name']);
        $this->set('ms', $ms);
        $this->Email->send();
        $this->set('smtp_errors', $this->Email->smtpError);

以下是github的读物:

https://github.com/ceeram/CakePdf

感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

我正在使用TCPDF。 我就是这样做的。使其适应您的代码:

$pdf->Content($cvData);
$content = $pdf->Output($cvFileName, 'S');
file_put_contents(TMP . $cvFileName, $content);
$this->Email->attachments = array($cvFileName=>TMP . $cvFileName);

答案 1 :(得分:0)

我最终得到了这个,继续我的所作所为:

// Build PDF
$CakePdf = new CakePdf();

// Add disbursements onto array
$merged = array_merge($expenseClaim, $expenseTotal, $approvedBy, $disbursements);

$CakePdf->viewVars($merged);
$CakePdf->template('email_view', 'default');
$filename = 'expense-claim-' .$id. '.pdf';
$CakePdf->write(APP . 'files' . DS . $filename);

基本上,我将传递给PDF视图所需的所有数据合并为一个数组,然后使用viewVars()传递它。

希望这有助于某人!