【问题标题】:How to create a pdf file from encoded string content - Codeigniter如何从编码的字符串内容创建 pdf 文件 - Codeigniter
【发布时间】:2019-12-26 12:54:36
【问题描述】:

我收到来自 cURL 的响应,它是 pdf 的编码字符串格式(我猜) - 见附图。

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

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

我可以使用从 cURL 获得的字符串数据创建文本文件,也可以创建 pdf。但是下载的文件显示Failed to load PDF document.

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

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();

}

【问题讨论】:

  • 不是像上面那样构建“强制下载”的方法,您是否尝试过使用 CI 的内置下载助手?它有一个方便的 force_download() 方法,可以为您处理所有这些,并且像魅力一样工作
  • 嗨@JavierLarroulet,谢谢!我也试过了。但结果是一样的。正在下载 pdf 文件,但显示“错误。无法加载 PDF 文档。'
  • 它已修复。实际上我做错的是我试图解码字符串并且我使用这个解码的字符串来创建pdf。非常感谢您宝贵的 cmets @JavierLarroulet。

标签: php codeigniter pdf


【解决方案1】:

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

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

else
{
        echo 'File written!';
}

如果您确实需要检查文件是否已写入(我假设是出于调试目的),您可以使用 CI 的 log_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);

【讨论】:

    【解决方案2】:

    上面给出的代码中的错误行是$pdf_decoded = base64_decode ($pdf_content);

    应该改成$pdf_decoded = $pdf_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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-24
      • 2022-11-22
      • 2017-06-19
      • 2018-07-10
      • 2017-04-29
      • 2021-03-04
      • 2015-06-10
      • 1970-01-01
      相关资源
      最近更新 更多