Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法下载或打开空白的PDF文件_Php_Firefox_Pdf - Fatal编程技术网

Php 无法下载或打开空白的PDF文件

Php 无法下载或打开空白的PDF文件,php,firefox,pdf,Php,Firefox,Pdf,在firefox(ubuntu机器)中从应用程序生成PDF报告时,我遇到了一些问题 我的代码是: <?php $path = "path_to_file" ; $file = "test.pdf"; header('Content-type: application/pdf'); header("Content-disposition: attachment; filename=$file"); readfile($path); return new Response("Success")

在firefox(ubuntu机器)中从应用程序生成PDF报告时,我遇到了一些问题

我的代码是:

<?php
$path = "path_to_file" ;
$file = "test.pdf";
header('Content-type: application/pdf');
header("Content-disposition: attachment; filename=$file");
readfile($path);
return new Response("Success");

我会这样做,以避免浏览器应用程序和缓存出现一些问题。试一试:

<?php
$path = "path_to_file" ;
$file = "test.pdf";

$content = file_get_contents($path);
header("Content-disposition: attachment; filename=\"".$file."\"");
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".strlen($content));
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
echo $content;
die();


得到我的答案。这可能会帮助其他人

<?php
$path = "path_to_file" ;
$file = "test.pdf";
header("Content-disposition: attachment; filename= $file"); //Tell the filename to the browser
header("Content-type: application/pdf");//Get and show report format 
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: bytes");
readfile($path); //Read and stream the file
get_curret_user();

谢谢@hellcode提供的宝贵解决方案。它直接下载了文件,但我也需要在浏览器中打开…请帮忙@adarsha hota:查看我的编辑,注意不要在问题中使用标题的换行符(\n)不起作用:当报告不包含任何数据时-如果您的意思是文档没有页面,那么这就是您的问题,PDF查看器通常需要PDF才能有页面。如果您不确定,请共享一个示例非工作PDF.Hi@mkl。。我对两种不同的方法使用相同的代码。当$path='/var/www/output'时,我能够读取文件(即使是空白文件)…但当$path位置在本地服务器之外,即,'/home/user/Documents/output'时,无法读取文件。请建议..@adarsha hota:那么这是一个open_basedir限制或简单的权限问题。@hellcode:从我的应用程序下载pdf时,将“user:group”更改为“www data:www data”而不是“user:user”。可能是因为这个原因,它无法读取/打开文件。
header("Content-type: application/pdf");
<?php
$path = "path_to_file" ;
$file = "test.pdf";
header("Content-disposition: attachment; filename= $file"); //Tell the filename to the browser
header("Content-type: application/pdf");//Get and show report format 
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: bytes");
readfile($path); //Read and stream the file
get_curret_user();