2

I am using laravel 4.2 barryvdh/laravel-dompdf. I want to have button for print my PDF. My routes:

Route::resource('orders', 'OrderController');

In OrderController I have method:

public function printpdf($id)
    {
        $order = Order::find($id);

        $pdf = PDF::loadView('print.en', $order);
        return $pdf->download($order->id .' '. $order->created_at);
    }

I can to call that method from my show.blade.php. I am using this link:

<a class="btn btn-small btn-danger" href="{{URL::to('orders/' . $order->id . '/printpdf')}}">Print PDF</a>

What route I must have to make this work? I want to make a function which takes a record from my MySQL database and prints it as PDF. How to use this correctly?

Rick Smith
  • 9,031
  • 15
  • 81
  • 85
Aipo
  • 1,805
  • 3
  • 23
  • 48

1 Answers1

2

As laravel doc provided,

If it becomes necessary for you to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource:

In your case, you can do like this,

Route::get('orders/{id}/printpdf', 'OrderController@printpdf');
Route::resource('orders', 'OrderController');

In your OrderController, according to this issue, you can do it like this.

public function printpdf($id)
{
    $order = Order::find($id);
    $pdf = PDF::loadView('print.en', array("order" => $order));
    return $pdf->download($order->id .' '. $order->created_at);
}

Hope it will be useful for you.

Arkar Aung
  • 3,554
  • 1
  • 16
  • 25
  • Ok, routes are clear, now problems apears at unknown parameter $order in my view... How to pass it? – Aipo Apr 22 '15 at 16:24
  • in my method var_dump($order) shows that I have all elements, but in my view, I have no info. – Aipo Apr 22 '15 at 16:28
  • One last thing, how to redirect after download pop- up to homepage with message "Order saved"? – Aipo Apr 23 '15 at 08:04
  • It is a little bit tricky. Because you can't send hearder twice. http://stackoverflow.com/questions/15872534/how-to-download-a-file-then-redirect-to-another-page-in-php – Arkar Aung Apr 23 '15 at 08:14
  • So, After download Myview stays show.blade.php. So solution could be: 1. To add button and redirect? – Aipo Apr 23 '15 at 08:19
  • Yes, this is the simplest way. But you can redirect by using that way. http://www.willmaster.com/blog/automation/one-link-download-and-redirect.php But it has to write pdf file first. – Arkar Aung Apr 23 '15 at 09:56
  • How how to inform user, that he already printed a view? add value for order in database? – Aipo Apr 23 '15 at 10:04
  • You can set flag in user session before download pdf. According to that flag, you can prevent downloading it and add value to db again. – Arkar Aung Apr 23 '15 at 10:10