From f108f026b9ed7a963bff2361f786d3c766a64c3b Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sun, 13 Jul 2025 22:04:06 +0200 Subject: [PATCH] organic/pdf: Add pdf upload feature --- www/organic/pdf.php | 58 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/www/organic/pdf.php b/www/organic/pdf.php index d19495d..abd21f7 100644 --- a/www/organic/pdf.php +++ b/www/organic/pdf.php @@ -1,9 +1,9 @@ ["pipe", "r"], // stdin + 1 => ["pipe", "w"], // stdout + 2 => ["pipe", "w"], // stderr + ]; + $process = proc_open(['pdftotext', '-raw', '-', '-'], $fd_spec, $pipes); + $input = fopen("php://input", "rb"); + while (!feof($input)) { + if (($buffer = fread($input, 8192)) === false) + break; + fwrite($pipes[0], $buffer); + } + fclose($input); + fclose($pipes[0]); + + fpassthru($pipes[1]); + fclose($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[2]); + $return_value = proc_close($process); + } else { + passthru("curl -s '" . escapeshellarg($url) . "' | pdftotext -raw - -"); + } } else if ($format === 'json') { header('Content-Type: application/json; charset=UTF-8'); @@ -51,13 +75,25 @@ if ($format === 'text') { 1 => ["pipe", "w"], // stdout 2 => ["pipe", "w"], // stderr ]; - $process = proc_open( - ['bash', '-c', - "curl -s " . escapeshellarg($url) . " | " . - "pdftotext -raw - -"], - $fd_spec, - $pipes - ); + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $process = proc_open(['pdftotext', '-raw', '-', '-'], $fd_spec, $pipes); + $input = fopen("php://input", "rb"); + while (!feof($input)) { + if (($buffer = fread($input, 8192)) === false) + break; + fwrite($pipes[0], $buffer); + } + fclose($input); + } else { + $process = proc_open( + ['bash', '-c', + "curl -s " . escapeshellarg($url) . " | " . + "pdftotext -raw - -"], + $fd_spec, + $pipes + ); + } + fclose($pipes[0]); $text = stream_get_contents($pipes[1]); fclose($pipes[1]);