【问题标题】:PHP get pdf file from base64 encoded data stringPHP从base64编码的数据字符串中获取pdf文件
【发布时间】:2013-06-06 00:05:23
【问题描述】:

我有一个包含 pdf 的 base64 编码字符串。如何使用 php 从此编码字符串创建 .pdf 文件?

标头函数中有类似Content-type:application/pdf 的东西?

this is the base64 encoded string

【问题讨论】:

标签: php pdf header base64 encode


【解决方案1】:

有了这个,你可以创建和打开文件......但这也许不是问题的答案

$data = base64_decode($this->Get(self::Body));
file_put_contents('file.pdf',$data);

答案是用标题回显内容:

$data = base64_decode($this->Get(self::Body));
header('Content-Type: application/pdf');
echo $data;

【讨论】:

    【解决方案2】:

    试试这段代码

    $pdf_base64 = "base64pdf.txt";
    //Get File content from txt file
    $pdf_base64_handler = fopen($pdf_base64,'r');
    $pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
    fclose ($pdf_base64_handler);
    //Decode pdf content
    $pdf_decoded = base64_decode ($pdf_content);
    //Write data back to pdf file
    $pdf = fopen ('test.pdf','w');
    fwrite ($pdf,$pdf_decoded);
    //close output file
    fclose ($pdf);
    echo 'Done';
    

    【讨论】:

    • 谢谢萨米,这就是我想要的..干杯..:) 但是我的查询获得了 -Ve 投票!我的问题有什么问题吗?
    • 对于 DomPdf,我只需要 $pdf_decoded = $domPdf->output() 和从 //Write data back to pdf file 开始的行。 (它可能关心的人)
    • 效果不好,我试图与这个答案不同,但我做不到。
    猜你喜欢
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 2019-02-26
    • 2021-09-22
    • 2018-08-15
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多