<?php
$pageTitle = "PPT to PDF Converter - Free PowerPoint Tool";
$pageDescription = "Convert PPT and PPTX files to PDF instantly. Free, fast, secure, and mobile-friendly PowerPoint to PDF converter.";

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['ppt_file'])) {
    header('Content-Type: application/json');

    $uploadDir = __DIR__ . '/uploads/ppt-pdf/';
    $outputDir = __DIR__ . '/uploads/ppt-pdf/converted/';

    if (!file_exists($uploadDir)) mkdir($uploadDir, 0777, true);
    if (!file_exists($outputDir)) mkdir($outputDir, 0777, true);

    $file = $_FILES['ppt_file'];
    $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));

    if (!in_array($ext, ['ppt', 'pptx'])) {
        echo json_encode(['success' => false, 'message' => 'Only PPT and PPTX files are allowed.']);
        exit;
    }

    if ($file['size'] > 50 * 1024 * 1024) he{
        echo json_encode(['success' => false, 'message' => 'File size must be under 50MB.']);
        exit;
    }

    $safeName = 'ppt_' . time() . '_' . rand(1000, 9999) . '.' . $ext;
    $uploadPath = $uploadDir . $safeName;

    if (!move_uploaded_file($file['tmp_name'], $uploadPath)) {
        echo json_encode(['success' => false, 'message' => 'Upload failed.']);
        exit;
    }

    $cmd = 'libreoffice --headless --convert-to pdf --outdir ' . escapeshellarg($outputDir) . ' ' . escapeshellarg($uploadPath) . ' 2>&1';
    shell_exec($cmd);

    $pdfName = pathinfo($safeName, PATHINFO_FILENAME) . '.pdf';
    $pdfPath = $outputDir . $pdfName;

    if (!file_exists($pdfPath)) {
        echo json_encode(['success' => false, 'message' => 'Conversion failed. Make sure LibreOffice is installed on server.']);
        exit;
    }

    echo json_encode([
        'success' => true,
        'message' => 'PPT converted to PDF successfully.',
        'download' => 'uploads/ppt-pdf/converted/' . $pdfName
    ]);
    exit;
}

include 'header.php';
?>

<main class="pptx-page">
<style>
*{box-sizing:border-box}
html,body{margin:0;padding:0;overflow-x:hidden}
.pptx-page{
  width:100vw;
  margin-left:calc(50% - 50vw);
  margin-right:calc(50% - 50vw);
  font-family:Arial,sans-serif;
  background:
    radial-gradient(circle at top left,rgba(99,102,241,.18),transparent 35%),
    radial-gradient(circle at top right,rgba(236,72,153,.16),transparent 35%),
    linear-gradient(135deg,#f3f4ff,#fff7fb);
  color:#101828;
}
.pptx-wrap{padding:70px 4vw}
.pptx-hero{text-align:center;max-width:900px;margin:0 auto 45px}
.pptx-badge{
  display:inline-block;background:#ede9fe;color:#6d28d9;
  padding:10px 20px;border-radius:50px;font-weight:900;margin-bottom:18px
}
.pptx-hero h1{font-size:68px;line-height:1;margin:0 0 18px;color:#111827}
.pptx-hero p{font-size:20px;color:#475467;line-height:1.7;margin:0 auto;max-width:760px}

.pptx-tool{
  background:rgba(255,255,255,.88);
  border:1px solid rgba(255,255,255,.85);
  border-radius:38px;
  padding:36px;
  box-shadow:0 35px 100px rgba(17,24,39,.14);
  margin-bottom:34px;
}
.pptx-top{display:flex;justify-content:space-between;align-items:center;gap:18px;margin-bottom:28px}
.pptx-top h2{font-size:36px;margin:0}
.pptx-free{background:#dcfce7;color:#047857;padding:11px 18px;border-radius:50px;font-weight:900}
.pptx-grid{display:grid;grid-template-columns:1.1fr .9fr;gap:28px}
.pptx-panel{
  background:#fff;border:1px solid #e0e7ff;border-radius:30px;
  padding:28px;box-shadow:0 16px 45px rgba(17,24,39,.06)
}
.drop-zone{
  min-height:270px;border:3px dashed #a78bfa;border-radius:30px;
  display:flex;align-items:center;justify-content:center;text-align:center;
  background:linear-gradient(135deg,#f5f3ff,#fff);
  cursor:pointer;transition:.25s
}
.drop-zone.dragover,.drop-zone:hover{
  border-color:#7c3aed;
  transform:translateY(-4px);
  box-shadow:0 20px 50px rgba(124,58,237,.15)
}
.drop-zone input{display:none}
.drop-zone b{display:block;font-size:31px;color:#6d28d9;margin-bottom:10px}
.drop-zone span{color:#667085;font-size:16px}
.preview-box{
  margin-top:20px;
  background:#f8fafc;
  border:1px solid #e5e7eb;
  border-radius:22px;
  padding:18px;
  display:none;
}
.preview-item{display:flex;align-items:center;gap:14px}
.preview-icon{
  width:62px;height:62px;border-radius:18px;
  background:linear-gradient(135deg,#7c3aed,#ec4899);
  color:#fff;display:flex;align-items:center;justify-content:center;
  font-weight:900;font-size:18px
}
.preview-name{font-weight:900;color:#111827}
.preview-size{font-size:14px;color:#667085;margin-top:5px}
.btn-row{display:flex;flex-wrap:wrap;gap:14px;margin-top:22px}
.pptx-btn{
  border:0;border-radius:18px;padding:17px 25px;
  font-weight:900;cursor:pointer;transition:.22s
}
.pptx-btn:hover{transform:translateY(-3px)}
.primary{background:linear-gradient(135deg,#7c3aed,#4f46e5);color:#fff}
.dark{background:#111827;color:#fff}
.light{background:#ede9fe;color:#6d28d9}
.result-card{
  min-height:250px;border-radius:28px;padding:30px;
  background:radial-gradient(circle at top right,rgba(124,58,237,.16),transparent 35%),linear-gradient(135deg,#f5f3ff,#fff);
  border:1px solid #ddd6fe
}
.result-card h3{font-size:32px;margin:0 0 12px;color:#6d28d9}
.result-card p{font-size:17px;color:#475467;line-height:1.7;margin:0}
.progress-wrap{
  margin-top:22px;background:#ede9fe;border-radius:50px;
  overflow:hidden;height:16px;display:none
}
.progress-bar{
  width:0%;height:100%;
  background:linear-gradient(135deg,#7c3aed,#ec4899);
  transition:.25s
}
.download-area a{
  display:inline-block;margin-top:22px;
  background:linear-gradient(135deg,#22c55e,#15803d);
  color:#fff;text-decoration:none;padding:17px 26px;
  border-radius:18px;font-weight:900
}

.vip-info{
  position:relative;overflow:hidden;background:linear-gradient(135deg,#fff,#fbfaff);
  border:1px solid #e9d5ff;border-radius:38px;padding:44px;margin:34px 0;
  box-shadow:0 25px 70px rgba(17,24,39,.08)
}
.vip-info:before{
  content:"";position:absolute;width:260px;height:260px;border-radius:50%;
  background:rgba(124,58,237,.08);top:-120px;right:-90px
}
.info-head{text-align:center;max-width:850px;margin:0 auto 32px;position:relative}
.mini{
  display:inline-block;background:#f3e8ff;color:#7e22ce;
  padding:8px 16px;border-radius:30px;font-size:13px;font-weight:900;margin-bottom:12px
}
.info-head h2{font-size:40px;margin:0 0 14px;color:#111827}
.info-head p{font-size:18px;line-height:1.8;color:#475467;margin:0}
.card-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:20px;position:relative}
.info-card{
  background:#fff;border:1px solid #e9d5ff;border-radius:28px;
  padding:28px 22px;text-align:center;transition:.25s;
  box-shadow:0 14px 35px rgba(17,24,39,.05)
}
.info-card:hover{transform:translateY(-6px);box-shadow:0 22px 50px rgba(124,58,237,.14)}
.icon{
  width:58px;height:58px;border-radius:18px;margin:0 auto 16px;
  display:flex;align-items:center;justify-content:center;
  background:linear-gradient(135deg,#7c3aed,#ec4899);
  color:#fff;font-size:25px;font-weight:900
}
.info-card b{display:block;font-size:20px;color:#111827;margin-bottom:9px}
.info-card span{color:#475467;font-size:16px;line-height:1.7}
.about-layout{display:grid;grid-template-columns:1fr 1.25fr;gap:25px}
.about-main{
  background:linear-gradient(135deg,#6d28d9,#111827);
  border-radius:30px;padding:32px;color:#fff
}
.about-main h3{font-size:30px;margin:0 0 14px}
.about-main p{font-size:17px;line-height:1.8;color:#f3e8ff;margin:0}
.feature-grid{display:grid;grid-template-columns:repeat(2,1fr);gap:18px}
.feature{
  background:#fff;border:1px solid #e9d5ff;border-radius:26px;padding:24px
}
.feature b{display:block;color:#7c3aed;font-size:20px;margin-bottom:8px}
.feature span{color:#475467;line-height:1.7}
.formula-box{
  background:linear-gradient(135deg,#111827,#6d28d9);
  border-radius:34px;padding:38px;color:#fff;text-align:center
}
.formula-line{
  display:inline-block;background:rgba(255,255,255,.12);
  border:1px solid rgba(255,255,255,.25);
  padding:18px 28px;border-radius:24px;font-size:22px;font-weight:900;margin-bottom:18px
}
.formula-box p{color:#f3e8ff;font-size:17px;line-height:1.8;max-width:780px;margin:auto}
.two-grid{display:grid;grid-template-columns:1fr 1fr;gap:24px}
.list-box{
  background:#fff;border:1px solid #e9d5ff;border-radius:30px;
  padding:30px;box-shadow:0 16px 40px rgba(17,24,39,.05)
}
.list-box h3{font-size:26px;margin:0 0 18px;color:#111827}
.list-box li{color:#475467;line-height:1.9;font-size:16px}
.faq-grid{display:grid;grid-template-columns:repeat(2,1fr);gap:20px}
.faq-card{
  background:#fff;border:1px solid #e9d5ff;border-radius:28px;
  padding:26px;transition:.25s
}
.faq-card:hover{transform:translateY(-4px);border-color:#c084fc}
.faq-card h3{font-size:21px;margin:0 0 10px;color:#7c3aed}
.faq-card p{margin:0;color:#475467;font-size:16px;line-height:1.7}

@media(max-width:900px){
  .pptx-wrap{padding:45px 18px}
  .pptx-hero h1{font-size:44px}
  .pptx-tool{padding:22px;border-radius:28px}
  .pptx-top{flex-direction:column;align-items:flex-start}
  .pptx-grid,.card-grid,.about-layout,.feature-grid,.two-grid,.faq-grid{grid-template-columns:1fr}
  .vip-info{padding:26px;border-radius:28px}
  .info-head h2{font-size:30px}
}
</style>

<section class="pptx-wrap">

  <div class="pptx-hero">
    <div class="pptx-badge">Free Online PowerPoint Tool</div>
    <h1>PPT to PDF Converter</h1>
    <p>Convert PPT and PPTX files into clean PDF documents with drag and drop, progress status, and instant download.</p>
  </div>

  <section class="pptx-tool">
    <div class="pptx-top">
      <h2>Use PPT to PDF Converter</h2>
      <div class="pptx-free">100% Free & Instant</div>
    </div>

    <div class="pptx-grid">
      <div class="pptx-panel">
        <label class="drop-zone" id="dropZone">
          <input type="file" id="pptFile" accept=".ppt,.pptx">
          <div>
            <b>Drag & Drop PPT File</b>
            <span>or click to upload PPT / PPTX file</span>
          </div>
        </label>

        <div class="preview-box" id="previewBox">
          <div class="preview-item">
            <div class="preview-icon">PPT</div>
            <div>
              <div class="preview-name" id="previewName">presentation.pptx</div>
              <div class="preview-size" id="previewSize">0 MB</div>
            </div>
          </div>
        </div>

        <div class="btn-row">
          <button class="pptx-btn primary" id="convertBtn">Convert to PDF</button>
          <button class="pptx-btn light" id="clearBtn">Clear File</button>
          <button class="pptx-btn dark" id="copyBtn">Copy Result</button>
        </div>
      </div>

      <div class="pptx-panel">
        <div class="result-card" id="resultBox">
          <h3>Ready</h3>
          <p>Upload your PPT or PPTX file. Your PDF download button will appear here.</p>
        </div>

        <div class="progress-wrap" id="progressWrap">
          <div class="progress-bar" id="progressBar"></div>
        </div>

        <div class="download-area" id="downloadArea"></div>
      </div>
    </div>
  </section>

  <section class="vip-info">
    <div class="info-head">
      <span class="mini">Simple Steps</span>
      <h2>How to Use This Converter</h2>
      <p>Convert your PowerPoint file into PDF with a clean and simple process.</p>
    </div>

    <div class="card-grid">
      <div class="info-card"><div class="icon">1</div><b>Upload PPT</b><span>Select or drag your PPT/PPTX file.</span></div>
      <div class="info-card"><div class="icon">2</div><b>Check Preview</b><span>See file name and size before conversion.</span></div>
      <div class="info-card"><div class="icon">3</div><b>Convert</b><span>Click convert and track progress.</span></div>
      <div class="info-card"><div class="icon">4</div><b>Download PDF</b><span>Save your converted PDF file.</span></div>
    </div>
  </section>

  <section class="vip-info">
    <div class="info-head">
      <span class="mini">Tool Benefits</span>
      <h2>About This PPT to PDF Tool</h2>
      <p>This converter helps you turn PowerPoint presentations into PDF files for sharing, printing, study, work, and online use.</p>
    </div>

    <div class="about-layout">
      <div class="about-main">
        <h3>Why Use This Tool?</h3>
        <p>PDF files are easier to share and open on any device. This tool keeps your presentation clean and simple.</p>
      </div>

      <div class="feature-grid">
        <div class="feature"><b>⚡ Fast</b><span>Convert PowerPoint files quickly.</span></div>
        <div class="feature"><b>📄 Clean PDF</b><span>Creates easy-to-share PDF files.</span></div>
        <div class="feature"><b>📱 Responsive</b><span>Works on mobile and desktop.</span></div>
        <div class="feature"><b>✅ Free</b><span>No signup or payment needed.</span></div>
      </div>
    </div>
  </section>

  <section class="vip-info">
    <div class="info-head">
      <span class="mini">Easy Formula</span>
      <h2>Conversion Formula & Explanation</h2>
    </div>

    <div class="formula-box">
      <div class="formula-line">PPT / PPTX File → PDF Document → Download</div>
      <p>The tool uploads your PowerPoint file, converts each slide into PDF format, and gives you a downloadable PDF file.</p>
    </div>
  </section>

  <section class="vip-info">
    <div class="info-head">
      <span class="mini">Helpful Advice</span>
      <h2>Common Mistakes & Tips</h2>
      <p>Follow these tips for better PDF output.</p>
    </div>

    <div class="two-grid">
      <div class="list-box">
        <h3>❌ Common Mistakes</h3>
        <ul>
          <li>Uploading unsupported files.</li>
          <li>Using very large presentations.</li>
          <li>Using missing fonts inside slides.</li>
        </ul>
      </div>

      <div class="list-box">
        <h3>✅ Pro Tips</h3>
        <ul>
          <li>Use PPTX format for best results.</li>
          <li>Keep file size under 50MB.</li>
          <li>Check your slides before upload.</li>
        </ul>
      </div>
    </div>
  </section>

  <section class="vip-info">
    <div class="info-head">
      <span class="mini">Quick Answers</span>
      <h2>FAQs</h2>
      <p>Here are simple answers about this PPT to PDF converter.</p>
    </div>

    <div class="faq-grid">
      <div class="faq-card"><h3>Is this PPT to PDF converter free?</h3><p>Yes. This tool is free to use.</p></div>
      <div class="faq-card"><h3>Which files are supported?</h3><p>You can upload PPT and PPTX files.</p></div>
      <div class="faq-card"><h3>Why is LibreOffice needed?</h3><p>It helps the server convert PowerPoint files into PDF correctly.</p></div>
      <div class="faq-card"><h3>Can I use it on mobile?</h3><p>Yes. The page is fully responsive.</p></div>
    </div>
  </section>

</section>

<script>
const pptFile = document.getElementById("pptFile");
const dropZone = document.getElementById("dropZone");
const previewBox = document.getElementById("previewBox");
const previewName = document.getElementById("previewName");
const previewSize = document.getElementById("previewSize");
const convertBtn = document.getElementById("convertBtn");
const clearBtn = document.getElementById("clearBtn");
const copyBtn = document.getElementById("copyBtn");
const resultBox = document.getElementById("resultBox");
const progressWrap = document.getElementById("progressWrap");
const progressBar = document.getElementById("progressBar");
const downloadArea = document.getElementById("downloadArea");

let selectedFile = null;
let lastResult = "";

function showResult(title,text){
  lastResult = title + ": " + text;
  resultBox.innerHTML = `<h3>${title}</h3><p>${text}</p>`;
}

function setProgress(percent){
  progressWrap.style.display = "block";
  progressBar.style.width = percent + "%";
}

function formatSize(bytes){
  return (bytes / (1024 * 1024)).toFixed(2) + " MB";
}

function handleFile(file){
  if(!file) return;

  const ext = file.name.split(".").pop().toLowerCase();

  if(!["ppt","pptx"].includes(ext)){
    showResult("Invalid File","Only PPT and PPTX files are allowed.");
    return;
  }

  if(file.size > 50 * 1024 * 1024){
    showResult("File Too Large","Please upload a file under 50MB.");
    return;
  }

  selectedFile = file;
  previewBox.style.display = "block";
  previewName.textContent = file.name;
  previewSize.textContent = formatSize(file.size);
  downloadArea.innerHTML = "";
  setProgress(0);
  showResult("File Added","Your presentation is ready to convert.");
}

pptFile.addEventListener("change",()=>handleFile(pptFile.files[0]));

dropZone.addEventListener("dragover",(e)=>{
  e.preventDefault();
  dropZone.classList.add("dragover");
});

dropZone.addEventListener("dragleave",()=>{
  dropZone.classList.remove("dragover");
});

dropZone.addEventListener("drop",(e)=>{
  e.preventDefault();
  dropZone.classList.remove("dragover");
  handleFile(e.dataTransfer.files[0]);
});

clearBtn.addEventListener("click",()=>{
  selectedFile = null;
  pptFile.value = "";
  previewBox.style.display = "none";
  downloadArea.innerHTML = "";
  progressWrap.style.display = "none";
  progressBar.style.width = "0%";
  showResult("Ready","Upload your PPT or PPTX file. Your PDF download button will appear here.");
});

copyBtn.addEventListener("click",async()=>{
  if(!lastResult) return;
  await navigator.clipboard.writeText(lastResult);
  showResult("Copied","Result copied successfully.");
});

convertBtn.addEventListener("click",()=>{
  if(!selectedFile){
    showResult("No File Selected","Please upload a PPT or PPTX file first.");
    return;
  }

  const formData = new FormData();
  formData.append("ppt_file", selectedFile);

  const xhr = new XMLHttpRequest();
  xhr.open("POST", window.location.href, true);

  xhr.upload.onprogress = function(e){
    if(e.lengthComputable){
      const percent = Math.round((e.loaded / e.total) * 70);
      setProgress(percent);
      showResult("Uploading","Please wait. Uploading your presentation...");
    }
  };

  xhr.onload = function(){
    setProgress(100);

    try{
      const res = JSON.parse(xhr.responseText);

      if(res.success){
        downloadArea.innerHTML = `<a href="${res.download}" download>Download PDF</a>`;
        showResult("PDF Ready","Your PowerPoint file has been converted successfully.");
      }else{
        showResult("Conversion Failed",res.message);
      }
    }catch(error){
      showResult("Server Error","Please check server settings and try again.");
    }
  };

  xhr.onerror = function(){
    showResult("Upload Failed","Please try again.");
  };

  xhr.send(formData);
});
</script>

</main>

<?php include 'footer.php'; ?>