How do I prevent Yii from altering the headers?

Hello,

I want to return a PDF file to the user. I am using the TCPDF library for this. TCPDF gives me the following error: “Some data has already been output, can’t send PDF file”. It appears that Yii earlier decided that the output of this view should be HTML so it went ahead and sent the HTML headers and now that breaks my PDF page.

How do I tell Yii to not mess with the headers for this particular page?

The code to produce the PDF file is in a view. I call the view with




<? $this->renderPartial('pdfpage', array('CPdf' => $CPdf, 'data' => $foo))



I thought that using “renderPartial” would be enough, but clearly it isn’t.

Help? Any ideas?

Ah… Fudge… I found the problem. This is my index.php file:




<?

header("Content-type: text/html; charset: UTF-8");

header("Cache-Control: must-revalidate");

$offset = 60*60*24; // 1 day.

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

header($ExpStr);


$yii = dirname(__FILE__).'/3rd-party/yii/framework/yii.php';

$config = dirname(__FILE__).'/protected/config/main.php';


// remove the following line when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);


require_once($yii);

Yii::createWebApplication($config)->run();



There you can clearly see that I’m messing with the headers. And I know why I do it. Using an appropriate expires header helps the browser now download pages unnecessarily. But it wrecks my program if I want to send some other content type. I’m not sure what I’m going to do here…

Hmm… I’m still missing something. I made an alternate entry script (“index2.php”) without the header stuff, and it still doesn’t work. This is my alternate entry script:




<?

$yii = dirname(__FILE__).'/3rd-party/yii/framework/yii.php';

$config = dirname(__FILE__).'/protected/config/main.php';


// remove the following line when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);


require_once($yii);

Yii::createWebApplication($config)->run();



Any ideas?

Moving your headers logic into a class method is generally a good idea (like, for example, to an application-wide filter), or you may want to attach an event handler to onBeginRequest, so you can decide whether you’d like to send out headers or not.