【问题标题】:Create PDF with Ajax使用 Ajax 创建 PDF
【发布时间】:2019-02-09 16:18:54
【问题描述】:

我正在使用 FPDF 创建我的报告。

$pdf = new Report('P','mm','A4', $_POST);
$pdf->AddPage();
$pdf->Output('file.pdf','I');

我使用 ajax 来请求 PHP。

$.ajax({
        type: 'POST',
        url: '../reports/report.php',
        data: { id: id }
    }).done(function(data){         
        window.open(data);           
    })

我想在新标签中显示报告

【问题讨论】:

  • 有什么问题???
  • 不行,问题很清楚。我需要在执行 Ajax 之后显示一个 pdf,但返回的不是 html。
  • 这个问题与 stackoverflow.com/questions/26837197/open-fpdf-in-new-tab 不同,因为我的 FPDF 它来自 ajax 请求而不是一个简单的 pdf 文件。
  • 这个问题展示了如何让你想做的事情发挥作用。你的想法在逻辑上有点偏离 - 你需要打开标签并“拉”信息,而不是“推”标签打开 - 请参阅链接....

标签: javascript php ajax fpdf


【解决方案1】:

我认为你应该是成功的关键。所以会是这样的

$.ajax({
        type: 'POST',
        url: '../reports/report.php',
        data: { id: id },
        success: function(data){
         var blob = new Blob([data]);
         window.open(URL.createObjectURL(blob));
       }
    })

【讨论】:

  • 不工作。出现类似的东西: %PDF-1.3 3 0 obj > endobj 4 0 obj >流 x��RMO�0��W
【解决方案2】:

已解决:

我做了不同的方式:

在我的 PHP 报告中,我生成了一个临时文件,并传递了参数“F”。

$pdf->Output('file.pdf','F');

在 Jquery Ajax 中,我打开文件,而不是打开从 ajax 请求恢复的数据。

$.ajax({
        type: 'POST',
        url: '../reports/report.php',
        data: { id: id }
    }).done(function(data){         
        var fileName = "file.pdf";
        $('#modalRel').modal('show');
         var object = "<object data=\"{FileName}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
                    object += "If you are unable to view file, you can download from <a href = \"{FileName}\">here</a>";
                    object += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
                    object += "</object>";
                    object = object.replace(/{FileName}/g, "../Files/" + fileName);
                     $("#body-rel").html(object);
    })

我希望有一天能帮助到需要的人。

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 2011-09-30
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    相关资源
    最近更新 更多