使用 JSON 和 ExtJS 将 MPDF 结果返回给浏览器

Return MPDF result to browser using JSON and ExtJS

本文关键字:结果 返回 浏览器 MPDF JSON ExtJS 使用      更新时间:2023-09-26

我在我的php代码中使用了mpdf来创建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!');
                    }
               }

你应该输出为字符串

$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 文件中的解析效果如何......