【问题标题】:How to get pdf from decoded base64 string如何从解码的base64字符串中获取pdf
【发布时间】:2020-01-21 04:46:28
【问题描述】:

我如何从这个数组中下载 pdf,这是我当前代码的样子,我无法保存 pdf。谢谢你的帮助。 这是我当前的代码:

$result = $SoapClient4->getContractBill($contractId, 2019, '09');
    var_dump($result);
    #$base64data = preg_replace('#data:application/pdf/[^;]+;base64,#', '', $result['ContractBills'][0]['RawFileData']);
    $decoded = base64_decode($base64);
    $file = 'invoice.pdf';
    $file_put_contents($file, $decoded);

    $pdf_decoded = base64_decode($base64data);
    $pdf = fopen('test.pdf','w');
    fwrite($pdf,$pdf_decoded);
    fclose($pdf);

【问题讨论】:

  • 您需要使用rawFileData并删除开头的base64,
  • @MarkusZeller 有我应该如何做的例子吗?
  • 如果我的回答对你有用,请标记为正确。

标签: php base64 encode


【解决方案1】:

试试这个:

  • 删除“base64”(7 个字符)
  • 解码字符串
  • 写入文件
file_put_contents('test.pdf', base64_decode(substr($result['ContractBills'][0]->rawFileData, 7)));

请注意键的大小写正确,因为它们区分大小写。 'RawFileData' !== 'rawFileData'.

EDIT在您的评论问题之后:

你可以用

替换你的代码
$result = $SoapClient4->getContractBill($contractId, 2019, '09');
file_put_contents('invoice.pdf', base64_decode(substr($result['ContractBills'][0]->rawFileData, 7)));

就是这样。确保您以正确的路径编写 invoice.pdf。

单线的作用与以下相同:

$result = $SoapClient4->getContractBill($contractId, 2019, '09');
$file = 'invoice.pdf';
$base64raw = $result['ContractBills'][0]->rawFileData;
$base64 = substr($base64raw, 7);
$decoded = base64_decode($base64);
file_put_contents($file, $decoded);

所以你可以决定你喜欢哪种方式。

【讨论】:

  • 感谢@Markus 的帮助,我有点困惑,如何将它放在我的代码中?
  • 我插入了所有最后的代码,我得到了这个错误:未捕获的错误:不能使用 stdClass 类型的对象作为数组
  • 啊,是的,$result['ContractBills'] 包含一个带有对象的数组。我会更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2013-06-06
  • 1970-01-01
  • 2021-12-12
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多