Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 serve open pdf file

Tags:

php

pdf

symfony

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?

like image 549
antanas_sepikas Avatar asked Jul 13 '14 10:07

antanas_sepikas


People also ask

Can we open PDF in Vim?

in vim's official website, the definition of vim is clear: vim the editor It is not pdf reader, it is not MS-Word reader. You can of course write a pdf reader with other language, e.g. Java with itext lib.

How do I open a PDF in laravel?

As of Laravel 5.2 documented under Other response types you can now use the file helper to display a file in the user's browser. You just need to send the contents of the file to the browser and tell it the content type rather than tell the browser to download it. $filename = 'test.

How do I force a PDF to open in browser?

At the top right, click More Settings. At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” (Uncheck this box if you want PDFs to open automatically when you click them.)


1 Answers

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');
like image 93
antanas_sepikas Avatar answered Oct 02 '22 18:10

antanas_sepikas