dongtiao0279
2018-08-31 10:25
浏览 265

Laravel - pdf文件的响应返回奇怪的文本而不是在浏览器中显示pdf

after two days of reading and looking for solutions and other possible ways I finally encouraged myself to ask for help here. A lot of people have asked similar questions, I have used ALL of their solutions and a lot of them helped, up to this point. I'm completely stuck at this point.

To my problem: I want to upload pdf files to the storage directory and afterwards make them accessable for users clicking on a simple button (using jquery ajax post request). Uploading works fine and the PDF ends up in the right directory, as the title states, returning the file works aswell, but instead of displaying the pdf in the browser, it simply returns a for most parts weirdly encoded string starting with the following:

%PDF-1.3 %âãÏÓ

My jQuery Code:

$('input.btn-pdf').click(function () {
    event.preventDefault();
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax({
        url: '/big-picture/getPDF',
        type: 'POST',
        data: {'filename':$(this).val()},
        success: function (data) {
        }
    });
});

After clicking the button as a user the PDF's shouldn't be downloaded immediately, therefore I consulted the Laravel (5.6) Documentation and used the appropriate response method as follows in my controller:

 public function getPDF(Request $request)
{
    $headers = [
        'Access-Control-Allow-Origin' => '*',
        'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, PATCH, DELETE',
        'Access-Control-Allow-Headers' => 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers',
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline'
        ];
    return response()->file(storage_path("app\pdfs\\".$request->filename), $headers);
}

As I said, it finds the file and I don't get any errors (status code 200). I couldn't find any information of me having to do anything in the view or in the success area of my jquery ajax call after return the file as a response in order to display it correctly. I also checked the filetype of the file before returning it and it correctly said 'application/pdf'. I have gotten a lot of error messages first, then I found out that I have to use the right headers to make it work, which I did. Now it should work, but it doesn't.

This is the response header:

Accept-Ranges: none Access-Control-Allow-Headers: Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, PATCH, DELETE Access-Control-Allow-Origin: * Cache-Control: public Connection: close Content-Disposition: inline Content-Length: 1650653 Content-Type: application/pdf Date: Fri, 31 Aug 2018 09:51:04 GMT Date: Fri, 31 Aug 2018 09:51:04 +0000 Host: localhost:8000 Last-Modified: Thu, 30 Aug 2018 09:30:39 GMT Set-Cookie: XSRF-TOKEN=aLongTokenHere; expires=Fri, 31-Aug-2018 11:51:04 GMT; Max-Age=7200; path=/ Set-Cookie: laravel_session=aLongSessionIdHere; expires=Fri, 31-Aug-2018 11:51:04 GMT; Max-Age=7200; path=/; httponly X-Powered-By: PHP/7.2.4

Thank you in advance. I'm not only interested in a solution for this problem, but if someone has a different idea of smoothly and intelligently storing PDF's for my particular use case and returning/displaying them I'm more than open to change my way of doing it.

All of you have a nice day.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushichi3678 2018-09-08 12:55
    已采纳

    My Workaround:

    Instead of using the local storage facility of laravel, I will use Drop Box to store the PDF files and simply put the display links to the PDF's n my database, which will later be passed to my view.

    Not the solution I was hoping to end up with but it works. Maybe this is helpful for others.

    打赏 评论

相关推荐 更多相似问题