我需要生成一個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