<?php
require './vendor/autoload.php';

use NcJoes\OfficeConverter\OfficeConverter;

$target_dir = "../uploads/";
$file = $_FILES['my_file']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['my_file']['tmp_name'];
$path_filename_ext = $target_dir . $filename . "." . $ext;
$filePdf = $target_dir . $filename . ".pdf";
if (file_exists($path_filename_ext)) {
    echo "檔案已存在!<br>";
} else {
    move_uploaded_file($temp_name, $path_filename_ext);
    echo "上傳成功! 已轉檔成功!<br>";
    $converter = new OfficeConverter($path_filename_ext, $target_dir, "soffice.bin ", false);
    $converter->convertTo($filePdf);
}
echo "<a href='" . $target_dir . $filePdf . "' target='_blank'>預覽PDF</a><br>";
echo "<a download href='" . $target_dir . $filePdf . "' >下載PDF</a>";
echo "<br><a href='pdfConverter.html' >返回上傳</a>";
