Laravel 8 How-To: Convert PDF Document to Image Tutorial
Laravel 8 convert pdf document to an image tutorial; with the help of this extensive guide, you will find out the profound technique on how to convert PDF to image in laravel 8 application using the 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.
In this tutorial, we will share with you how to install PHP imagick extension and how to set it up in a PHP environment using the command line. For this laravel pdf to image tutorial, we will use this impeccable library to convert a PDF file to an image file in the Laravel app.
Laravel 8 Convert PDF to Image Example
Follow the recommended steps gradually to create the demo app:
- Step 1: Construct Laravel Project
- Step 2: Install PHP Imagick Extension
- Step 3: Generate and Configure Controller
- Step 4: Create Route
- Step 5: Try Out Laravel App
Construct Laravel Project
Make sure you have PHP and Composer already installed on your system, after which you can create a new laravel application:
composer create-project --prefer-dist laravel/laravel demo-app
Thereafter, get inside the newly created project:
Install PHP Imagick Extension
This tutorial’s foundational step is to install the PHP Imagick extension, head over to the terminal window, run the suggested command; it will install the imagick extension on the fly.
sudo apt install php-imagick
The recommended command helps in listing all the available versions via Ubuntu repositories:
sudo 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 systemctl restart apache2
Here is another command which helps you 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:
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" / >
Generate and Configure Controller
Further, go to terminal use the php artisan command to generate or make a new controller:
php artisan make:controller ImageController
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 ImageController extends Controller
{
public function index()
{
$imgExt = new Imagick();
$imgExt->readImage(public_path('pdf-document.pdf'));
$imgExt->writeImages('pdf_image_doc.jpg', true);
dd("Document has been converted");
}
}
Create Route
In this step, create a new route that will make the GET request in conjunction with the associated controller to convert pdf to image.
Hence open routes/web.php file import the controller on the top section, next define the Route and declare the Route’s name also pass the controller name inside of it.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;
/*
|--------------------------------------------------------------------------
| Web Route
|--------------------------------------------------------------------------
*/
Route::get('convert-pdf-to-image', [ImageController::class, 'index'])->name('form');
Start Laravel App
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
Conclusion
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.