服务器发布ajax问题后返回TCPDF [英] return TCPDF after server post ajax issue
问题描述
我正在尝试在验证表单后返回tcpdf.如果验证中存在错误,则php文件将返回错误消息.如果验证成功,我想返回PDF.麻烦的是,当返回pdf时,它不会显示为PDF,而只是显示垃圾文本.我需要对代码进行哪些更改?
I am trying to return a tcpdf after validating a form. If there is an error in validation the php file returns an error msg. If the validation succeeds I want to return the PDF. Trouble is that when the pdf is returned it is not displayed as a PDF just garbage text. What change do I need to make in my code?
这是我的ajax提交代码:
Here is my ajax submit code:
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:
In my PHP file I either echo an error msg or return the pdf:
$pdf->writeHTML($report, true, false, true, false, '');
$pdf->Output('match_report.pdf', 'D');
推荐答案
出于显而易见的原因,因为它不是HTML,所以无法将PDF直接显示到div中.但是您可以使用以下方法将其嵌入您的网页中:
You can't show directly a PDF into a div for the obvious reason that it's not HTML. But you can embed it in your page using this:
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作为二进制流.
Like base64 data image you can include a PDF in your page as a binary stream.
这篇关于服务器发布ajax问题后返回TCPDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!