2017-06-13 8 views
0

私のlaravelコントローラにpdfを生成するサンプルコードを書きました。それは200の応答コードを得るが、pdfは生成されない。laravelコントローラでpdfを生成できません

以下は私のコードです。

function exportPDF() { 
    // instantiate and use the dompdf class 
    $dompdf = new PDF(); 
    $dompdf->loadHtml('<h1>hello world</h1>'); 

    // (Optional) Setup the paper size and orientation 
    $dompdf->setPaper('A4', 'landscape'); 

    // Render the HTML as PDF 
    $dompdf->render(); 

    // Output the generated PDF to Browser 
    return $dompdf->stream(); 
} 

しかし、これは私が直接web.phpファイル内のルート内のコードが含まれている場合に取り組んでいます。

Route::get('/generate-pdf', function() { 
     $pdf = App::make('dompdf.wrapper'); 
     $pdf->loadHTML('<h1>Test</h1>'); 
     return $pdf->stream(); 
    }); 

EDITED

web.php

Route::post('/report-audit-export-pdf', '[email protected]'); 

の.js

window.exportPDF = function() { 
    var hidden_category = $('#hidden_category').val(); 
    $.ajax({ 
     type: "POST", 
     url: '/report-audit-export-pdf', 
     data: { 

     }, 
     headers: { 
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      console.log("jqXHR : " + jqXHR + " and textStatus : " + textStatus + " and errorThrown : " + errorThrown); 
     }, 
     success: function(content) { 
     // alert("Success"); 
     } 
    }); 
} 

私は、問題が何であるかを知っているかもしれませんか?

+0

のようなAJAXリクエストから成功応答を使用することができますので、それはあなたのルートとは何かを持っているアンカー

<a href="/display-pdf">Display PDF</a> 

を使用し、コントローラ機能イオン名。コントローラを使用しているときの初期ルートを表示します。 – EddyTheDove

+0

@EddyTheDove私の投稿を編集しました。見てください。ありがとう! – etzzz

+0

あなたはajax経由でpdfを作成していますか? –

答えて

1

Ajaxを使用してPDFを生成することはできません.Ajaxリクエストは応答を期待しているため、LaravelはデフォルトでJSONとして返信します。だから、あなたは何ができるか、PDFの例を表示する通常のGETルートを作るです:

Route::get('display-pdf', '[email protected]'); 

あなたのアヤックスは、(あなたのデータオブジェクトが空である)任意のデータをPOSTしないので、あなたは、AJAXリクエストをバイパスし、単に可能性何らかの理由で、あなたはまだアヤックスを使用したい場合は、この

$.ajax({ 
    type: "POST", 
    url: '/data-url', 
    data: {}, 
    success: function(content) { 
     window.location.href = '/display-pdf'; 
    } 
}); 
関連する問題