ItSolutionStuff.com

Laravel Dompdf Set Custom Paper Size Example

By Hardik Savani • November 5, 2023
Laravel

Hello,

In this short guide, we will show you laravel dompdf custom paper size. This example will help you laravel dompdf set page size. This post will give you a simple example of laravel dompdf set paper size a4. step by step explain dompdf custom page size laravel. follow the below example for how to set custom paper size in pdf laravel dompdf.

In this illustration, I will demonstrate the process of configuring a custom paper size in a PDF document using the Laravel Dompdf library. To accomplish this, we will utilize the setPaper() method, which permits us to define custom paper dimensions in inches, centimeters, points, or pixels. Consequently, you can effortlessly specify an A4 paper size using Dompdf. Let's examine the solution code along with a complete example.

Laravel Dompdf Set Custom Paper Size Solution:

$customPaper = [0, 0, 567.00, 500.80];

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

->setPaper($customPaper, 'landscape');

Laravel Dompdf Set Paper Size A4 Solution:

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

->setPaper('a4', 'landscape');

Now, let's see example step by step:

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Install DomPDF Package

next, we will install DomPDF package using following composer command, let's run bellow command:

composer require barryvdh/laravel-dompdf

Step 3: Create Controller

In this step, we will create PDFController with generatePDF() where we write code of generate pdf. so let's create controller using bellow command.

php artisan make:controller PDFController

Now, update the code on the controller file.

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 ItSolutionStuff.com',

];

$customPaper = [0, 0, 567.00, 500.80];

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

->setPaper($customPaper, 'landscape');

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

}

}

Step 4: Add Route

Furthermore, open routes/web.php file and update code on it.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PDFController;

/*

|--------------------------------------------------------------------------

| 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('generate-pdf', [PDFController::class, 'generatePDF']);

Step 5: Create View File

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 10 Generate PDF Example - ItSolutionStuff.com</title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

</head>

<body>

<h1>{{ $title }}</h1>

<p>Dear Boss,

<br/><br/>

I am writing to formally resign from my position as [Your Position] at [IT Company Name], with my last working day being [Last Working Day], in accordance with the notice period stipulated in my employment contract.

<br/><br/>

I want to express my sincere gratitude for the opportunities and experiences I've had during my time at [IT Company Name]. It has been an incredible journey, and I have had the privilege of working alongside an outstanding team of professionals.

<br/><br/>

After careful consideration, I have decided to take a new direction in my career. This decision wasn't easy, as I have cherished my time here and the projects we've accomplished together. I am immensely proud of our collective achievements.

<br/><br/>

During my notice period, I am committed to ensuring a seamless transition. I am more than willing to assist in the transfer of my responsibilities, provide training to my successor, and complete any pending projects. Please let me know how I can best contribute to this process.

<br/><br/>

Sincerely,

Hardik Savani

</p>

</body>

</html>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/generate-pdf

you will downloaded file as like bellow:

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

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Read Content from PDF File in Laravel?

Read Now →

Laravel 10 Generate PDF and Send Email Example

Read Now →

How to Merge Multiple PDF Files in Laravel 10?

Read Now →

Laravel 10 Mail | Laravel 10 Send Mail Tutorial

Read Now →

Laravel 10 Generate PDF File using DomPDF Example

Read Now →

Laravel Change Mail Driver Dynamically Example

Read Now →

Laravel Mail Send with PDF Attachment Example

Read Now →

How to Add Password Protection for PDF File in Laravel?

Read Now →

Laravel Convert PDF to Image Example

Read Now →

How to Generate PDF and Send Email in Laravel?

Read Now →

Laravel Datatables Export to PDF File Example

Read Now →

Laravel Create PDF File with Image Example

Read Now →

How to Generate PDF with Graph in Laravel?

Read Now →

Laravel 11 Merge Multiple PDF Files Example

Read Now →