某些.pdf文件下载在浏览器中损坏 / Acrobat Pro - 使用 Laravel 和 AWS S3



我使用 Laravel 5.2 成功将.pdf文件上传到我的 AWS S3 存储桶。我可以直接转到 AWS S3 上的存储桶并毫无问题地下载每个.pdf。当我尝试使用以下代码下载某些 .pdf 时,浏览器或 Acrobat Pro 无法打开该文件。这是代码:

$file = Storage::disk('s3')->get($myfile);
// Set headers and force download
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=file.pdf");
echo $file;

在浏览器中,我收到以下错误:Failed to load PDF document在 Acrobat Pro 中,我收到以下错误:There was an error opening this document. The file is damaged and could not be repaired.

该.pdf可以在其他Apple程序中成功打开,例如预览或略读。此外,旧.pdf文件可以成功上传和下载。

虽然我不清楚以前使用 echo $file; 的问题是什么,但现在当修改为使用 Laravel 方法时,这在 return Response::make($file, 200, $headers); 中起作用。

// Get the file from s3
$file = Storage::disk('s3')->get($myfile);
// Set headers
$headers = array(
    'Content-type' => 'application/pdf',
    'Content-Disposition'   => 'attachment; filename=filename=file.pdf"
);
// Download
return Response::make($file, 200, $headers);

最新更新