Yii: Generating Pdf Document From Server Response By Streaming Html Into Pdf

I am new to web development and remember from past that a friend of mine once had an implementation in which Server response stream of HTML was stored in a variable and then output as PDF, this was JSP some ages ago. I don’t know how to achieve this in Yii rather bit skeptical if I even remember it right. I have explored TCPDF and can produce the PDF by coding the HTML tags, however, I am looking into the option to stream the output of a URL response in a variable and then use that to generate the PDF. I did the following but it is not working. I receive error at filesize($filename):


$filename = 'http://localhost/webapp/index.php/link/to/some/page';

$handle = fopen($filename, "r");

$contents = fread($handle, filesize($filename));


$pdf->writeHTML($contents ,true);

I am bit lost on how to achieve this, all the help is much appreciated. Thanks in advance.

Try using Curl. If you are not familiar with it, there is also a yii extension that may facilitate things for you CURL for Yii

I’m not directly answering your post question, but is the page by any chance Yii-controlled? I mean, is it a controller that generates it?

In that case, you should find easier ways. I know that pdf extension has a WriteHTML() method, which you can pass a view rendered by Yii, so maybe TCPDF does the same?

Yes, I am planning to make it Yii based. I am thinking of creating an action in controller which would be called from a button, the action would take the HTML of an other view(HTML input streaming comes here) and pass it to the TCPDF->pdf object and I will save the PDF on the server. Once the PDF is successfully generated and saved, I will open a view to user which would let him see that PDF. User can then save it to local machine or close it. This is all how I am planning to implement, please correct me if I am wrong all the way and suggest an approach which would do the same.

I am on to what you suggested, it seems promising, will get back with my results.

Thanks a lot.

It’s exactly what I meant FaisalKhan. That’s something I coded several times using the extension I mentioned above. Works as advertised :)

Edit: so Instead of passing the page url and reading its contents, you can do


It worked exactly the way I wanted. So many thanks to you! Cheers :)

Need a little push on one thing. I have generated the PDF and can open it with 100% results but i am unable to present the generated PDF to user after it has been created. I am trying to open it soon after it created and tried the following in my controller action but in vain:

$html2pdf->Output($pdfFilename,EYiiPdf::OUTPUT_TO_FILE);//This is where I get the PDF successfully.


header("Content-Type: text/plain");

header("Content-disposition: attachment; filename=$pdfFilename");

header("Pragma: no-cache");

echo $filecontent;


Oh never mind…! a small mistake and now it working. I was rendering the view along with PDF generation because of which the Open/Save Dialog box was not coming. After I commented out the view code, it worked fine. Thanks in advance.

Cheers :)

Well I guess this should work:

$html2pdf->Output($pdfFilename, EYiiPdf::OUTPUT_TO_FILE);

header('Content-Type: application/pdf');

header('Cache-Control: must-revalidate'); // Depends on your project

$offset = 3600*24*30 ; // 1 month, adapt to your needs

$ExpStr = 'Expires: ' . gmdate('D, d M Y H:i:s', time() + $offset) . ' GMT';