我需要生成一个PDF文件,其中包含我从数据库获得的一些信息,我使用PHPExcel来执行此操作,但这里是当我点击按钮“报告`所以我得到所有的信息,并把它放到PDF中,我在我的页面中收到很多“垃圾”,这个垃圾就像当你试图用记事本打开一个PDF并且它只显示随机符号。 PHPExcel在主页面显示垃圾
这里是我如何做到这一点
我使用的是$.ajax
调用get显示成表格的所有信息:
$.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
的我必须报告我的代码中的错误。
我很困惑。您正在生成Excel电子表格,但将其命名为PDF?一个PDF不是,从来没有,也不会是一个Excel电子表格,所以你难怪你要扔垃圾。这就像重命名一个.jpg图像为“cutekittens.txt”,并希望记事本能够显示图像。 – 2012-07-23 19:09:43
PHPExcel允许我创建一个excel文件,但最后保存为PDF格式,大多数情况下我只是从PHPExcel文档中的教程复制 – 2012-07-23 19:12:03