如何在Laravel中重定向到生成的PDF文件。

huangapple go评论58阅读模式
英文:

How to redirect to a generated PDF in Laravel

问题

我正在使用 mikehaertl\pdftk\Pdf 在 Laravel 中动态生成 PDF。

PDF 被成功生成,并且我可以在 storage/app/public 目录中看到它。

然而,我希望当用户点击触发按钮时,不仅生成 PDF,而且在浏览器中打开 PDF(如果支持),或者下载到他们的设备上。

这是我目前的代码:

$fileName = "fillable-" . $user->unique_id . ".pdf";
$pdf = new Pdf(public_path().'/pdfs/fillable.pdf');
$result = $pdf->fillForm([
   'form_field1' => 'John',
   'form_field2' => 'Smith',
   'form_field3' => '07-14-1983',
   'form_field4' => true
])->needAppearances()->saveAs(storage_path().'/app/public/pdfs/'.$fileName);

if ($result === false) {
   $error = $pdf->getError();
   echo $error;
} else {
   //这是我想要重定向/下载 PDF 的地方

   //*** 失败的方法 #1 ***
   //return response()->download($path); //这会下载一个文件 - 但是 PDF 有问题


   //*** 失败的方法 #2 ***
   //return redirect(storage_path().'/app/public/pdfs/'.$fileName); //这只会导致 404 找不到
}

正如我提到的 - PDF 已经生成,所以它存在于文件系统中。我只需要知道如何将用户重定向到它。

英文:

I am using mikehaertl\pdftk\Pdf in order to dynamically generate a PDF in Laravel.

The PDF is getting generated fine, and I see it in my storage/app/public directory.

However, I want to make it so that when the user clicks on the trigger button, it not only generates the PDF, but then proceeds to either opens the PDF in the browser (if it is supported), or download it to their device.

Here is the code I have so far:

$fileName = "fillable-".$user->unique_id.".pdf";
$pdf = new Pdf(public_path().'/pdfs/fillable.pdf');
$result = $pdf->fillForm([
   'form_field1' => 'John',
   'form_field2' => 'Smith',
   'form_field3' => '07-14-1983',
   'form_field4' => true
])->needAppearances()->saveAs(storage_path().'/app/public/pdfs/'.$fileName);

if ($result === false) {
   $error = $pdf->getError();
   echo $error;
} else {
   //This is where I want to redirect / download the PDF

   //*** Failed Method #1 ***
   //return response()->download($path); //This downloads a file – but, the PDF is "broken"


   //*** Failed Method #2 ***
   //return redirect(storage_path().'/app/public/pdfs/'.$fileName); //This just gives a 404 not found
}

As I mentioned – the PDF is getting generated, so it exists inside of the file system. I just need to know how I can redirect the user to it.

Anybody have any thoughts on how I could pull this off?

答案1

得分: 1

以下是翻译好的部分:

$fileName = "fillable-" . $user->unique_id . ".pdf";
$tempFilePath = storage_path() . "/app/public/pdfs/" . $fileName;
return response()->download($tempFilePath, $fileName)->deleteFileAfterSend();
英文:

For anybody else that might have a similar question – this worked for me:

$fileName = "fillable-".$user->unique_id.".pdf";
$tempFilePath = storage_path()."/app/public/pdfs/".$fileName;
return response()->download($tempFilePath, $fileName)->deleteFileAfterSend();

huangapple
  • 本文由 发表于 2023年7月13日 23:07:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定