8

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.

2
  • Try $pdf->stream('testfile.pdf') without ->header('Content-Type','application/pdf'); Probably dompdf sends the headers by itself Commented Dec 28, 2015 at 12:38
  • Thanks for quick response, but i have tried it already..& problem persists.. :( Commented Dec 28, 2015 at 12:42

3 Answers 3

4

I had the same problem, after searching all over without results, I decided to play with the code and this worked for me.

Inside config/app.php

-> under providers add the line of code,

'Barryvdh\DomPDF\ServiceProvider' not Barryvdh\DomPDF\ServiceProvider::class,

enter image description here

-> under aliases add the line of code,

'PDF'       => 'Barryvdh\DomPDF\Facade' not 'PDF' => Barryvdh\DomPDF\Facade::class,

enter image description here

Mind the quotes and commas! Check the images above,

-> In your controller add the line of code, use App;

Hopefully, you will be good to go....test using the lines of code..place them inside a method in your controller.

$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();

On success, your browser will open a PDF Viewer with a white PDF with the word Test

Cheers, enjoy your coding ;)

Sign up to request clarification or add additional context in comments.

Comments

3

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'); 

2 Comments

@SJBansod this code is actually working. I do this in the same way. What is the error or misbehaviour you get?
Thanks. i actually wanted to download the file., but it was also not working.,,i was using firefox with a bit old version..but when i tested it in different browser...(chrome), both codes( original code that i have posted in question and other one of downloading pdf ) works fine...lesson: when using dompdf test it in different browser :)
0

I use this code view()->share($data); $pdf = PDF::loadview('sale.pdf'); return $pdf->download('slip_out.pdf');

instead of

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.