douzuo0002 2012-07-23 19:08
浏览 19
已采纳

PHPExcel在主页面显示垃圾

I need to generate a PDF file with some information i get from a database, i'm using PHPExcel in order to do this, but here is the thing, when i click the button `report` so i get all the information and put it into the PDF, i get a lot of "garbage" in my page, this garbage it's like when you try to open a PDF with notepad and it just shows random symbols.

Here is how i do it

I'm using an $.ajax call to get all the information displayed into a form:

$.ajax({
    // gets all the information and puts them into the form and several tables
});

Then i add an event handler to the button report so that i send the request id to a php script which will gather the necessary information to fill the report

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);
        }
    }); 
});

And on my php file i have something like this:

<?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"));
}

So long story short i get garbage on the page where the form is. I think it has something to do with the fact that i'm doing this via ajax but i need to do this so the echo's i have to report the errors in my code.

  • 写回答

2条回答 默认 最新

  • dsjojts9734 2012-07-23 20:01
    关注

    You're writing the PDF output inside an existing HTML page

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

    The browser assumes everything displayed in an HTML page is HTML markup, not a stream of binary data.

    Direct the output to a new browser window if success.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)