Laravel Image to PDF File

In this article, we will show you laravel image to PDF file. This article will give you simple example of laravel image to PDF file. you will learn laravel image to PDF file.

You need to just follow bellow step to create pdf file and also can download. So let’s do bellow steps.

This article will give you simple example of laravel image to PDF file of how to add laravel image to PDF file Function in laravel 6, laravel 7 and laravel 8 version.

Step 1:- Install Laravel

composer create-project --prefer-dist laravel/laravel blog

Step 2:- Install dompdf Package

composer require barryvdh/laravel-dompdf

config/app.php

'providers' => [

	....

	Barryvdh\DomPDF\ServiceProvider::class,

],


'aliases' => [

	....

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

]

Step 3:- Add Route

routes/web.php

Route::get('generate-pdf','PDFController@generatePDF');

Step 4:- Add Controller

app/Http/Controllers/PDFController.php

<?php

namespace App\Http\Controllers;

  

use Illuminate\Http\Request;

use PDF;

  

class PDFController extends Controller

{

    /**

     * Display a listing of the resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function generatePDF()

    {

        $data = ['title' => 'Welcome to codeplaners.com'];

        $pdf = PDF::loadView('myPDF', $data);

  

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

    }

}

Step 5:- Create View File

Here, you have to add two images on following path:

public/dummy.jpg

storage/app/public/dummy.jpg

In Last step, let’s create myPDF.blade.php(resources/views/myPDF.blade.php) for layout of pdf file and put following code:

resources/views/myPDF.blade.php

<!DOCTYPE html>

<html>

<head>

    <title>Laravel Image to PDF File - codeplaners</title>

</head>

<body>

    <h1>Welcome to codeplaners.com - {{ $title }}</h1>

    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod

    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse

    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non

    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

  

    <br/>

    <strong>Public Folder:</strong>

    <img src="{{ public_path('dummy.jpg') }}" style="width: 200px; height: 200px">

 
    <br/>

    <strong>Storage Folder:</strong>

    <img src="{{ storage_path('app/public/dummy.jpg') }}" style="width: 200px; height: 200px">

</body>

</html>

Now we are ready to run this example and check it…

Recommended Posts