How to display PDF file on yii2

15,629

Solution 1

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()

Solution 2

You could add a button that opens the file in a new tab, but make it link to an action in your controller that returns the file instead of the direct path to the file:

In your view:

<?= Html::a('PDF', [
    'controller/pdf',
    'id' => $model->id,
], [
    'class' => 'btn btn-primary',
    'target' => '_blank',
]); ?>

In your controller:

public function actionPdf($id) {
    $model = ModelClass::findOne($id);

    // This will need to be the path relative to the root of your app.
    $filePath = '/your/file/path';
    // Might need to change '@app' for another alias
    $completePath = Yii::getAlias('@app'.$filePath.'/'.$model->fileName);

    return Yii::$app->response->sendFile($completePath, $model->fileName);
}

Aliases - Key Concepts - The Definitive Guide to Yii 2.0

Solution 3

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']
);
Share:
15,629
HarshEcho
Author by

HarshEcho

Updated on July 24, 2022

Comments

  • HarshEcho
    HarshEcho almost 2 years

    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.

  • HarshEcho
    HarshEcho over 7 years
    Thanks for the reply.I tried this.It is going to that page with Id.But i got "404 page not found error ".
  • HarshEcho
    HarshEcho over 7 years
    now i am getting this link localhost/echosoftware/backend/web/index.php?r=AuditControll‌​er%2Fpdf&id=3 Instead of this i need to get this localhost/echosoftware/backend/web/uploads/AUDIT/Achies-2015‌​.pdf..If i get this pdf file will display.
  • marche
    marche over 7 years
    The button needs to link to the pdf action in your controller, with the id of your model that holds the file name. That way the action can get the file name from the model and return it as the response. You might need to use aliases to build the file full path, so i will update that.
  • HarshEcho
    HarshEcho over 7 years
    This function is pointing to correct file.But i am getting "PHP warning.fopen(........./web/uploads/AUDIT/Achies-2015.pdf): failed to open stream: No such file or directory." This file is excisting there.I checked the permission of file.It is 755.
  • rob006
    rob006 about 5 years
    It should detect MIME type correctly as long as file has correct extension: github.com/yiisoft/yii2/blob/…
  • Muhammad Omer Aslam
    Muhammad Omer Aslam about 5 years
    @rob006 i was talking about viewing the file inside a browser when you have the file content instead of a link to file or file name, for instance using a Dropbox API you get the file content from the API and you need to display that PDF file in the browser rather than forcing the browser to download, and for viewing the file in the browser you have to specify application/pdf instead of application/octet-stream with Content-Disposition: inline i was working with it today when i came along this thread and it didnt displayed until i set the mimeType manually.
  • Muhammad Omer Aslam
    Muhammad Omer Aslam about 5 years
    try using the content from this pastebin , it is a PDF file content which is base64_encoded just decode it when providing in the sendContentAsFile() and you will get , i am using php 7.2 by the way
  • rob006
    rob006 about 5 years
    Oh, sorry, somehow I missed first paragraph of your answer and assumed that you want to serve file from local filesystem (since this is what OP asking). :P