我正在使用laravel。我不想将pdf的网址返回到前端,因为它位于google cloud中,我也不想看到链接。这就是为什么我需要直接返回pdf并让用户通过浏览器下载它的原因。
我尝试过:
return response()->json(['data' => file_get_contents(Storage::url($cert_path))]);
//this says: malformed utf-8 characters, 500 error
然后我用谷歌搜索并尝试了此操作:
$file = file_get_contents(Storage::url($cert_path));
$file = mb_convert_encoding($file, 'HTML-ENTITIES', "UTF-8");
return response()->json(['data'=>$file]);
//this returns pdf, but in front, when i have a download code of js, when I download it, the whole pdf is empty.
pdf的URL是正确的,因为我可以转到该URL并查看pdf。
如何成功返回pdf?
我还将包括javascript下载代码:
var blob = new Blob([pdf], {
type: 'application/pdf'
});
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "ffa.pdf";
link.click();