Laravel 9 Convert Pdf Document File to Image Example Tutorial

Today in this tutorial we are going to share laravel 9 convert pdf docs to image tutorial with example. Many times we need to convert pdf document file to show in image and we can do it with awesome PHP imagick extension.

The imagick PHP extension is free on top of that, an open-source, cross-platform library for showing, creating, transforming, edition images. It is a robust plugin that supports an extensive array of image file formats; generically, it is broadly used in open source web applications.

So let’s see how to install PHP imagick extension how this package convert pdf to image easily in laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 versions.

Step 1: Install Laravel Project

First we will install here a new project of laravel, you can impletment in existing app.

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

Step 2: Install PHP Imagick Extension

If you are using ubuntu please use sudo before the following command.

apt get install php-imagick

The recommended command helps in listing all the available versions via Ubuntu repositories:

apt list php-magick -a

The -a tag commands to apt to list down all the package version:

php-imagick/bionic,now 3.4.3~rc2-2ubuntu4 amd64 [installed]

In the subsequent step, you require to restart the apache web server with offred command:

sudo service apache2 restart

If yo are using window restart the xamp or wamp or namp server which you are using.

The following command will confirms the module’s configuration.

php -m | grep imagick

If you are seeing the extension name on the console screen that means installation done as expected.

imagick

The other way to verify the module installation is to execute the phpinfo() method from the terminal window.

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

You can check the given detials about the extention same as below.

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.set_single_thread => 1 => 1
imagick.shutdown_sleep_count => 10 => 10
imagick.skip_version_check => 1 => 1

On the other hand, the phpinfo() method can also give you access to the module information directly on a web browser:

Please make some update on given path…

/etc/ImageMagick-6/policy.xml

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

To convert:

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

Step 3: Create Controller

Further, go to terminal use the php artisan command to generate or make a new controller:

php artisan make:controller FileController

In response to the execution of the above command, a new controller file has generated; hence append the following code in the app/controllers/ImageController.php file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use Imagick;


class FileController extends Controller
{
    public function index()
    {
        $imgExt = new Imagick();
        $imgExt->readImage(public_path('pdf-document.pdf'));
        $imgExt->writeImages('pdf_image.jpg', true);

        dd("Document has been converted");
    }
}

Step 4: Create Route

Here we add a route for converting pdf document to image file. Add the following route in your web.php file.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileController;

/*
|--------------------------------------------------------------------------
| Web Route
|--------------------------------------------------------------------------
*/

Route::get('convert-pdf-to-image', [FileController::class, 'index'])->name('image-form');

Step 5: Run Server

Subsequently, head over to console afterward run the command similarly run the laravel application:

php artisan serve

Here is the url for testing the laravel demo app:

http://localhost:8000/convert-pdf-to-image

Ultimately, the laravel pdf to image converter tutorial is over; in this comprehensive guide, we studied bit by bit how to convert pdf to image using the imagick open-source extension.

Leave a Comment