PHP base64编码pdf文件

时间:2015-12-09 15:17:47

标签: php base64

我正在使用API​​,我可以将文档发送到dropbox之类的内容。根据文档,发送的文件需要是BASE64编码数据。

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

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

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

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

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

由于

3 个答案:

答案 0 :(得分:18)

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

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:-1)

将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;
}