In this tutorial you will learn about the Laravel 8 Convert PDF to Image Tutorial Example and its application with practical example.
In this Laravel 8 Convert PDF to Image Tutorial Example tutorial I will show you how to convert pdf to image file in laravel. In this tutorial you will learn to generate or convert a pdf file to image in laravel. In this example we will be using php imagick package to convert pdf to image. In this article I will show you how to install and use imagick to convert pdf to image file. You will also learn to install imagick with laravel. Before starting with this tutorial you are required to install imagick package and to enable imagick package in apache web server.
Laravel 8 Convert PDF to Image Tutorial Example
Use the following steps to convert pdf to image in laravel 8 app:
Step 1: Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Installing Imagick PHP Extension And Configuration
Now in this step we will install imagick library in server using following command:
1 |
sudo apt install php-imagick |
Now, run following command to check the list the version available from the Ubuntu repositories using the apt list command.
1 |
sudo apt list php-magick -a |
The -a flag tells apt to list all version of a package available from the repositories. The output will look similar to the following, and at the time of this writing, there was only a single version available.
1 |
php-imagick/bionic,now 3.4.3~rc2-2ubuntu4 amd64 [installed] |
After this you are required to restart Apache web server:
1 |
sudo systemctl restart apache2 |
Verify Installation
Run the following command to verify the imagick library installation:
1 |
php -m | grep imagick |
If the installation was successful, the output of the command will simply show one line, and it will only contain the name of the module imagick.
1 |
imagick |
You can also verify installation with phpinfo() method. Or execute the following command
1 |
php -r 'phpinfo();' | grep imagick |
Which will output the following information, where the modules status is shown as enabled.
1 2 3 4 5 6 7 8 9 10 |
/etc/php/7.3/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.set_single_thread => 1 => 1 imagick.shutdown_sleep_count => 10 => 10 imagick.skip_version_check => 1 => 1 |
Alternatively, by adding the phpinfo() function to a php script, and then accessing the script from a web browser. Now, you are able to see the module is installed and enabled.
After some authorization change in fowling the path
/etc/ImageMagick-6/policy.xml
1 |
< policy domain="coder" rights="none" pattern="PDF" / > |
To Convert
1 |
< policy domain="coder" rights="read|write" pattern="PDF" / > |
Step 3: Add Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\MyController; Route::get('pdf-to-image', [MyController::class, 'index'])->name('form'); |
Step 3: Create Controller
Now, lets create a controller named MyController using command given below –
1 |
php artisan make:controller MyController |
And put the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php namespace App\Http\Controllers; use Imagick; use Illuminate\Http\Request; use Illuminate\Support\Facades\Blade; class MyController extends Controller { /** * Write code on Method * * @return response() */ public function index() { $imagick = new Imagick(); $imagick->readImage(public_path('dummy.pdf')); $imagick->writeImages('converted.jpg', true); dd("done"); } } |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/pdf-to-image |