2012-07-23 58 views
1

我需要生成一個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的我必須報告我的代碼中的錯誤。

+0

我很困惑。您正在生成Excel電子表格,但將其命名爲PDF?一個PDF不是,從來沒有,也不會是一個Excel電子表格,所以你難怪你要扔垃圾。這就像重命名一個.jpg圖像爲「cutekittens.txt」,並希望記事本能夠顯示圖像。 – 2012-07-23 19:09:43

+0

PHPExcel允許我創建一個excel文件,但最後保存爲PDF格式,大多數情況下我只是從PHPExcel文檔中的教程複製 – 2012-07-23 19:12:03

回答

0

你寫了一個現有的HTML頁面

$('body').append(response.responseText);    

瀏覽器呈現在HTML頁面中顯示的所有裏面的PDF輸出HTML標記,而不是二進制數據流。

如果成功,則將輸出導向新的瀏覽器窗口。

+0

我怎樣才能打開從JavaScript的另一個窗口? – 2012-07-24 15:20:51

0

將所有輸出寫入HTML文件,然後使用wkhtmltopdf - >http://code.google.com/p/wkhtmltopdf/將其轉換爲PDF。它工作真棒!

這裏是代碼,你會用它來創建你的HTML文件:一旦成功將其轉換爲PDF

if (!$handle = fopen('/path/to/folder/your_file.html', 'w')) { 
    die("Cannot open file for write"); 
} 

$data_string = "<html><body></body></html>"; //all your html output goes here, formatted correctly, etc 

// Write somecontent to our opened file. 
if (fwrite($handle, $data_string) === FALSE) { 
    die("Cannot write to file"); 
} 

然後,然後取出舊的HTML文件

// convert to PDF 
    $output = shell_exec('wkhtmltopdf --page-size Letter /path/to/folder/your_file.html /path/to/folder/your_file.pdf'); 
    if (strpos($output,'Error') == false){ 
     $output = shell_exec('rm /path/to/folder/your_file.html'); 
    }else{ 
     die("error creating pdf, please contact IT administrator."); 
    } 

現在你有PDF坐在位置 - >/path/to/folder/your_file.pdf,您可以使用戶下載。

+0

我看到這個使用了webkit引擎,我必須確保這個工作100%與Firefox,不會導致問題? – 2012-07-23 19:16:47

+0

除了我認爲迫使我的用戶使用命令行將不可行 – 2012-07-23 19:17:59

+0

這是所有服務器端。您輸出到您的Web服務器上的HTML文件(而不是客戶端計算機上),然後使用PHP將HTML文件轉換爲PDF。然後讓客戶端下載PDF。我將用示例腳本更新我的答案。 – 2012-07-23 19:21:14