Hi,This post is focused on laravel 8 convert pdf to image example. This tutorial will give you simple example of laravel 8 convert pdf to image. This article goes in detailed on laravel 8 pdf to image convert. We will look at example of how to convert pdf to image in laravel 8.
Sometime we may need to convert pdf file to image in php laravel. If you need then i will give you simple example of how to convert pdf to image in laravel 6, laravel 7 and laravel 8 version.
we will use php imagick extension for convert pdf to image file. you must have to install imagick extension for pdf and give to permission. let's see step by step installation and configuration for imagick.
Step 1: Install imagick extension
in first step, we need install imagick extension for php, so let's install it and give permission as bellow:
Install php-imagick:
sudo apt install php-imagick
Check list of php-magick:
sudo apt list php-magick -a
Restart apache2 server:
sudo systemctl restart apache2
Check imagick installed:
php -r 'phpinfo();' | grep imagick
Output:
/etc/php/7.4/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.skip_version_check => 1 => 1
Give Permission to Convert PDF File:
Open following file and update as bellow line into that line:
File Path: /etc/ImageMagick-6/policy.xml
Check imagick installed:
<policy domain="coder" rights="none" pattern="PDF" />
INTO
<policy domain="coder" rights="read|write" pattern="PDF" />
Step 2: Create Route
In this is step we need to create one route as bellow.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('convert-pdf-to-image', [DemoController::class, 'index']);
Step 3: Create Controller
in this step, we need to create DemoController and add following code on that file:
app/Http/Controllers/DemoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Imagick;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$imagick = new Imagick();
$imagick->readImage(public_path('dummy.pdf'));
$saveImagePath = public_path('converted.jpg');
$imagick->writeImages($saveImagePath, true);
return response()->file($saveImagePath);
}
}
Now make sure you have to add "dummy.pdf" put on public folder. After run this url you will able to view image on your browser.
Now we are ready to run our example. so run bellow command so quick run:
php artisan serve
Now you can open bellow URL on your browser:
localhost:8000/convert-pdf-to-image
Output:
I hope it can help you...
Do you like below Tutorials ?
- How to Display Data in Angular 10?
- Angular 10 Routing Module Example Tutorial
- Angular 10 CRUD Operations with Web API Example
- Laravel 8 CRUD Operation Step By Step Tutorial
- Solved - Target class [ProductController] does not exist in Laravel 8
- Step by Step Form Validation in Laravel 8 Example
- Laravel 8 Image Upload with Preview Example
- Laravel 8 Multiple Images Upload with Preview Example