How to Convert PDF to Image in Laravel 10

Websolutionstuff | Dec-22-2023 | Categories : Laravel PHP

Greetings, Laravel enthusiasts! Today, let's unravel a common challenge in web development – converting PDFs to images using Laravel 10. Whether you're dealing with document previews, image galleries, or any creative project.

This step-by-step guide will walk you through the process using Imagick. Join me in exploring this functionality and elevating your Laravel skills to a new level.

In this article, we'll see examples of how to convert PDF to image in laravel 8/9/10. You can easily convert PDF to image using Imagick in laravel 10.

Let's see laravel 10 convert PDF to image.

Step 1: Install Imagick Extension

Before diving into PDF to image conversion, make sure you have the Imagick extension installed. Imagick is a powerful PHP extension that provides advanced image manipulation capabilities.

Install php-imagick:

sudo apt install php-imagick

Check php-magick:

sudo apt list php-magick -a

Restart apache2 server:

sudo systemctl restart apache2

Check imagick installed:

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

Give Permission to Convert PDF File:

Open the following file and update as below 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 Controller

Generate a controller for handling the PDF to image conversion:

php artisan make:controller PDFToImageController

In your PDFToImageController.php file, use the following code to convert a PDF to an image:

use Imagick;
use Illuminate\Http\Request;
use Imagick;

class PDFToImageController extends Controller
{
    public function convertPDFToImage(Request $request)
    {
        $pdfPath = $request->file('pdf')->path();

        $imagick = new Imagick();
        $imagick->readImage($pdfPath . '[0]'); // Convert the first page of the PDF

        $imagick->setImageFormat('png'); // Set the output format, you can change it as needed

        $imagePath = public_path('images/') . 'converted_image.png'; // Define the output image path
        $imagick->writeImage($imagePath);
        $imagick->clear();

        return response()->json(['image_path' => $imagePath]);
    }
}

 

Step 3: Define Routes

In your web.php file, define a route to trigger the PDF to image conversion:

use App\Http\Controllers\PDFToImageController;

Route::post('/convert-pdf-to-image', [PDFToImageController::class, 'convertPDFToImage']);

 

Step 4: Test Your Application

Run your Laravel application:

php artisan serve

 

Conclusion:

Congratulations! You've successfully implemented PDF to image conversion in Laravel 10 using Imagick. This powerful feature opens doors to a wide array of possibilities, from document management to creative projects.

 


You might also like:

Recommended Post
Featured Post
Laravel 8 Form Class Not Found
Laravel 8 Form Class Not Found

In this small post, we will solve the laravel 8 form class not found error, many time we have received errors like the l...

Read More

Mar-12-2021

How To Create Calendar Event In Laravel 9 Using AJAX
How To Create Calendar Event I...

In this article, we will see how to create a calendar event in laravel 9 using ajax. Here, we will learn how to add...

Read More

Dec-28-2022

How to Add Bootstrap 5 in Angular 17 Application
How to Add Bootstrap 5 in Angu...

Welcome to this tutorial where I'll guide you through the process of integrating Bootstrap 5 into your Angular 17 ap...

Read More

Mar-25-2024

Laravel 9 Ajax File Upload With Progress Bar
Laravel 9 Ajax File Upload Wit...

In this article, we will see the laravel 9 ajax file upload with a progress bar. we will learn how to file upload using...

Read More

Nov-15-2022