How to Add Image to PDF File in Laravel
Hi Artisan, in this tutorial I’m going to share how to add image to PDF file in Laravel application. I’m testing on Laravel 7. Let’s get started:
Table of Contents
Create Laravel Project
Each Laravel project needs this thing. That’s why I have written an article on this topic. Please see this part from here: Install Laravel and Basic Configurations.
Install dompdf Package
Run this command to install barryvdh/laravel-dompdf
package:
composer require barryvdh/laravel-dompdf
Make Controller
Let’s make a controller named PDFController using this command:
php artisan make:controller PDFController
Open the controller and paste this code:
<?php
namespace App\Http\Controllers;
use PDF;
use Illuminate\Http\Request;
class PDFController extends Controller
{
public function makePDF()
{
$data = ['title' => 'MyNotePaper.com'];
$pdf = PDF::loadView('pdf', $data);
return $pdf->download('mnp.pdf');
}
}
Make View File
Before making the blade file, I’m keeping a dummy picture at location public/mypic.jpg. Now navigate to resources/views path and create a file named pdf.blade.php.
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to {{ $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>The picture:</strong><br/><br/>
<img src="{{ public_path('mypic.png') }}">
</body>
</html>
Define Route
This is the last step. Register a route for makePDF()
function:
Route::get('pdf','[email protected]');
Now, run the project and see the output by navigating to http://example.com/test
route.
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)