remove theme from controller action to output PDF

I am trying to use TCPDF to output PDF files to the browser.

To do this, I need to disable all themes so the PDF is send without ANY output from yii before the PDF starts sending.

I thought I could do this with:

$this->layout = false;

But it is still giving me the error

TCPDF ERROR: Some data has already been output, can't send PDF file

I also tried

public $layout = '';


public $layout = false;

and several other variations with no change.

It looks like TCPDF is working otherwise…

How can be sure YII2 only outputs the file I need? Something is sending out the text/html headers and spoiling the PDF transfer.



I haven’t tried this, but you should probably set the header?

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

There are dedicated Yii2 extensions to deal with PDF, by the way.

TCPDF is trying to send new headers and the browser is complaining about the fact that headers have already been sent. That is why I need to figure out how to keep YII2 from sending anything.

There are 4,495 ( roughly… ), extensions to deal with PDF. Do you have one to two that you actively use and recommend?



You to to teach Yii what the correct reponse is, of course.

I would have posted some code, but this editor is horrible :)

Personally I`m using this to send pdf file to the browser, then user can download or open the file in browser if he have plugin installed.

Yii::$app->response->sendFile($path, $name, ['inline' => false])->send();

To render/create and so on PDF files im using this extension and im very happy with it:

MPDF looks like a popular package, but I don’t need/want to turn html into a pdf.

I actually need to generate a PDF file with very specific layout requirements that I am importing from an older system. While I could probably reformat the reports into html and print them, I already have the layout information in a format ready for page layout and converting the reports would be a very time consuming project.

TCPDF looks like it gives me all the control of the layout that I need. Plus, I can sent it html later if I need to.

I just need to figure out how to keep YII from spewing out anything before the content/pdf headers.



Did you look at what the yii2-pdf guys are doing in their code?

It shows exactly how to stop Yii from sending any wrong headers. ;)