diff --git a/app.php b/app.php index a1197e3..1e11a62 100644 --- a/app.php +++ b/app.php @@ -390,12 +390,39 @@ $f3->route('GET /metadata', $f3->route('GET /compress', function($f3) { - $f3->set('maxSize', min(array(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize'))))); - echo View::instance()->render('compress.html.php'); } ); +$f3->route ('POST /compress', + function($f3) { + $filename = null; + $tmpfile = tempnam($f3->get('UPLOADS'), 'pdfsignature_sign'); + unlink($tmpfile); + + $files = Web::instance()->receive(function($file,$formFieldName) { + if ($formFieldName == "pdf" && strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) { + $f3->error(403); + } + }); + + $arrayPath = array_keys($files); + $filePath = reset($arrayPath); + $outputFileName = str_replace(".pdf", "_compressed.pdf", $filePath); + + $returnCode = shell_exec(sprintf("gs -sDEVICE=pdfwrite -dPDFSETTINGS=/screen -dQUIET -o %s %s", $outputFileName, $filePath)); + + if ($returnCode !== false) { + header('Content-Type: application/pdf'); + header("Content-Disposition: attachment; filename=$outputFileName"); + readfile($outputFileName); + unlink($outputFileName); + } else { + echo "PDF compression failed. Please try again."; + } + } +); + function getCommit() { if(!file_exists(__DIR__.'/.git/HEAD')) { diff --git a/public/js/compress.js b/public/js/compress.js index 32753fb..e69de29 100644 --- a/public/js/compress.js +++ b/public/js/compress.js @@ -1,12 +0,0 @@ -const inputFileElement = document.getElementById('input_pdf_upload'); -const compressBtn = document.getElementById('compressBtn'); - -compressBtn.addEventListener('click', async () => { - const inputPdfFile = inputFileElement.files[0]; - - if (inputPdfFile) { - console.log("ping"); - } else { - alert('Please select a PDF file to compress.'); - } -}) \ No newline at end of file diff --git a/templates/compress.html.php b/templates/compress.html.php index 00d0719..5134d20 100644 --- a/templates/compress.html.php +++ b/templates/compress.html.php @@ -44,18 +44,20 @@ '); ?> -
-

'); ?>

-

-
+
+
+

'); ?>

+

- " class="form-control form-control-lg" type="file" accept=".pdf,application/pdf" /> + " class="form-control form-control-lg" type="file" accept=".pdf,application/pdf" />

 

- -
+
+ +
+ @@ -113,6 +115,6 @@ - - +