Laravel 8 Nepali font PDF Tutorial: Generate Nepali font PDF in Laravel with DOMPDF

Tamrakar Shreyaa
6 min readFeb 8, 2021

--

In this tutorial, we will discourse about the Laravel 8 Export to PDF topic. We will learn how to generate PDF from HTML using the DomPDF library.

Install Laravel Project

For Laravel 8 export to PDF demo example we require to download a fresh laravel app.

Run the command to induct fresh Laravel project.

composer create-project laravel/laravel nepali-font --prefer-dist

Install DomPDF Package in Laravel

In general, there are many other third-party packages available for HTML to PDF conversion in Laravel. DomPDF package is a better choice, among others.DOMPDF is a wrapper for Laravel.

Run the under-mentioned command to install DomPDF in Laravel 8.

composer require barryvdh/laravel-dompdf

Configure DomPDF Package in Laravel

Open config/app.php file and incorporate DomPDF service provider in providers array along with DomPDF facade to the aliases array.

'providers' => [
Barryvdh\DomPDF\ServiceProvider::class,
],

'aliases' => [
'PDF' => Barryvdh\DomPDF\Facade::class,
]

Execute the following command to publish the assets from vendor.

php artisan vendor:publish

A handful of packages list appeared on your terminal window, and we have to select the “Provider: Barryvdh\DomPDF\ServiceProvider” option from the list. It will create the new file config/dompdf.php, holding global configurations for the DomPDF plugin.

Ultimately, you can take the DomPDF in service to convert HTML to PDF File in Laravel using the following attribute.

use Barryvdh\DomPDF\Facade as PDF;
OR
use PDF;

Setting Up Model and Migrations

Create a new Model for Project using the given below command.

php artisan make:model Project -m

Open app\Project.php file, and place the following code.

<?phpnamespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model{
use HasFactory;
protected $fillable = ['title','description']
}

Open database/migrations/timestamp_create_projects_table.php and add the form values that we need to store in the database.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProjectsTable extends Migration{
/**
* Run the migrations.
*
* @return void
*/
public function up(){
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->longText('description');
$table->timestamps();
});}/**
* Reverse the migrations.
*
* @return void
*/
public function down(){
Schema::dropIfExists('projects');
}
}

Execute the following command to migrate the database values, verify the table created with the values mentioned above in the MySQL database.

php artisan migrate

Define Controller & Route

Create a new controller, and It will have the logic to display the project’s list. Run the command to create the controller.

php artisan make:controller ProjectController -r

Open app/Http/Controllers/ProjectController.php and add the following code.

<?php
namespace App\Http\Controllers;
use App\Models\Project;
use Barryvdh\DomPDF\Facade as PDF;
use Illuminate\Http\Request;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(){
$projects=Project::all();
return view ('project.index',compact('projects'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view ('project.add');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$project = new Project();
$project->fill($request->all());
$project->save();
return redirect()->route('project.index')->with('message', 'Add successfully');
}
/*** Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
// Generate PDF
public function createPDF() {
// retreive all records from db
$projects=Project::all();
// share data to view
$pdf=mb_convert_encoding(\View::make('project.nepali_font_pdf_view', compact('projects')), 'HTML-ENTITIES', 'UTF-8');
libxml_use_internal_errors(true);
return PDF::loadHtml($pdf)->download('nepali_font_pdf_view.pdf');
}
}

Define Route

Open routes/web.php and insert the following code. It will create relation between the controller & the view over and above that it will also invoke the pdf download.

<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
// for route index,store,
Route::resource('project',ProjectController::class);
// for route createPDF
Route::get('/pdf', [App\Http\Controllers\ProjectController::class, 'createPDF'])->name('pdf');

Display Project Index in Blade View Template

Generate a blade file resources/views/project/index.blade.php then insert the following code inside.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title class="text-center">Laravel 8 HTML to nepali font PDF Example</title><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"><link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" /></head>
<body>
<div class="container mt-5">
<h2 class="text-center mb-3">Laravel 8 HTML to nepali font PDF Example</h2>
<div class="d-flex justify-content-end mb-4">
<a class="btn btn-primary" href="{{route('project.create')}}">Add Project</a>
<div class="col-md">
<a class="btn btn-primary" href="{{route('pdf')}}">Export to PDF</a>
</div>
</div>
</div>
<div class="container mt-5">
@foreach($projects as $project)
<h1 class="text-center">{{$project->title}}</h1><p>{{$project->description}}</p>
@endforeach
</div>
<script src="{{ asset('js/app.js') }}" type="text/js"></script></body>
</html>

Generate a blade file resources/views/project/add.blade.php then insert the following code inside.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Laravel 8 nepali font PDF Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container mt-5">
<h2 class="text-center mb-3">Laravel 8 HTML to nepali font PDF Example</h2>
<form method="POST" action="{{route('project.store')}}" class="w-100 create-form">
@csrf
<div class="row">
{{--title--}}
<div class="col-md-4">
<div class="form-group">
<label for="">त्यैतल</label>
<input type="text" class="form-control" name="title" value="{{old('title')}}">
<label class="error ">{{$errors->first('title')}}</label>
</div>
</div>
</div>
<div class="row">
{{--description--}}
<div class="col-md-4">
<div class="form-group">
<label for="">वर्णन</label>
<!-- <input type="textarea" class="form-control" name="description" value="{{old('description')}}"> -->
<textarea rows="5" class="form-control" name="description">{{old('description')}}</textarea>
<label class="error ">{{$errors->first('description')}}</label></div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<button type="submit" class="btn btn-primary d-block w-100" id="btnSubmit">submit<i class="far fa-arrow-right ml-2"></i>
</button>
</div>
</div>
</form>
</div>
<script src="{{ asset('js/app.js') }}" type="text/js"></script></body>
</html>

Make a fonts directory in the storage folder of your Laravel project. ( storage/fonts )and put your downloaded font .ttf file

In your pdf view add @font-face rules by using the storage_path method Laravel provides.

<style>
@font-face {
font-family: Nirmala;
font-style: normal;
font-weight: 400;
src: url('{{storage_path('fonts/Nirmala.ttf') }}') format('truetype');
}
body {
font-family: Nirmala;
font-size: 14px;
}
</style>

Generate a blade file resources/views/project/nepali_font_pdf_view.blade.php then insert the following code inside.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title class="text-center" >Laravel 8 HTML to nepali font PDF Example</title>
<style>
@font-face {
font-family: Nirmala;
font-style: normal;
font-weight: 400;
src: url('{{storage_path('fonts/Nirmala.ttf') }}') format('truetype');
}
body {
font-family: Nirmala;
font-size: 14px;
}
</style>
</head>
<body style='font-family: Nirmala;'>
<div class="container mt-5">
<h2 class="text-center mb-3">Laravel 8 HTML to nepali font PDF Example</h2>
@foreach($projects as $project)
<span class="col-lg-4 mx-0">{{$project->title}}</span>
<p>{{$project->description}}</p>
@endforeach
</div>
</body>
</html>

Output

--

--

Tamrakar Shreyaa
Tamrakar Shreyaa

Written by Tamrakar Shreyaa

Laravel | PHP | Node| nestjs |API | AJAX | jQuery | Laravel vue | Livewire | LAMP stack | CI CD

Responses (2)