Mercurial > pdfconcat
diff pdfconcat.php @ 0:8f7e68d54c6d
initial commit: should be already usable
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Thu, 29 Aug 2013 13:58:17 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pdfconcat.php Thu Aug 29 13:58:17 2013 +0200 @@ -0,0 +1,133 @@ +<!-- + pdfconcat-0.1 + Written by markus schnalke <meillo@marmaro.de>, + developed at KIT-Library, Karlsruhe. + This is free software under the CC0 license. + http://marmaro.de/prog/pdfconcat +--> +<html> +<head> +<title>PDF concat and detextify</title> +<meta name="author" content="markus schnalke <meillo@marmaro.de>"> +<meta name="copyright" content="No copyright applies."> +</head> +<body> +<h2>PDF concat and detextify</h2> + +<?php + +define('PDFDETEXTIFY', dirname(__FILE__).'/bin/pdfdetextify'); +define('PDFCONCAT', dirname(__FILE__).'/bin/pdfconcat'); + +define('LOGFILE', dirname(__FILE__).'/log'); +define('UPLOADDIR', 'upload'); + + +function +detextify($file) +{ + $newfile = tempnam(sys_get_temp_dir(), basename(__FILE__)."."); + $cmd = sprintf("%s %s 2>&1 >%s", PDFDETEXTIFY, $file, $newfile); + system($cmd); + return $newfile; +} + + +function +concatpdfs($files) +{ + $newfile = sprintf("%s/%s/%s.pdf", dirname(__FILE__), UPLOADDIR, + date('Y-m-d_H-i-s')); + $cmd = sprintf("%s %s 2>&1 >%s", PDFCONCAT, implode(' ', $files), + $newfile); + system($cmd); + foreach ($files as $file) { + unlink($file); + } + return sprintf("%s/%s", UPLOADDIR, basename($newfile)); +} + + +function +procfiles() +{ + $date = date("Y-m-d H:i:s"); + $ip = $_SERVER['REMOTE_ADDR']; + $files = array(); + foreach ($_FILES as $key => $val) { + if ($val['error'] == UPLOAD_ERR_NO_FILE) { + continue; + } + if ($val['error'] > 0) { + echo "Errors in transferring $val[name]. Skipping.\n"; + echo "($val[error])\n"; + continue; + } + if (isset($_POST[$key.'detextify']) && $_POST[$key.'detextify'] == 'on') { + $files[] = detextify($val['tmp_name']); + } else { + $files[] = $val['tmp_name']; + } + } + $newfile = concatpdfs($files); + // log + $logmsg = sprintf("[%s] %s creates `%s'\n", $date, $ip, $newfile); + file_put_contents(LOGFILE, $logmsg, FILE_APPEND); + + return $newfile; + +} + + +// main() + +if (isset($_POST['submit'])) { + echo '<pre>'; + $outfile = procfiles(); + echo '</pre>'; + echo '<hr>'; + echo '<h2><a href="'. $outfile .'">The concatenated PDF</a></h2>'; + echo '<hr>'; +} + +?> + + +<p> + This webservice concatenates PDF files and optionally converts their + text to bitmaps. +</p> +<p> + The files are stored temporary on the webserver. The detextification + function modifies them. Use this service only of you have the + appropriate rights on the files. +</p> + +<form action="<?php echo basename($_SERVER['SCRIPT_NAME']); ?>" + method="post" enctype="multipart/form-data"> +<p> + <input type="file" name="pdf1" /> + detextify? <input type="checkbox" name="pdf1detextify" /> + <br /> + <input type="file" name="pdf2" /> + detextify? <input type="checkbox" name="pdf2detextify" /> + <br /> + <input type="file" name="pdf3" /> + detextify? <input type="checkbox" name="pdf3detextify" /> + <br /> + <input type="file" name="pdf4" /> + detextify? <input type="checkbox" name="pdf4detextify" /> + <br /> + <input type="file" name="pdf5" /> + detextify? <input type="checkbox" name="pdf5detextify" /> +</p> +<p> + (Maximum file size: <?php echo ini_get('upload_max_filesize'); ?>) +</p> +<p> + <input type="submit" name="submit" /> +</p> +</form> + +</body> +</html>