parseFile($pdf_path); $text = $pdf->getText(); // 3. Extract Text Parameters using RegEx $name = ''; $category = 'Architecture & Planning'; // default $location = 'Colombo, Sri Lanka'; // default $completion_date = null; $description = ''; // Match Project Name if (preg_match('/Project\s*Name\s*:\s*(.*)/i', $text, $matches)) { $name = trim($matches[1]); } else { // Fallback to filename if not found in text $name = pathinfo($file_name, PATHINFO_FILENAME); $name = str_replace(['-', '_'], ' ', $name); $name = ucwords($name); } // Match Category if (preg_match('/Category\s*:\s*(.*)/i', $text, $matches)) { $category = trim($matches[1]); } // Match Location if (preg_match('/Location\s*:\s*(.*)/i', $text, $matches)) { $location = trim($matches[1]); } // Match Completion Date if (preg_match('/(?:Completion\s*Date|Date)\s*:\s*([0-9\-]+)/i', $text, $matches)) { $completion_date = trim($matches[1]); } // Match Description if (preg_match('/Description\s*:\s*([\s\S]*?)(?=Category|Location|Date|Images|Before|After|$)/i', $text, $matches)) { $description = trim($matches[1]); } else { // Fallback to snippet of text if description block not found $description = "Extracted from " . $file_name . ". " . substr(strip_tags($text), 0, 250); } // 4. Extract Images from PDF Objects $extracted_images = []; $objects = $pdf->getObjects(); foreach ($objects as $object) { if ($object instanceof \Smalot\PdfParser\Object) { $details = $object->getDetails(); if (isset($details['Subtype']) && $details['Subtype'] === 'Image') { $data = $object->getContent(); if (!empty($data)) { $img_ext = 'jpg'; if (isset($details['Filter'])) { if ($details['Filter'] === 'FlateDecode') $img_ext = 'png'; elseif ($details['Filter'] === 'JPXDecode') $img_ext = 'jp2'; } $img_name = 'ext_' . bin2hex(random_bytes(6)) . '.' . $img_ext; $img_dest = PROJECT_UPLOAD_DIR . $img_name; if (@file_put_contents($img_dest, $data)) { $extracted_images[] = 'uploads/projects/' . $img_name; } } } } } // Assign main images from list $before_img = null; $after_img = null; $sub_images = []; if (count($extracted_images) >= 2) { $before_img = $extracted_images[0]; // first as before $after_img = $extracted_images[1]; // second as after $sub_images = array_slice($extracted_images, 2); } elseif (count($extracted_images) === 1) { $after_img = $extracted_images[0]; // only one image } else { // fallback placeholder if no images extracted $after_img = 'assets/images/gallery-1.jpg'; } // 5. Save Extracted Data into database (projects table) $stmt_proj = $pdo->prepare("INSERT INTO projects (name, description, category, location, completion_date, before_image, after_image, is_featured) VALUES (:name, :description, :category, :location, :completion_date, :before_image, :after_image, 0)"); $stmt_proj->execute([ 'name' => $name, 'description' => $description, 'category' => $category, 'location' => $location, 'completion_date' => $completion_date, 'before_image' => $before_img, 'after_image' => $after_img ]); $new_project_id = $pdo->lastInsertId(); // Save sub-images foreach ($sub_images as $sub_path) { $stmt_sub = $pdo->prepare("INSERT INTO project_images (project_id, image_path) VALUES (:project_id, :image_path)"); $stmt_sub->execute(['project_id' => $new_project_id, 'image_path' => $sub_path]); } // 6. Save upload logging information into pdf_uploads table $meta_json = json_encode([ 'project_id' => $new_project_id, 'name' => $name, 'category' => $category, 'location' => $location, 'completion_date' => $completion_date, 'images_extracted' => count($extracted_images) ]); $stmt_log = $pdo->prepare("INSERT INTO pdf_uploads (file_name, file_path, extracted_data, status) VALUES (:file_name, :file_path, :extracted_data, 'processed')"); $stmt_log->execute([ 'file_name' => $file_name, 'file_path' => $db_pdf_path, 'extracted_data' => $meta_json ]); $success_msg = "PDF successfully uploaded and processed."; // Keep info array to display to admin $extracted_info = [ 'name' => $name, 'category' => $category, 'location' => $location, 'date' => $completion_date, 'desc' => $description, 'images_count' => count($extracted_images) ]; } else { $error_msg = "Failed to copy uploaded file to PDF destination folder."; } } catch (Exception $e) { $error_msg = "PDF Processing Failure: " . $e->getMessage(); } } } } // Fetch past uploads $past_uploads = []; try { $stmt = $pdo->query("SELECT * FROM pdf_uploads ORDER BY id DESC LIMIT 10"); $past_uploads = $stmt->fetchAll(); } catch (PDOException $e) { // offline } require_once __DIR__ . '/header.php'; require_once __DIR__ . '/navbar.php'; ?>

Automated PDF Extractor

Upload Project PDF

Upload a structured project brochure. The extraction module will parse text parameters and rip embedded image elements to generate a portfolio entry instantly.

Extracted Project Properties

Project Name
Category
Location
Completion Date
Images Extracted images
Description Snippet

...

Processing History

Document Title Processed Date Status
Project: Processed
No PDF logs available.