Php 如何从编码字符串内容创建pdf文件-Codeigniter

Php 如何从编码字符串内容创建pdf文件-Codeigniter,php,codeigniter,pdf,Php,Codeigniter,Pdf,我得到了来自cURL的响应,这是一种编码的字符串格式的pdf(我猜)-见附图 我得到的pdf文件,如果我直接把网址在浏览器中。由于它要求api凭据来查看pdf,因此它对用户不友好,我正在寻找另一种直接下载该文件的方法。 因此,我试图用cURL获取pdf文件的字符串内容,我得到的正是所附图像中的内容(仅附加了一小部分) 使用该字符串内容,我试图将其保存为一个普通的txt文件,并从中尝试解码字符串文本并新建一个pdf文件。创建后,我需要下载相同的 我可以用从cURL获得的字符串数据创建文本文件,也

我得到了来自cURL的响应,这是一种编码的字符串格式的pdf(我猜)-见附图

我得到的pdf文件,如果我直接把网址在浏览器中。由于它要求api凭据来查看pdf,因此它对用户不友好,我正在寻找另一种直接下载该文件的方法。 因此,我试图用cURL获取pdf文件的字符串内容,我得到的正是所附图像中的内容(仅附加了一小部分)

使用该字符串内容,我试图将其保存为一个普通的txt文件,并从中尝试解码字符串文本并新建一个pdf文件。创建后,我需要下载相同的

我可以用从cURL获得的字符串数据创建文本文件,也可以创建pdf。但是显示
的下载文件无法加载PDF文档。

下面是我尝试过的代码,我不确定我是否正确尝试过

function pdf_download(){
    $this->load->helper('file');
    $curl = $this->api_call->callapi('GET',APIURL."carts/96171/tickets");

    $content = $curl;
    $my_file = FCPATH . '/document/text.txt';

    if (write_file($my_file, $content) == FALSE)
    {
            echo 'Unable to write the file';
    }
    else
    {
            echo 'File written!';
    }


    $pdf_base64 = $my_file;
    //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_file=FCPATH . '/document/ticket.pdf';
    $pdf = fopen ($pdf_file,'w');
    fwrite ($pdf,$pdf_decoded);//Creating a pdf from the encoded content in txt file
    fclose ($pdf);

    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=tickets.pdf");
    ob_clean(); flush(); 
    readfile($pdf_file);//downloading the pdf file
    exit();

}

由于将下载流式传输到浏览器需要发送头文件,因此在发送头文件之前不得输出任何内容。因此,你必须摆脱:

if (write_file($my_file, $content) == FALSE)
{
        echo 'Unable to write the file';
}

else
{
        echo 'File written!';
}
如果您确实需要检查文件是否已写入(我假设是为了调试目的),您可以使用CI的
log\u message()
在日志中写入调试条目,或者只设置一个控制变量。例如:

if (write_file($my_file, $content) == FALSE)
{
        log_message('debug', "Unable to write file");
        $file_written = false;
}

else
{
        log_message('debug', "File succesfully written");
        $file_written = true;
}
此外,还缺少一些头文件,以确保文件不会流式传输到浏览器,而是发送供下载,还有一些头文件是确保文件正确下载所必需的

header('Content-Description: File Transfer');
header('Content-Type: '. mime_content_type($pdf_file)); // since you're using Codeigniter, this will try to use the correct mimetype
header('Content-Disposition: attachment; filename="ticket.pdf"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pdf_file));
readfile($pdf_file);

由于将下载流式传输到浏览器需要发送头文件,因此在发送头文件之前不得输出任何内容。因此,你必须摆脱:

if (write_file($my_file, $content) == FALSE)
{
        echo 'Unable to write the file';
}

else
{
        echo 'File written!';
}
如果您确实需要检查文件是否已写入(我假设是为了调试目的),您可以使用CI的
log\u message()
在日志中写入调试条目,或者只设置一个控制变量。例如:

if (write_file($my_file, $content) == FALSE)
{
        log_message('debug', "Unable to write file");
        $file_written = false;
}

else
{
        log_message('debug', "File succesfully written");
        $file_written = true;
}
此外,还缺少一些头文件,以确保文件不会流式传输到浏览器,而是发送供下载,还有一些头文件是确保文件正确下载所必需的

header('Content-Description: File Transfer');
header('Content-Type: '. mime_content_type($pdf_file)); // since you're using Codeigniter, this will try to use the correct mimetype
header('Content-Disposition: attachment; filename="ticket.pdf"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pdf_file));
readfile($pdf_file);

上述给定代码中的错误行是
$pdf\u decoded=base64\u decode($pdf\u content)

应将其更改为
$pdf\u decoded=$pdf\u content

因为编码格式的字符串已经得到创建
PDF
所需的字符串。所以不需要解码

此外,上述代码缩减如下

function pdf_download(){
    $this->load->helper('file');
    $this->load->helper('download');
    $cart_id=$this->input->get('cart_id');
    $curl = $this->api_call->callapi('GET',APIURL."cart/'.$cart_id.'/tickets");
    $pdf_file=FCPATH . 'document\voucher-'.$cart_id.'.pdf';
    if (write_file($pdf_file, $curl))
    { 
        force_download($pdf_file, NULL);
    }
}

上述给定代码中的错误行是
$pdf\u decoded=base64\u decode($pdf\u content)

应将其更改为
$pdf\u decoded=$pdf\u content

因为编码格式的字符串已经得到创建
PDF
所需的字符串。所以不需要解码

此外,上述代码缩减如下

function pdf_download(){
    $this->load->helper('file');
    $this->load->helper('download');
    $cart_id=$this->input->get('cart_id');
    $curl = $this->api_call->callapi('GET',APIURL."cart/'.$cart_id.'/tickets");
    $pdf_file=FCPATH . 'document\voucher-'.$cart_id.'.pdf';
    if (write_file($pdf_file, $curl))
    { 
        force_download($pdf_file, NULL);
    }
}

您是否尝试过使用CI的内置下载助手,而不是像上面那样构建“强制下载”方法?它有一个方便的
force_download()
方法,可以为您解决所有这些问题,就像charmHi@javierlaroulet一样工作,谢谢!。我也试过了。但结果是一样的。正在下载pdf文件,但显示“错误”。无法加载PDF文档。其已修复。实际上,我做错的是我试图解码字符串,我用这个解码字符串创建pdf。非常感谢您的宝贵意见@javierlaroulet。您是否尝试过使用CI的内置下载帮助程序,而不是像上面那样构建“强制下载”方法?它有一个方便的
force_download()
方法,可以为您解决所有这些问题,就像charmHi@javierlaroulet一样工作,谢谢!。我也试过了。但结果是一样的。正在下载pdf文件,但显示“错误”。无法加载PDF文档。其已修复。实际上,我做错的是我试图解码字符串,我用这个解码字符串创建pdf。非常感谢您的宝贵评论@Javierlaroulet。