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 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序