簡體   English   中英

無法下載或打開空白的PDF文件PHP

[英]not able to download or open blank PDF file PHP

在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");

代碼正在工作:

  • 當PDF報告包含數據時
  • 當大小大於1 kb時
  • 在Windows機器

不起作用:

  • 當報告不包含數據或大小以字節為單位時。
  • 它在瀏覽器的新選項卡中生成二進制字符串,而不是生成空白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();

我將以這種方式這樣做,以避免瀏覽器應用程序和緩存出現一些問題。 試試看:

<?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();

編輯:
如果確實有必要使用瀏覽器的pdf插件,而不是立即使用關聯的默認pdf閱讀器下載並打開它,請更換

header("Content-type: application/force-download");

header("Content-type: application/pdf");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM