<!DOCTYPE html>
<html>
<head>
    <title>PDF to Image Converter</title>
</head>
<body>
    <h1>PDF to Image Converter</h1>
    <?php
    // Check if form is submitted
    if (isset($_POST['submit'])) {
        // Check if file is uploaded without errors
        if ($_FILES['pdfFile']['error'] === UPLOAD_ERR_OK) {
            $pdfPath = $_FILES['pdfFile']['tmp_name']; // Temporary path of the uploaded file
            $outputFolder = 'output_images'; // Output folder for converted images

            // Convert PDF to images
            pdf_to_images($pdfPath, $outputFolder);
        } else {
            echo "Error uploading file.";
        }
    }
    ?>

    <form method="post" enctype="multipart/form-data">
        <input type="file" name="pdfFile" accept=".pdf">
        <button type="submit" name="submit"><span>Upload PDF</span></button>
    </form>

    <?php
    // Function to convert PDF to images
    function pdf_to_images($pdfPath, $outputFolder) {
        // Check if output folder exists, create it if not
        if (!file_exists($outputFolder)) {
            mkdir($outputFolder, 0777, true);
        }

        // Command to convert PDF to images (using ImageMagick)
        $command = "convert {$pdfPath} {$outputFolder}/output.jpg";

        // Execute the command
        exec($command, $output, $returnCode);

        // Check if command execution was successful
        if ($returnCode === 0) {
            echo "PDF converted to images successfully!";
        } else {
            echo "Error converting PDF to images.";
        }
    }
    ?>
</body>
</html>
