Laravel 8 Convert PDF Document to Image File Tutorial

Share this Article
Reading Time: 6 minutes
888 Views

Inside this article we will see step by step guide to convert a PDF file into an image file in Laravel 8. Tutorial is well explained using an example to understand.

While working with this article also you need to know some PHP extension or modules. To work with images in PHP few extension must be enabled in your system. You will get the complete idea of Laravel 8 Covert PDF Document to Image File.

Learn More –

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Install imagick extension

To work with Images and their manipulations tasks in PHP, a extension must be either installed or enabled in system i.e php-imagick

Let’s install in system (in not installed).

Open terminal and run these commands –

#Install

$ sudo apt install php-imagick

#Check list of php-magick

$ sudo apt list php-magick -a

#Restart apache2 server

$ sudo systemctl restart apache2

Successfully, now you have installed extension into system.

Next, to verify follow these –

#Check imagick installed

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

You will get something like in your terminal –

This is the first way to check installation in system. In alternative way or the best way to create a file info.php in your localhost directory.

Open info.php and write this code –

<?php

phpinfo();

Open browser and run it –

http://localhost/info.php

You will see the list of all settings of PHP. Inside that if you scroll, then you will see like this –


Allow Permission to policy.xml of ImageMagick-6

Open terminal and type these commands –

$ cd /etc

$ cd /ImageMagick-6

OR

$ cd /etc/ImageMagick-6

Next, to list files from /etc/ImageMagick-6/ type ls (list files)

$ ls

Now, you will see a file i.e policy.xml. Need to pattern PDF for read and write from here.

Open policy.xml and search for PDF

Initial Line –

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

Update it to like –

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

Create a Controller

Open laravel project into terminal and run this artisan command –

$ php artisan make:controller SampleController

It will create SampleController.php inside /app/Http/Controllers folder.

Put a sample pdf file into /public directory for testing. For this article we took a onlinewebtutorblog.pdf file inside /public folder.

Open SampleController.php and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Imagick;

class SampleController extends Controller
{
    public function index()
    {
        $imagick = new Imagick();
  
        $imagick->readImage(public_path('onlinewebtutorblog.pdf'));
  
        $saveImagePath = public_path('my-image.jpg');
        $imagick->writeImages($saveImagePath, true);
  
        return response()->file($saveImagePath);
    }
}

PDF file location –

  • public_path(‘onlinewebtutorblog.pdf’) – /public/onlinewebtutorblog.pdf

Converted Image file location

  • public_path(‘my-image.jpg’) – /public/my-image.jpg

Add Route

Open web.php from /routes folder and add this route into it.

//...
use App\Http\Controllers\SampleController;

Route::get('convert-pdf-to-image', [SampleController::class, 'index']);
//...

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL – http://127.0.0.1:8000/convert-pdf-to-image

When you run this, you will see inside your /public folder as –

We hope this article helped you to learn Laravel 8 Convert PDF Document to Image File in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here