php - 从 Base64 字符串下载 PDF

标签 php pdf base64


我的情况(全部在 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/

相关文章:

python - 如何将 Python numpy 数组转换为 base64 输出

php - Yii 验证码没有改变

java - java.io.IOException : Underlying input stream returned zero bytes 的原因是什么

Python 为 S3 Post 生成的签名

javascript - 无法在 PDFjs 中指定 pdf.worker.js 的自定义路径

php - 如何在数据库中添加 PDF 文件并能够使用 phpMyAdmin 通过网站搜索它们

ruby-on-rails - 在新浏览器选项卡中打开的 PDF 文档中创建链接

php - 下拉菜单不适用于日、月和年

php - 如何使用 PHP 在 PDF 文件中查找所需文本的坐标?

php - 是否可以从 PDF 文件创建视频?