pdf generation then email

Hi all,

still a bit of a noob with yii…

I know there are a couple of topics about pdf’s but none of them really answer my problem.

First off little overview.

  • I have multiple forms and I would like in the grid view to have a link to the pdf of a the view of the model.

I could not find exactly how to do this, I am just wondering how would i go about retrieving the pdf and where would it be stored.

I was looking at using wkhtmltopdf as tcpdf seems to be pretty big unless you guys would recommend a different one.

The next step I would like to do after being able to create the pdf - how would I go about attaching this to an email?

Thanks in advance if you can help,

That is not a question regarding Yii very much.

Both wkhtmltopdf and tcpdf are external libraries, refer to them for the specific documentation.

About email there is the documentation of php.

Check anyway the extensions of Yii: you will find lot of useful things:



and much more

I was able to do this using tcpdf and swiftmailer. :) just refer to their respective documentation

tcpdf can render different output types(render to browser, show as downloadable, email attachment format, etc.) and swiftmailer also allow you to add attachments

ah Cool never realised that, thanks for this I will give this a go.

What are the errors that you encountered?.. try setting the 3rd parameter to false, then 4th parameter to true in the renderPartial function

just tried that get an error saying "Cannot modify header information - headers already sent"

Maybe i am not doing it correctly i am pointing the browser to the pdf action in my controller. This is my action

	public function actionPdf($id){

		$params = array("author" => "Pdf test", "title" =>"TCPDF Example 002", "subject" => "TCPDF Tutorial", "keywords"=>array("TCPDF, PDF, example, test, guide"), "header" => false, "footer" => false, "output" => "example_002.pdf");

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


		$html = $this->renderPartial('view', array('model' => $this->loadModel($id)), false, true);



		$pdf->SetTitle("TCPDF Example 002");

		$pdf->SetSubject("TCPDF Tutorial");

		$pdf->SetKeywords("TCPDF, PDF, example, test, guide");





		$pdf->SetFont("times", "BI", 20);

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

		$pdf->Output("example_002.pdf", "I");

		return $pdf;


i did mine like this:



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

        $outputPdf = new TCPDF();






        $outputPdf->setHeaderData('', 0, $note->title, '');

        $outputPdf->setHeaderFont(array('helvetica', '', <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />);

        $outputPdf->setFooterFont(array('helvetica', '', 6));

        $outputPdf->setMargins(15, 18, 15);



        $outputPdf->setAutoPageBreak(true, 0);

        $outputPdf->setFont('dejavusans', '', 7);


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


        $outputPdf->Output(trim($note->title) . '.pdf', 'I');

tcpdf’s output function send headers by default so you do not need to send it again for that request.

*note that you need to point to the last page before you output anything -> $outputPdf->LastPage();

Awesome! perfect.

When you create the pdf does it actually save it somewhere on the server?

thanks for the help so far.

 $outputPdf->Output(trim($note->title) . '.pdf', 'I');

That’s possible. Try changing the 2nd parameter to letter “F”. For the other possible values, see the documentation for the output function :)

In my case ("I"), it will render automatically to the browser.

glad to help :)

Cool that did the trick! found the documentation too thanks give it a read now.

"I" is pretty useful for testing and formatting.

will give the email attachment a go soon once i have created all the pdf’s


Pdf generation is working fine now, Thanks.

Just got a slight issue with emailing with attachments, I am using the swift mailer extension but when using

$message->attach(Swift_Attachment::fromPath(Yii::app()->request->baseUrl."/pdf/".$model->id."/".$model->name.".pdf"), $model->name.".pdf", "application/pdf");

I get this error:

[<a href=‘function.fopen’>function.fopen</a>]: failed to open stream: No such file or directory

The file definitely exists in the location I can see it on the server… is there something I am doing wrong.

I did also try just attach with the location of the file but just said not expecting string.

Thanks in advance

You may want to try basePath instead of baseUrl

Ah yes that wouldn’t help B). totally missed that but doesn’t seem to fix it

I have done a bit of checking, it appears that the file cannot be accessed when I try php function file_exists it returns false. I also tried is_readable - same result… not too sure why this would be happening as I am echoing the location it is checking and pasting in browser url and hey presto loads the PDF.

Would Yii be doing something to block access to certain areas?

If that’s the case, then there is something wrong with how you specified the path to the pdf file. :) try playing with the string some more

That’s what I thought but I have even tried putting the direct url to the pdf in function file_exists but still returns false.

Really doesn’t make sense as files are there.

any ideas?

You should specify a path to the file and not a url. file_exists().

Where did you execute the swift attach function? Is it on a controller action? Try putting the absolute path to the file.

Aha found the issue basepath was pointing to the protected folder so I am now saving pdf’s in a folder within the protected folder, Is this OK?

In answer to you questions I executed it just after creating and yes it in the controller action.

how to create pdf output into a new tab…

when i enter some data’s into a text box during creation of some records…

at the time when i click create it should show a pdf output into a new tab…

You may look at this extension http://www.yiiframework.com/extension/pdf/ (not tested yet)

how can i send my current page view instead of $note->content…i need to convert a particular <div>…</div > element to pdf. that <div> will contain a CDetail view and a Cgridview on same page