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.

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.

Write all the output to an HTML file, then use wkhtmltopdf -> http://code.google.com/p/wkhtmltopdf/ to convert it to PDF. It works awesome!

Here is code you would use to create your html file:

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

Then once that is successful you convert it to PDF, then remove the old html file

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

Now you have the PDF sitting in the location -> /path/to/folder/your_file.pdf which you can make the user download.