我需要生成一个 PDF 文件,其中包含从数据库中获取的一些信息,我正在使用 PHPExcel 来执行此操作,但事情是这样的,当我单击“报告”按钮时,我会获取所有信息并将其放入进入 PDF,我的页面中有很多“垃圾”,这些垃圾就像您尝试用记事本打开 PDF 时一样,它只显示随机符号。
这是我的做法
我正在使用$.ajax
调用将所有信息显示到表单中:
$.ajax({
// gets all the information and puts them into the form and several tables
});
然后我向按钮添加一个事件处理程序,report
以便我将请求 id 发送到一个 php 脚本,该脚本将收集必要的信息来填写报告
report.on('click',function(){
$.ajax({
url : '../../php/reports/requestReport.php',
type : 'post',
data : {'idRequest' : id },
dataType : 'json',
success : function(response){
console.log(response);
},
error : function(response){
if(response.responseText != undefined)
$('body').append(response.responseText);
console.warn(response);
}
});
});
在我的 php 文件中,我有这样的东西:
<?php
require '../functions.php';
require '../classes/PHPExcel.php';
if(isset($_POST['idRequest']))
$idRequest = $_POST['idRequest'];
else{
$idRequest = false;
echo json_encode(array("ExitCode"=>1,"Message"=>"idRequest Not Received","Location"=>"requestReport.php"));
}
if($idRequest){
try{
// get all the data
// save the request and send it to the report
$excel = new PHPExcel();
//Initializing
$excel->getProperties()->setCreator("TTMS")
->setLastModifiedBy("TTMS")
->setTitle("Request update $idRequest");
$excel->setActiveSheetIndex(0)
->setCellValue('C1', 'Request For Q.A. / Q.C. / Testing');
// Rename worksheet
$excel->getActiveSheet()->setTitle("$idRequest");
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$excel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (PDF)
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="Update_Request_'.$idRequest.'.pdf"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($excel, 'PDF');
$objWriter->save('php://output');
} catch(PDOException $ex){
echo json_encode(array("ExitCode"=>2,"Message"=>$ex->getMessage(),"Location"=>"requestReport.php PDOException"));
}
长话短说,我在表单所在的页面上得到了垃圾。我认为这与我正在执行此操作有关,ajax
但我需要这样做,因此echo
'si 必须报告我的代码中的错误。