0

I had write some sample codes to generate pdf in my laravel controller. It get a 200 response code but the pdf is not generating.

Below is my code.

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

But this is working when i directly include the code inside route in web.php file.

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', 'ReportAuditController@exportPDF');

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

May i know what the problem is?

etzzz
  • 165
  • 1
  • 4
  • 15
  • So it has something to do with your route and controller function name. Show your initial route when using the controller. – EddyTheDove Jun 13 '17 at 03:14
  • @EddyTheDove I edited my post. Do have a look. thanks! – etzzz Jun 13 '17 at 03:59
  • are you creating pdf via ajax ..? – Alankar More Jun 13 '17 at 04:01
  • @AlankarMore , Yes, I will post the data to the controller and let the controller generate the pdf. Is this wrong? if yes what are the best way to do this? – etzzz Jun 13 '17 at 04:06
  • Controller will create your PDF via ajax but for rendering save your pdf some location and return the name of that generated file. After rendering redirect user to other route passing generated PDF name to display PDF content. In simple word you have to access PDF directly via browser after creating it by Ajax – Alankar More Jun 13 '17 at 05:06

1 Answers1

1

You cannot generate PDFs via Ajax because Ajax requests expect a response, which Laravel sends back as JSON by default. So what you could do, is make a normal GET route that will display the PDF e.g:

Route::get('display-pdf', 'ReportAuditController@exportPDF');

Since your ajax does not POST any data (your data object is empty), you could bypass your ajax request and simply use an anchor

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

If for some reason, you still want to use Ajax, you can use the success response from the ajax request like this

$.ajax({
    type: "POST",
    url: '/data-url',
    data: {},
    success: function(content) {
        window.location.href = '/display-pdf';
    }
}); 
EddyTheDove
  • 12,979
  • 2
  • 37
  • 45