[英]Symfony2 serve open pdf file
I need to serve pdf files through the controller, but I can't make them open in browser window. 我需要通过控制器提供pdf文件,但我无法在浏览器窗口中打开它们。 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?
当我用pdf文件发送响应时,它会呈现乱码,或者如果我按照http://symfony.com/doc/current/components/http_foundation/introduction.html#serving-files中的示例,它会下载文件,但我需要在查看模式下打开它,我该怎么办?
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? 我错过了什么,或者我的php或apache配置有问题吗?
Found a solution: 找到了解决方案:
Add BinaryFileResponse before controller class declaration from example 在示例中的控制器类声明之前添加BinaryFileResponse
use Symfony\Component\HttpFoundation\BinaryFileResponse;
In controller action return BinaryFileResponse 在控制器操作中返回BinaryFileResponse
return new BinaryFileResponse('/file/path/file.pdf');
I have the same issue 我有同样的问题
Try to add this : 尝试添加:
use \Symfony\Component\HttpFoundation\Response;
.......
return $response->headers->set('Content-type' , 'application/pdf');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.