<?php
require_once 'vendor/autoload.php'; // Include PDF parsing library (e.g., Spatie\PdfParser\PdfParser)
function pdfToForm($pdfPath) {
  // Parse the PDF
  $parser = new PdfParser($pdfPath);
  $pages = $parser->getPages();
  // Initialize empty form
  $form = '<form method="post">';
  // Loop through pages and extract form fields
  foreach ($pages as $page) {
    $text = $page->getText();
    $fields = [];
    // Regular expression to match field names and values (adjust as needed)
    preg_match_all('/^(.*?):\s*(.*?)$/m', $text, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
      $fieldName = trim($match[1]);
      $fieldValue = trim($match[2]);
      // Determine field type based on name (e.g., checkbox, radio)
      $fieldType = 'text';
      if (stripos($fieldName, '[]') !== false) {
        $fieldType = 'checkbox';
      }
      // Generate HTML form element
      $fieldHtml = '<label for="' . $fieldName . '">' . $fieldName . '</label>';
      $fieldHtml .= '<input type="' . $fieldType . '" name="' . $fieldName . '" value="' . $fieldValue . '">';
      // Add field to the form
      $fields[] = $fieldHtml;
    }
    // Add page fields to the form
    $form .= '<fieldset>';
    $form .= implode('<br>', $fields);
    $form .= '</fieldset>';
  }
  // Close the form
  $form .= '</form>';
  // Return the generated HTML form
  return $form;
}
// Example usage
$pdfPath = 'path/to/your/pdf.pdf';
$htmlForm = pdfToForm($pdfPath);
// Output the HTML form
echo $htmlForm;
?>