Laravel 5.1 - barryvdh/laravel-dompdf, PDF file download not working properly

SJB picture SJB · Dec 28, 2015 · Viewed 14.6k times · Source

I have installed 'barryvdh/laravel-dompdf' using composer and added these lines

Barryvdh\DomPDF\ServiceProvider::class
'PDF'=>  Barryvdh\DomPDF\Facade::class

to 'app.php' file in 'config' folder and also created PdfController, like this

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App;
use PDF;

class PdfController extends Controller
{
    public function invoice()
    {
        $pdf = App::make('dompdf.wrapper');
        $pdf->loadHTML('<h1>Test</h1>');
        return $pdf->stream('testfile')
               ->header('Content-Type','application/pdf');
    }
}

now the code works fine , contents are show in browser in pdf format, but when i download this file, the file doesn't have any extension.I tried to modify the line containing return code like

return $pdf->stream('testfile.pdf')
                   ->header('Content-Type','application/pdf');

But now, page directly provides file to download with name 'document.pdf' which shows error while downloading,(dont konw how and from where this name 'document.pdf' came from). Also i m working on localhost.

Answer

Adnan picture Adnan · Dec 28, 2015

What do you want to do with pdf ? To show in browser or to download to disc? this is example where pdf is downloaded

return $pdf->download('pdfName.pdf'); 

example from my code where i show notebook information in pdf

   $laptop = Notebook::findOrFail($id);
        $data['laptop'] = $laptop;
        $pdf = \PDF::loadView('pdf.detaljiLaptop', $data);

        return $pdf->download($laptop->modelName.' Computers.pdf');