Laravel Convert PDF to Image Example
Hi,
In this quick example, let's see laravel convert pdf to image example. i would like to show you laravel convert pdf to image. We will use laravel pdf to image convert. if you want to see example of how to convert pdf to image in laravel then you are a right place.
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, laravel 8, laravel 9, laravel 10 and laravel 11 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Image Upload with Spatie's Media Library Example
- Laravel Google Autocomplete Address Example
- Laravel Eloquent Select Single Column to Array Example
- Laravel File Manager Tutorial Step by Step
- How to Generate PDF and Send Email in Laravel?
- Laravel 8 PDF | Laravel 8 Generate PDF File using DomPDF
- Laravel Datatables Export to PDF File Example
- Laravel Create PDF File with Image Example
- How to Generate PDF with Graph in Laravel?
- Laravel 11 Merge Multiple PDF Files Example