Laravel 5.1 - barryvdh / laravel-dompdf, PDF file loading does not work properly

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

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

to 'app.php' in the 'config' folder, and a PdfController was created, for example

 <?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, the content is displayed in the browser in pdf format, but when I download this file, the file has no extension. I tried changing the line containing the return code like

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

But now the page directly provides a download file with the name 'document.pdf' which shows an error during loading (I don’t know how and where this name β€œdocument.pdf” came from). I also work on localhost.

+5
source share
3 answers

What do you want to do with pdf? Show in browser or download to disk? this is an example where pdf is loaded

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

An example from my code where I show information about a laptop in pdf

  $laptop = Notebook::findOrFail($id); $data['laptop'] = $laptop; $pdf = \PDF::loadView('pdf.detaljiLaptop', $data); return $pdf->download($laptop->modelName.' Computers.pdf'); 
+2
source

I had the same problem, after I searched for it without any results, I decided to play with the code, and it worked for me.

Inside config / app.php

-> under the providers add a line of code,

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

enter image description here

-> under the aliases add a line of code,

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

enter image description here

Remember quotes and commas! Check out the images above

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

Hope you can pass the test well ... using lines of code .. put them inside the method in your controller.

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

If successful, your browser will open the PDF Viewer with a white PDF with the word Test

Hi, enjoy your coding;)

+1
source

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

instead

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

Source: https://habr.com/ru/post/1239350/


All Articles