Symfony2 + nginx提供pdf文件?

时间:2014-05-09 14:03:49

标签: php symfony pdf nginx response

我在控制器Symfony2中尝试show file pdf,我的行动是:

/**
 * @Route("/user/manual/pdf", name="helps_user_manual")
 */
public function userManualAction() {
    $em = $this->getDoctrine()->getManager();
    $file = $em->getRepository('AllusInventoryBundle:Files')->findOneByHashFile('4b2edce2ae98a2a9915d7a1f8cb482f5');

    $response = new Response();
    $response->setStatusCode(Response::HTTP_OK);
    $response->headers->set('Content-Type', 'application/pdf');
    $response->headers->set('Content-Disposition', 'inline');
    $response->headers->set('Content-Transfer-Encoding', 'binary');
    $response->headers->set('Accept-Ranges', 'bytes');
    $response->setContent(readfile($file->getAbsolutePath('pdf')));

    return $response;
}

我在nginx中的配置是:

http {
server_names_hash_bucket_size 64;
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout  0;
keepalive_timeout 65;
server_tokens off;

gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_http_version 1.1;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 2;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/json  application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x

# enabled sites
include /etc/nginx/sites-enabled/*.conf;

}

在我的开发环境中,我正在使用apache并成功显示pdf。但在我的生产环境中,我向您展示了以下内容:

  

%PDF-1.5%μμμμ10obj&gt;&gt; endobj 2 0 obj&lt;&gt; endobj 3 0 obj&lt;&gt; / XObject&lt;&gt; / ProcSet [/ PDF / Text / ImageB / ImageC / ImageI]&gt;&gt; / MediaBox [0 0 595.32 841.92] / Contents 4 0 R / Group&lt;&gt; /标签/ S / StructParents 0&gt;&gt; endobj 4 0 obj&lt;&gt;流xœμXÛn7}à£À¥ØNR8€UÛÃ, - »* l©u“Ío÷:ÜμlIËá2U<]™{öÌýÒœ&lt;®················ éíxBÑt^Ȭ~&gt;ŒGJÜ¥Ë÷ãÑå'~\‰‡èè™™ÞÞ¼¢ƒƒZÁμïìžï.Γh&gt;&amp;rgço...Ú”°°ZÚ,Ò«0€Åð!H_ ...¡yoe ¬Â0&lt;†JJ}XÃzilòòH¬ÂpiŒQuü:À'ê:~1J [Â/lJuüFÀ¨UlØ÷þÎÊB€Uhûñõ«l¥²-¿Ð2''Ê¡-¿Ñ2ÞIWÂ{±qZ†g196 / W ·UA $ðÏßzÔý

Nginx不接受'Content-Type','application / pdf',感谢您的帮助。

0 个答案:

没有答案