0

I have a Laravel 5.3 application (that uses URL rewriting) that runs on nginx 1.12.1 on an Ubuntu server.

I want to create PDF report, and I find this mPDF wrapper for Laravel. My page works from the browser but throw 404 error when mPDF tries to generate PDF. After investigation, I found that it fails on this line in the html:

 <link href="/css/app.css" rel="stylesheet">

I dug into mPDF source and found out it would fail in a line like:

$content = @file_get_content("https://mywebaddr/css/app.css");

So first thing, instead of returning false everythings stops and nginx displays Error 404 (despite the fact that the url is valid, I can open my css in my browser, and despite the ignore error operator) but that is not the main problem.

And second thing why does this request fails? I checked and allow_url_fopen is ON, the request could fail with 401 Unauthorize if it was a credential issue.

I don't understand this behaviour, may someone lead me to the solution?

EDIT: My routes: (the first displays the report as a webpage and the second displays a PDF).

|        | GET|HEAD      | report/{id}                       |                    | App\Http\Controllers\ReportController@viewReport        | web                                                            |
|        | GET|HEAD      | report/{id}/download              |                    | App\Http\Controllers\ReportController@downloadReport    | web                                                            |

Note that when I remove the css include I succeed in getting my PDF, except that my images are broken. Note also that this isn't CSS that generate the 404 but mPDF PHP class that tries to file_get_content it.

Code for PDF is:

 $pdf = PDF::loadView('report.index', $datas);
 return $pdf->stream('document.pdf');
Krapow
  • 611
  • 6
  • 26
  • 404 cannot be generated from missing css as far as I know .. can you show more code? specifically the pdf generation controller/s and routes – Hamza Mohamed Mar 07 '18 at 15:02
  • @HamzaMohamed I added precisions. I think that you slightly misunderstood the problem, missing CSS is not the 404 error, file_get_content in PHP finding a file is. – Krapow Mar 07 '18 at 15:22
  • Could it be because the package cannot reach the css file from the view you are providing? – Hamza Mohamed Mar 07 '18 at 15:50
  • What do you mean? This view is the same as the controller that just displays my report and it works there – Krapow Mar 07 '18 at 15:54
  • what I mean is laravel blade generates the views .. and so does the package .. so there could be some issue there in making the view .. if the package cannot fetch the app.css try adding that to the blade file itself that you are creating directly – Hamza Mohamed Mar 07 '18 at 15:58
  • It would work but it is a workaround that will only work for css and js, but it will fail for pictures. – Krapow Mar 07 '18 at 16:08
  • well if that works and images wont work .. then the issue is with $view in [LoadView](https://github.com/niklasravnsborg/laravel-pdf/blob/2dc7281c1b6f520a018e9868c2ec9d815748ad5e/src/LaravelPdf/PdfWrapper.php#L42) – Hamza Mohamed Mar 07 '18 at 16:59
  • The call to view in LoadView is exactly the same as in the controllers that displays HTML and it works here. I say again that I looked into mPDF source and put some print in it, I am SURE it fail in a call to file_get_content. – Krapow Mar 07 '18 at 17:02
  • Then I have nothing .. but you can check [this](https://stackoverflow.com/questions/7230719/nginx-and-php-cgi-cant-file-get-contents-of-any-website-on-the-server) .. also if you can advise what was the `file_get_content` output .. then I'll leave you in peace :D – Hamza Mohamed Mar 07 '18 at 17:38

1 Answers1

0

Well I didn't succeed in resolving my 404, but as I can inline css, I can also inline my images thanks to data URI. This way my html doc will need no requests.

Krapow
  • 611
  • 6
  • 26