armancs's avatar

how can i print this pdf using dompdf

controller

 public function printPDF($id)
{
    $order = Order::findOrFail($id);
    $products = OrderedProducts::where('orderid',$id)->get();
    $pdf = PDF::loadView('admin.paymentPdf', compact('order','products'));
    return $pdf->stream("invoice.pdf",array("Attachment" => false));
}

its generate pdf and download the file but i want to show this page as print view. please help

0 likes
6 replies
bobbybouwmann's avatar

One important thing to remember is that you can't open a new tab using PHP to show the PDF. So you can work around that by using target="_blank" in your link to the PDF. This willen open the a new tab and show you the PDF

Then the second important thing is the browser settings of the user. You can see to your download response that you don't want it to be downloaded by default, but the user can configure this in the settings of the browser as well. So you don't have control over that.

In Laravel you can use something like this to achieve that

$pdf = PDF::loadView('admin.paymentPdf', compact('order','products'));

return $pdf->stream('invoice.pdf');

Note: The stream method doesn't have a second parameter! See: https://github.com/barryvdh/laravel-dompdf/blob/master/src/PDF.php#L177

1 like
bobbybouwmann's avatar
Level 88

Yes you can by doing something like this

use Illuminate\Http\Response;

$pdf = PDF::loadView('admin.paymentPdf', compact('order','products'));
$output = $pdf->output();

return new Response($output, 200, [
    'Content-Type' => 'application/pdf',
    'Content-Disposition' =>  'inline; filename="'myfilename.pdf"',
]);

Basically we're creating a new response using Laravels Response class by passing in the output of a given PDF.

Let me know if this works for you!

2 likes
armancs's avatar

@BOBBYBOUWMANN - Thanks for your response. i am Getting this error after running this code:

Error-

local.ERROR: Use of undefined constant filename - assumed 'filename' 

Code-

   public function downloadPDF($id)
     {
    $order = Order::findOrFail($id);
    $products = OrderedProducts::where('orderid',$id)->get();
    $pdf = PDF::loadView('admin.paymentPdf', compact('order','products'));
    // return $pdf->download('invoice.pdf');

    $output = $pdf->output();

return new Response($output, 200, [
 'Content-Type' => 'application/pdf',
  'Content-Disposition' =>  'inline',
 'filename'=>"'invoice.pdf'"]);
 }
bobbybouwmann's avatar

@armancs filename is not an extra key in the array. It belongs to the Content-Disposition key. If you look carefully to my example you can see that there as well!

return new Response($output, 200, [
    'Content-Type' => 'application/pdf',
    'Content-Disposition' =>  'inline; filename="'invoice.pdf"',
]);
1 like
armancs's avatar

Thanks For Response. Erorr fix by removing a quotation in invoice.pdf

return new Response($output, 200, [
   'Content-Type' => 'application/pdf',
  'Content-Disposition' =>  'inline; filename="invoice.pdf"',
]);

But still file downloaded when open new tab

1 like

Please or to participate in this conversation.