Buffer PDF to Browser

Is it possible to buffer a pdf to the browser in Yii?

Or am I not doing this right?

I am calling the controller class and want it to buffer the output to prevent people from accessing the files directly.

Using this in my controller class to buffer:




   $filename='C:/wamp/www/mproject/web/doc/cat.pdf';

   $fp=fopen($filename, "rb");

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

    		

   while(!feof($fn))

   {

   	$buffer = fread($fn, 4096);

   	echo $buffer;

   }



Okay, I have created a buffer view file.

I have also created a public buffer variable in my Model.

Now in my view file I am doing this:




<?php

$filename = $model->buffer;


$fp=fopen($filename, "rb");

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


while(!feof($fp))

{

	$buffer = fread($fp, 4096);

	echo $buffer;

}

?>



It seems to be working fine, but because it is wrapped in the Yii main.php view, it is displaying it incorrectly.

6305

pdf.jpg

How to render the view file by itself and not wrapped?




return $this->render('buffer', ['model' => $model,]);



Thanks

Simply in your controller:




public function actionDownloadPdf()

{

    // ... define your $model

  


    $filename = $model->buffer;


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


    echo file_get_contents($filename);

}



PERFECTION, works great. Thanks