How To Mail Generated Pdf

I have generated PDF using tcpdf extension …which display content in pdf format…code is below


$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8');

$pdf->SetCreator(PDF_CREATOR);

$pdf->setPrintHeader(false);

$pdf->setPrintFooter(false);

$pdf->AliasNbPages();

$pdf->AddPage();


$content.='<img src="/logo.jpg" /><br/>'; 


$pdf->writeHTML($content, true, false, false, false, '');

$pdf->LastPage();

$outPut = $pdf->Output('test.pdf', 'I');

Yii::app()->end();

how can i mail this generated PDF in mail with email extension?

Hi chaitsi

You can just attach it in email content as a link of pdf (url on your server),

Here is how I did it:

I have an action with second parameter determing to ouput PDF for download/screen OR a file (which is needed to attach the pdf to an email)




	public function actionPdf($id, $file_output=false)

	{                              

                $model=$this->loadModel($id);


				..... 

                

                if($file_output) {

                    $filename=Yii::app()->params['pathPdfTemp'].$label.date("-YmdHis").'.pdf';

                } else {

                    $filename='';

                }

		        $this->renderPartial('pdf',array(

			   'model' => $model,

                           'config' => $xmlConfig,

                           'label' => $label,

                           'filename' => $filename

		        ));

                

                if($file_output) {

                    return($filename);

                }

	}



If $file_output===true a file will be generated and filename is returned (in order to pass it i.e. to your addAttachment method in your mail routine)

Here is the corresponding view:




$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 

                            'P', 'mm', 'A4', true, 'UTF-8');




....

[Doing your pdf stuff here]

....							

							

if(strlen($filename)) {

    $pdf->Output($filename, "F");

} else {    

    $pdf->Output($label.date("-YmdHis").".pdf", "I");

}



The advantage of this solution is one action and one view for displaying PDF or creating physical files.