我在验证表单后尝试返回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');
答案 0 :(得分: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作为二进制流。