Pages

PHP code to convert .pdf to image

 

To input a PDF file and generate JPG files with 600 DPI in PHP, you can use the Imagick extension, which is a PHP wrapper for the ImageMagick library. ImageMagick can convert PDFs to images, and it supports adjusting the resolution (DPI) for high-quality image generation.

Here’s the PHP code to upload a PDF file and convert it into JPG images with 600 DPI:

Prerequisites:

  1. ImageMagick must be installed on your server.
  2. Imagick PHP extension must be enabled in your PHP configuration.

If Imagick is not installed, you can usually install it via your package manager or using pecl for PHP.

PHP Code:

<?php

// Check if a file was uploaded
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['pdfFile']) && $_FILES['pdfFile']['error'] == 0) {
    // Get the uploaded file info
    $pdfFile = $_FILES['pdfFile']['tmp_name'];
    $pdfFileName = $_FILES['pdfFile']['name'];

    // Set the output directory for JPG images
    $outputDir = 'output_images/';
    if (!file_exists($outputDir)) {
        mkdir($outputDir, 0777, true);
    }

    try {
        // Create an Imagick object and read the PDF
        $imagick = new Imagick();
        $imagick->setResolution(600, 600); // Set DPI to 600
        $imagick->readImage($pdfFile); // Read the PDF file

        // Loop through all pages and convert them to JPG
        $numPages = $imagick->getNumberImages();
        for ($i = 0; $i < $numPages; $i++) {
            $imagick->setIteratorIndex($i); // Set the current page to be processed
            $outputFile = $outputDir . 'page_' . ($i + 1) . '.jpg';

            // Set image format to JPG and save it
            $imagick->setImageFormat('jpg');
            $imagick->writeImage($outputFile);
            echo "Generated JPG for page " . ($i + 1) . ": <a href='$outputFile' target='_blank'>$outputFile</a><br />";
        }

        // Clear Imagick object
        $imagick->clear();

    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
    }
} else {
    echo "Please upload a PDF file.";
}

?>

<!-- HTML Form to upload PDF -->
<form action="" method="POST" enctype="multipart/form-data">
    <label for="pdfFile">Upload PDF:</label>
    <input type="file" name="pdfFile" accept=".pdf" required>
    <button type="submit">Upload and Convert</button>
</form>

Explanation:

  1. PDF Upload: The user uploads a PDF file through the HTML form.
  2. Imagick Setup:
    • setResolution(600, 600): This sets the resolution to 600 DPI (dots per inch) for high-quality image conversion.
    • readImage($pdfFile): This loads the uploaded PDF file into Imagick.
    • getNumberImages(): Returns the number of pages in the PDF.
  3. Page Conversion: Each page of the PDF is processed in a loop. The page is converted to a JPG image with setImageFormat('jpg') and saved using writeImage().
  4. Output Directory: JPG files are saved to the output_images/ folder. If the folder doesn't exist, it is created automatically.
  5. Linking Images: After each image is generated, a clickable link is displayed to view the JPG.

Notes:

  • The JPG images will be saved in the output_images/ folder. You can customize this directory or allow the user to specify the location.
  • The code assumes that the server has ImageMagick and Imagick installed. If not, you need to install them to enable this functionality.

No comments:

Post a Comment