我的情况(全部在 PHP 中):
我得到的:来自 API 的 Base64 编码字符串。
我想要的:单击该链接可将此文档下载为 PDF。 (我确定它永远是一个 PDF 文件)
我试过这个:
$decoded = base64_decode($base64);
file_put_contents('invoice.pdf', $decoded);
但我有点迷路了,似乎无法在解码后找到下载它的方法。
我希望有人能帮助我,在此先感谢!
最佳答案
这里的例子似乎很有帮助:http://php.net/manual/en/function.readfile.php
在你的情况下:
<?php
$decoded = base64_decode($base64);
$file = 'invoice.pdf';
file_put_contents($file, $decoded);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
这应该会强制进行下载。
关于php - 从 Base64 字符串下载 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34698016/