2012-07-23 65 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