16

I need to serve pdf files through the controller, but I can't make them open in browser window. When I send response with pdf file, it renders gibberish, or if I follow example at http://symfony.com/doc/current/components/http_foundation/introduction.html#serving-files it downloads the file, but I need to open it in view mode, how can I do that?

Here's what I'v got so far:

return new Response(readfile('/file/path/file.pdf'), 200,
    array('Content-Type' => 'application/pdf')
);

Am I missing something, or maybe there's something wrong with my php or apache configuration?

1
  • 4
    try to set Content-Disposition: inline; filename.pdf Header. Commented Jul 13, 2014 at 10:08

2 Answers 2

45

Found a solution:

Add BinaryFileResponse before controller class declaration from example

use Symfony\Component\HttpFoundation\BinaryFileResponse;

In controller action return BinaryFileResponse

return new BinaryFileResponse('/file/path/file.pdf');
Sign up to request clarification or add additional context in comments.

Comments

-5

I have the same issue

Try to add this :

use \Symfony\Component\HttpFoundation\Response;
.......
return  $response->headers->set('Content-type' , 'application/pdf');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.