【问题标题】:PHP base64 encode a pdf filePHP base64编码pdf文件
【发布时间】:2018-03-15 04:01:10
【问题描述】:

我正在使用一个 API,我可以在其中将文档发送到 Dropbox 之类的东西。根据文档,发送的文件需要是BASE64编码的数据。

因此,我正在尝试这样的事情

$b64Doc = chunk_split(base64_encode($this->pdfdoc));

$this->pdfdoc 是我的 PDF 文档的路径。

目前,文件正在发送,但似乎无效(不显示任何内容)。

我是否正确地将我的 PDF 转换为 BASE64 编码数据?

谢谢

【问题讨论】:

    标签: php base64


    【解决方案1】:

    base64_encode 接受字符串输入。所以你所做的就是对路径进行编码。你应该抓取文件的内容

    $b64Doc = chunk_split(base64_encode(file_get_contents($this->pdfdoc)));
    

    【讨论】:

      【解决方案2】:

      base64_encode() 将编码您传递给它的任何字符串。如果您传递的值是文件名,那么您将得到的只是一个编码的文件名,而不是文件的内容。

      您可能想先做file_get_contents($this->pdfdoc) 或其他事情。

      【讨论】:

        【解决方案3】:

        将base64转换为pdf并保存到服务器路径。

        // Real date format (xxx-xx-xx)
        $toDay   = date("Y-m-d");
        
        // we give the file a random name
        $name    = "archive_".$toDay."_XXXXX_.pdf";
        
        // a route is created, (it must already be created in its repository(pdf)).
        $rute    = "pdf/".$name;
        
        // decode base64
        $pdf_b64 = base64_decode($base_64);
        
        // you record the file in existing folder
        if(file_put_contents($rute, $pdf_b64)){
            //just to force download by the browser
            header("Content-type: application/pdf");
        
            //print base64 decoded
            echo $pdf_b64;
        }
        

        【讨论】:

        • 这不能回答问题。当 OP 关于编码时,您正在解码
        猜你喜欢
        • 2018-06-13
        • 2017-10-07
        • 1970-01-01
        • 2013-06-06
        • 2014-09-12
        • 2013-01-13
        • 2010-09-17
        • 2021-04-24
        • 1970-01-01
        相关资源
        最近更新 更多