Laravel Convert PDF to Image Example

By Hardik Savani November 5, 2023 Category : Laravel

Hi,

In this quick example, let's see laravel convert pdf to image example. i would like to show you laravel convert pdf to image. We will use laravel pdf to image convert. if you want to see example of how to convert pdf to image in laravel then you are a right place.

Sometime we may need to convert pdf file to image in php laravel. If you need then i will give you simple example of how to convert pdf to image in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 version.

we will use php imagick extension for convert pdf to image file. you must have to install imagick extension for pdf and give to permission. let's see step by step installation and configuration for imagick.

Step 1: Install imagick extension

in first step, we need install imagick extension for php, so let's install it and give permission as bellow:

Install php-imagick:

sudo apt install php-imagick

Check list of php-magick:

sudo apt list php-magick -a

Restart apache2 server:

sudo systemctl restart apache2

Check imagick installed:

php -r 'phpinfo();' | grep imagick

Output:

/etc/php/7.4/cli/conf.d/20-imagick.ini,

imagick

imagick module => enabled

imagick module version => 3.4.4

imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel

imagick.locale_fix => 0 => 0

imagick.progress_monitor => 0 => 0

imagick.skip_version_check => 1 => 1

Give Permission to Convert PDF File:

Open following file and update as bellow line into that line:

File Path: /etc/ImageMagick-6/policy.xml

Check imagick installed:

<policy domain="coder" rights="none" pattern="PDF" />

INTO

<policy domain="coder" rights="read|write" pattern="PDF" />

Step 2: Create Route

In this is step we need to create one route as bellow.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\DemoController;

/*

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

| 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('convert-pdf-to-image', [DemoController::class, 'index']);

Step 3: Create Controller

in this step, we need to create DemoController and add following code on that file:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Imagick;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$imagick = new Imagick();

$imagick->readImage(public_path('dummy.pdf'));

$saveImagePath = public_path('converted.jpg');

$imagick->writeImages($saveImagePath, true);

return response()->file($saveImagePath);

}

}

Now make sure you have to add "dummy.pdf" put on public folder. After run this url you will able to view image on your browser.

Now we are ready to run our example. so run bellow command so quick run:

php artisan serve

Now you can open bellow URL on your browser:

localhost:8000/convert-pdf-to-image

Output:

I hope it can help you...

Tags :
Shares