【问题标题】:Return MPDF result to browser using JSON and ExtJS使用 JSON 和 ExtJS 将 MPDF 结果返回到浏览器
【发布时间】:2015-01-29 17:22:11
【问题描述】:

我使用 mpdf 在我的 php 代码中创建 pdf 报告。当我使用 Output($pdfFilePath) 保存它时,我可以看到 pdf 文件,但需要使用 json 将其返回到浏览器而不将其保存在服务器上,所以我使用了 Output($pdfFilePath,"D")。下面是我的代码,但它返回一个空的 html 页面:

php 中的服务器端代码:

$report = $this->load->view('report', $data, true);

$pdfFilePath = "the_pdf_output.pdf";

$this->load->library('m_pdf');

$pdf = $this->m_pdf->load();
$pdf->SetDisplayMode('fullpage');
$pdf->WriteHTML($report);

    $result["success"] = true;
    $result["data"] =$pdf->Output($pdfFilePath,"D");
    $result["extra"] = $row->extra;

    echo json_encode($result);

extjs 中的前端:

        success: function(xhr) {

                    var response = Ext.decode(xhr.responseText);
                    if (response.success) {
                        var html =response.data.replace(/\//g, '')
                        ui_print(html);
                     } else {
                        Ext.example.msg('Error', 'Oops, there has been a problem!');
                    }
               }

【问题讨论】:

    标签: javascript php json extjs mpdf


    【解决方案1】:

    你应该输出as a string

    $result["data"] =$pdf->Output($pdfFilePath,"S");
    

    使用D是直接将数据发送到浏览器

    此外,我不确定EXT.decode 是什么,但我通常使用JSON.parse 来使用来自ajax 的json:

    var response = JSON.parse(xhr.responseText);
    console.log(response['data']);
    

    话虽如此,我不确定 pdf 文件在 json 文件中的解析效果如何......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      相关资源
      最近更新 更多