1

データベースから取得した情報を含む PDF ファイルを生成する必要があります。これを行うために PHPExcel を使用していますが、「レポート」ボタンをクリックすると、すべての情報が取得されて配置されます。このゴミは、メモ帳で PDF を開こうとするとランダムな記号が表示されるだけのようなものです。

ここに私がそれを行う方法があります

$.ajax呼び出しを使用して、表示されるすべての情報をフォームに取得しています。

$.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、コードのエラーを報告する必要があります。

4

2 に答える 2

0

すべての出力を HTML ファイルに書き込み、wkhtmltopdf -> http://code.google.com/p/wkhtmltopdf/を使用して PDF に変換します。それは素晴らしい作品です!

html ファイルの作成に使用するコードは次のとおりです。

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

それが成功したら、それを PDF に変換し、古い 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ユーザーにダウンロードさせることができます。

于 2012-07-23T19:12:49.300 に答える
0

既存の HTML ページ内に PDF 出力を書き込んでいます

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

ブラウザーは、HTML ページに表示されるものはすべて、バイナリ データのストリームではなく、HTML マークアップであると想定します。

成功した場合は、出力を新しいブラウザ ウィンドウに送信します。

于 2012-07-23T20:01:46.873 に答える