OXIESEC PANEL
- Current Dir:
/
/
var
/
www
/
3-31-025chanakya
/
hindiConverter
Server IP: 139.59.38.164
Upload:
Create Dir:
Name
Size
Modified
Perms
ЁЯУБ
..
-
03/31/2025 06:36:42 AM
rwxr-xr-x
ЁЯУБ
converted
-
03/26/2025 04:15:50 AM
rwxr-xr-x
ЁЯУД
index.php
5.68 KB
03/26/2025 03:48:05 AM
rw-r--r--
ЁЯУД
process_pdf.php
5.48 KB
03/26/2025 03:48:05 AM
rw-r--r--
ЁЯУБ
uploads
-
03/26/2025 04:15:50 AM
rwxr-xr-x
Editing: process_pdf.php
Close
<?php // process_pdf.php header('Content-Type: application/json'); // Check if the request is a POST request if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['success' => false, 'message' => 'Invalid request method']); exit; } // Check if a file was uploaded if (!isset($_FILES['pdfFile']) || $_FILES['pdfFile']['error'] !== UPLOAD_ERR_OK) { echo json_encode(['success' => false, 'message' => 'No file uploaded or upload error']); exit; } // Create an uploads directory if it doesn't exist $uploadsDir = 'uploads/'; if (!file_exists($uploadsDir)) { mkdir($uploadsDir, 0755, true); } // Create an output directory if it doesn't exist $outputDir = 'converted/'; if (!file_exists($outputDir)) { mkdir($outputDir, 0755, true); } // Get file details $file = $_FILES['pdfFile']; $fileName = $file['name']; $fileTmpName = $file['tmp_name']; $fileError = $file['error']; $fileSize = $file['size']; // Validate file type $fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); if ($fileExtension !== 'pdf') { echo json_encode(['success' => false, 'message' => 'Only PDF files are allowed']); exit; } // Generate unique filename to prevent overwriting $uniqueFileName = uniqid() . '_' . $fileName; $uploadFilePath = $uploadsDir . $uniqueFileName; // Move uploaded file to uploads directory if (!move_uploaded_file($fileTmpName, $uploadFilePath)) { echo json_encode(['success' => false, 'message' => 'Failed to upload file']); exit; } // Path for the converted file $outputFilePath = $outputDir . 'hindi_' . $uniqueFileName; // Check if required libraries are available if (!extension_loaded('imagick') || !class_exists('Imagick')) { // Fallback if Imagick is not available - using pdftk and pdftotext if available if (!isCommandAvailable('pdftk') || !isCommandAvailable('pdftotext')) { echo json_encode([ 'success' => false, 'message' => 'Required libraries (ImageMagick or pdftk/pdftotext) are not installed on the server' ]); unlink($uploadFilePath); // Remove the uploaded file exit; } // Use pdftk for processing processPdfWithPdftk($uploadFilePath, $outputFilePath); } else { // Use Imagick for processing processPdfWithImagick($uploadFilePath, $outputFilePath); } // Return the path to the converted file echo json_encode([ 'success' => true, 'message' => 'File converted successfully', 'file' => $outputFilePath ]); exit; // Functions // Check if a command-line tool is available function isCommandAvailable($command) { $whereIsCommand = (PHP_OS == 'WINNT') ? 'where' : 'which'; $process = proc_open( "$whereIsCommand $command", [ 0 => ["pipe", "r"], 1 => ["pipe", "w"], 2 => ["pipe", "w"], ], $pipes ); if (is_resource($process)) { $stdout = stream_get_contents($pipes[1]); proc_close($process); return !empty($stdout); } return false; } // Map western digits to Hindi digits function convertToHindiDigits($text) { $westernDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; $hindiDigits = ['реж', 'рез', 'реи', 'рей', 'рек', 'рел', 'рем', 'рен', 'рео', 'реп']; return str_replace($westernDigits, $hindiDigits, $text); } // Process PDF using ImageMagick function processPdfWithImagick($inputFile, $outputFile) { try { // Need to implement PDF text extraction, modification, and recreation // This requires advanced PDF manipulation that's beyond simple Imagick functions // For a production environment, consider using libraries like TCPDF, FPDF, or mPDF // For demonstration purposes, we'll use a simple approach // Extract text from PDF using Imagick $im = new Imagick(); $im->readImage($inputFile); // Get number of pages $numPages = $im->getNumberImages(); // Create a new PDF document using a PDF library // This is a placeholder - actual implementation would require using a PDF library // Copy the original file as we don't have full PDF editing capabilities copy($inputFile, $outputFile); // Note: In a real implementation, you would: // 1. Extract text while preserving positions // 2. Replace digits with Hindi equivalents // 3. Generate a new PDF with the replaced text // This requires specialized PDF libraries // Clean up $im->clear(); $im->destroy(); } catch (Exception $e) { echo json_encode(['success' => false, 'message' => 'Error processing PDF: ' . $e->getMessage()]); exit; } } // Process PDF using pdftk and pdftotext (fallback approach) function processPdfWithPdftk($inputFile, $outputFile) { // This is a simplified placeholder implementation // In a real implementation, you would need to: // 1. Extract text while preserving coordinates // 2. Replace digits with Hindi equivalents // 3. Generate a new PDF with the replaced text // For demonstration purposes, we'll just copy the file copy($inputFile, $outputFile); // Note: A complete implementation would require: // - PDF parsing library that preserves text positions // - PDF creation library to reconstruct with Hindi numerals } ?>