Mithun Shreevatsa
Mithun Shreevatsa

Reputation: 3609

How to view pdf from routes in laravel?

Trying to view pdf stored in /public/assets/others/brochure/jan-2016.pdf file from brochure/jan-2016.pdf', 'BrochureController@show'); route in laravel.

But i get error as : Whoops, looks like something went wrong

Route Code :

Route::get('/brochure/{file}', 'BrochureController@show');

And in controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class BrochureController extends Controller
{   
    public function show($filename)
    {       
        $filename = 'jan-2016.pdf';
        $filepath = 'assets/others/brochure/';
        $path = $filepath.$filename;

        return Response::make(file_get_contents($path), 200, [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; '.$filename,
        ]);
    }
}

When i try to print file contents only, its capturing the file and display in some odd manner using file_get_contents($path)

Output: %PDF-1.4 %���� 1 0 obj <>stream PScript5.dll Version 5.2.2 GVTP January 2016 Dummy content endstream endobj 2 0 obj <> endobj 4 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <>/Rotate 90/Parent 3 0 R/MediaBox[0 0 595 842]/Contents 11 0 R/Type/Page>> endobj 11 0 obj <>stream x��]Ys�u.;����8Ξ��d���z_�'�Z���Iت�� �$����_��oN�L�9=sw@��-...

That means its picking the file from the exact path. I believe an reader to be specifed to handle this as a third party service, Ex: Adobe pdf reader.

What am i doing wrong?

Upvotes: 3

Views: 6922

Answers (4)

borodatych
borodatych

Reputation: 316

Consider on example of an offer contract


Controller:

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;

class OfferController extends Controller
{
    public function index()
    {
        $file = public_path().DIRECTORY_SEPARATOR.'media'.DIRECTORY_SEPARATOR.'offer.pdf';
        $file = File::get($file);
        $response = Response::make($file,200);
        $response->header('Content-Type', 'application/pdf');
        return $response;
    }
}

Route:

Route::get('/offer','OfferController@index')->name('offer');

Profite:

https://name.site/offer

Upvotes: 6

Vijay Rana
Vijay Rana

Reputation: 1079

You can use this package [barryvdh/laravel-dompdf][1]. and use simply as below: This is just the example snippset

class BrochureController extends Controller
{   
    public function show($filename)
    {       
        $html = 'Your content in PDF';    

        $pdf = PDF::loadHTML($html);

        //set the name of the pdf document
        $file_name = 'jan-2016.pdf';

        //check if the path exist or not
        if(!File::exists(storage_path() . '/pdf')) {

             $result = File::makeDirectory(storage_path() . '/pdf');
        }

        $pdf->save(storage_path() . '/pdf/' . $file_name);

        //return $file_name;

        return $pdf->download($file_name);

    }
}

Upvotes: 0

Joseph
Joseph

Reputation: 2712

In BrochureController.php

public function show($filename)
{
    $file = base_path(). "/public/assets/others/brochure/". $filename;

    $name = "Human Name For File.pdf";

    $headers = ['Content-Type: application/pdf'];

    return Response::download($file, $name, $headers);
}

Now access http://example.com/brochure/jan-2016.pdf in your app and the jan-2016.pdf file will download.

If you don't want the users to be able to guess that they can also access http://example.com/assets/others/jan-2016.pdf then you can move the PDF to another directory (such as /resources/brochures) where it is not publicly accessible and then you can just change the first line of the method to:

$file = base_path(). "/resources/brochures/". $filename;

This will mean that everyone has to go to http://example.com/brochure/jan-2016.pdf to access the file.

Upvotes: 0

Panda
Panda

Reputation: 2510

Here is the code

$filename = 'assets/others/129778.pdf';
$path = storage_path().DIRECTORY_SEPARATOR.$filename;

return Response::make($path, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; '.$filename,
]);

See if it works and do lemme know.

Upvotes: 0

Related Questions