Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display PDF file on yii2

Tags:

pdf

display

yii2

I have a audit table and audit_report field.Field type is text .I saved pdf files into folder and saved name to database.I tried to display the pdf on view page. but the box with image sign only getting. I could display jpeg and png files nicely.How to display PDF on view page of yii2 framework.

like image 245
HarshEcho Avatar asked Dec 03 '25 12:12

HarshEcho


2 Answers

This would work,

return Yii::$app->response->sendFile($completePath, $filename, ['inline'=>true]);

Input the function with third param as array of value 'inline'=>true to open the file within the browser window. See the documentation here sendFile()

like image 196
nithin_cs Avatar answered Dec 05 '25 07:12

nithin_cs


Although the question has been answered there is one thing that needs to be addressed if you have the file content/stream instead of the file path, like for instance you are using Dropbox API and you receive a stream from the API and want to display that file instead of forcing the browser to download.

For this case you can use the sendContentAsFile when attempting to display the PDF file in the browser you will need to specify the mimeType option too along with the "inline"=>true because the default mimeType value is set to application/octet-stream which is used to download a file and to display inline in browser you need to change it to application/pdf.

return Yii::$app->response->sendContentAsFile(
       $fileContent, 
       $filename, 
       ['inline' => true, 'mimeType' => 'application/pdf']
);
like image 24
Muhammad Omer Aslam Avatar answered Dec 05 '25 06:12

Muhammad Omer Aslam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!