【问题标题】:return TCPDF after server post ajax issue服务器发布 ajax 问题后返回 TCPDF
【发布时间】:2016-05-13 20:10:20
【问题描述】:

我在验证表单后尝试返回一个 tcpdf。如果验证出现错误,php 文件将返回错误消息。如果验证成功,我想返回 PDF。麻烦的是,当返回 pdf 时,它不会显示为 PDF,只是垃圾文本。我需要对我的代码进行哪些更改?

这是我的 ajax 提交代码:

    function postForm() {
    var ajaxRequest;
    /* Clear result div*/
    $("#result").html('');
    /* Get from elements values */
    var values = $("#matchReportForm").serialize()
    ajaxRequest= $.ajax({
            url: "mReport.php",
            type: "post",
            data: values,
            beforeSend: function() {
                    $('#result').html('<img src="images/indicator.gif" alt="loading..." />');
                    $('#btnGo').attr("disabled", "disabled");
                    $("#txtSecurity").focus();
                }
        });

     ajaxRequest.done(function (response, textStatus, jqXHR){
          // show successfully for submit message
          $("#result").html(response);
          $('#btnGo').removeAttr("disabled");
     });

     /* On failure of request this function will be called  */
     ajaxRequest.fail(function (){
       // show error
       $("#result").html(response);
       $('#btnGo').removeAttr("disabled");
     });

    }

在我的 PHP 文件中,我要么回显错误消息,要么返回 pdf:

    $pdf->writeHTML($report, true, false, true, false, '');

    $pdf->Output('match_report.pdf', 'D');

【问题讨论】:

  • 好吧,您不能简单地将 PDF 有效负载注入到 html 元素中。服务器应该如何知道这是一个 PDF 文档以及如何显示它?您将其视为字符串或 html 标记。那是行不通的。
  • 这不是很有帮助,对不起,我应该提到我希望在返回 pdf 时打开它。我只希望错误消息显示在 div 而不是 pdf 中。所以我需要检查响应是 html 还是二进制,然后在 div 中显示 msg 或打开 pdf 本身。谢谢。

标签: php jquery ajax tcpdf


【解决方案1】:

您不能将 PDF 直接显示到 div 中,原因很明显,它不是 HTML。但是您可以使用以下方法将其嵌入到您的页面中:

function postForm() {
    var ajaxRequest;
    /* Clear result div*/
    $("#result").html('');
    /* Get from elements values */
    var values = $("#matchReportForm").serialize()
    ajaxRequest= $.ajax({
        url: "mReport.php",
        type: "post",
        data: values,
        beforeSend: function() {
            $('#result').html('<img src="images/indicator.gif" alt="loading..." />');
            $('#btnGo').attr("disabled", "disabled");
            $("#txtSecurity").focus();
        }
    });

    ajaxRequest.done(function (response, textStatus, jqXHR){
        // show successfully for submit message
        var pdf = $.base64.decode($.trim(response));
        $("#result").html('<embed width=100% height=100% type="application/pdf" src="data:application/pdf;base64,' + escape(pdf) + '"></embed>');
        $('#btnGo').removeAttr("disabled");
    });

    /* On failure of request this function will be called  */
    ajaxRequest.fail(function (){
        // show error
        $("#result").html(response);
        $('#btnGo').removeAttr("disabled");
    });
}

像 base64 数据图像一样,您可以在页面中包含 PDF 作为二进制流。

【讨论】:

  • 感谢您的回复 :)。我只想在 div 中显示错误消息,如果它返回一个 pdf,那么我希望 pdf 只是打开而不是嵌入到 div 中。我该怎么做?
  • sry 我尝试过,但失败了,在 $.base64 中
猜你喜欢
  • 2010-11-12
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 2012-02-09
  • 1970-01-01
相关资源
最近更新 更多