<?php
require('fpdf/fpdf.php');

// Get data from the AJAX request
$name = isset($_POST['name']) ? $_POST['name'] : '{{name}}';
$raname = isset($_POST['raname']) ? $_POST['raname'] : '{{raname}}';
$date = isset($_POST['date']) ? $_POST['date'] : '{{date}}';
$address = isset($_POST['address']) ? $_POST['address'] : '{{address}}';
$content = isset($_POST['content']) ? $_POST['content'] : '';
$content = str_replace('&nbsp;', ' ', $content);
$content = str_replace('â€“', '–', $content);
$content = str_replace('â¦•', '*', $content);
$content = str_replace('â‚¹', '₹', $content);
$content = str_replace('â€™', '`', $content);
$content = str_replace('â€œ', '"', $content);
$content = str_replace('â€•', '"', $content);
$content = str_replace('&amp; ', '&', $content);

// Create a new PDF instance
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 14);

// Header
$pdf->Cell(0, 10, 'Agreement', 0, 1, 'C');
$pdf->Ln(8);

// Content
$pdf->SetFont('Arial', '', 12);

// Add dynamic content (plain text from contentPreview)
if (!empty($content)) {
    $pdf->Ln(7);
    $pdf->MultiCell(0, 7, strip_tags($content), 0, 1);
}

// Ensure the directory exists
$pdf_dir = '../upload/pdf/';
if (!is_dir($pdf_dir)) {
    mkdir($pdf_dir, 0755, true); // Create the directory if it doesn't exist
}

// Save the PDF to the specified directory
$pdf_file = $pdf_dir . 'Agreement_' . time() . '.pdf';
$pdf->Output('F', $pdf_file);

// Return the PDF file path as the response
echo $pdf_file;
?>