Laravel 9 Convert PDF Doc to Image File Tutorial

This step by step tutorial will share with you how to convert PDF file to Image in Laravel application using the Imagick PHP extension. On top of that, you will also learn to set up an Imagick extension for PDF to Image conversion.

The Imagick extension gives you innumerable options to work with images in a PHP environment, and you can create, display, update, edit image files whenever or anytime you want.

Once you configure this profound PHP extension on your development environment, you can handle image tasks with multiple file format support.

Not just that, this plugin is free and open-source, which works on cross-platforms without giving you a tough time in developing image conversion-related tasks.

Convert PDF to Image in Laravel 9 Example

Below mentioned steps will make you understand on how to convert pdf to image in laravel effortlessly.

  • Step 1: Create Laravel App
  • Step 2: Setting Up PHP Imagick Extension
  • Step 3: Construct Controller
  • Step 4: Set Up Route
  • Step 5: Run Development Server

Create Laravel App

Commence this tutorial by installing a brand new laravel project using the composer command::

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

Setting Up PHP Imagick Extension

In this step, you must install the Imagick extension, open terminal, and execute the provided command to start the PHP Imagick extension’s installation process.

sudo apt install php-imagick

The below command list all the avilable version through Ubuntu repositories:

sudo apt list php-magick -a
php-imagick/bionic,now 3.4.3~rc2-2ubuntu4 amd64 [installed]

Further, you have to restart the apache web server:

sudo systemctl restart apache2

Also, you can verfify the pacakges’ configuration:

php -m | grep imagick

Following name tells you htat the extension has been added:

imagick

Not only but also the phpinfo() method also helps in verfifying the modules details:

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

You can check the given detials about the extention:

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

Moreover, if you want to check the Imagick details, thereupon use the phpinfo() method and check it on the browser:

Convert PDF to Image in Laravel

Make authorization update on given the path:

/etc/ImageMagick-6/policy.xml

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

To convert:

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

Construct Controller

Next, we need a controller not only create it but also write the logic to covert pdf to image and later we will make the GET request through Route to process the pdf to image:

php artisan make:controller PdfController

Plus, get into the app/controllers/PdfController.php carefully update the given below code within the file:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Imagick;
use Illuminate\Support\Facades\Blade;


class PdfController extends Controller
{
    public function pdfToImage()
    {
        $imgExtension = new Imagick();
        
        $imgExtension->readImage(public_path('pdf_file.pdf'));
        $imgExtension->writeImages('pdftoimage.jpg', true);

        dd("File has been processed.");
    }
}

Set Up Route

In this step, create a new route that will communicate with the controller; likewise, execute the controller’s method to convert the pdf file to an image.

Open routes/web.php file and update the provided code:

<?php

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


Route::get('laravel-pdf-image', [PdfController::class, 'pdfToImage'])->name('form');

Run Development Server

You can run the laravel development server using the suggested command:

php artisan serve

Threafter, you can test the app using the offered url on the browser:

http://localhost:8000/laravel-pdf-image

Summary

You have gone through all the foundational steps respectively, you have found out how to convert a pdf document to an image file using the impeccable Imagick, which is a php extension.

You have only seen the one use of Imagick extension; however, it gives you more than what you may think.

You can resize, sharpen, threshold, annotate, blur images with this plugin, and this is an ultimate elixir for PHP developers looking to customize the image file object.

I believe you will appreciate this guide; hence this Laravel Convert PDF to Image tutorial is over.