pdfconcat

view pdfconcat.php @ 1:6ea97e3f7cb5

ignore log and upload, because they change in a live setup
author markus schnalke <meillo@marmaro.de>
date Thu, 29 Aug 2013 13:59:09 +0200
parents
children
line source
1 <!--
2 pdfconcat-0.1
3 Written by markus schnalke <meillo@marmaro.de>,
4 developed at KIT-Library, Karlsruhe.
5 This is free software under the CC0 license.
6 http://marmaro.de/prog/pdfconcat
7 -->
8 <html>
9 <head>
10 <title>PDF concat and detextify</title>
11 <meta name="author" content="markus schnalke <meillo@marmaro.de>">
12 <meta name="copyright" content="No copyright applies.">
13 </head>
14 <body>
15 <h2>PDF concat and detextify</h2>
17 <?php
19 define('PDFDETEXTIFY', dirname(__FILE__).'/bin/pdfdetextify');
20 define('PDFCONCAT', dirname(__FILE__).'/bin/pdfconcat');
22 define('LOGFILE', dirname(__FILE__).'/log');
23 define('UPLOADDIR', 'upload');
26 function
27 detextify($file)
28 {
29 $newfile = tempnam(sys_get_temp_dir(), basename(__FILE__).".");
30 $cmd = sprintf("%s %s 2>&1 >%s", PDFDETEXTIFY, $file, $newfile);
31 system($cmd);
32 return $newfile;
33 }
36 function
37 concatpdfs($files)
38 {
39 $newfile = sprintf("%s/%s/%s.pdf", dirname(__FILE__), UPLOADDIR,
40 date('Y-m-d_H-i-s'));
41 $cmd = sprintf("%s %s 2>&1 >%s", PDFCONCAT, implode(' ', $files),
42 $newfile);
43 system($cmd);
44 foreach ($files as $file) {
45 unlink($file);
46 }
47 return sprintf("%s/%s", UPLOADDIR, basename($newfile));
48 }
51 function
52 procfiles()
53 {
54 $date = date("Y-m-d H:i:s");
55 $ip = $_SERVER['REMOTE_ADDR'];
56 $files = array();
57 foreach ($_FILES as $key => $val) {
58 if ($val['error'] == UPLOAD_ERR_NO_FILE) {
59 continue;
60 }
61 if ($val['error'] > 0) {
62 echo "Errors in transferring $val[name]. Skipping.\n";
63 echo "($val[error])\n";
64 continue;
65 }
66 if (isset($_POST[$key.'detextify']) && $_POST[$key.'detextify'] == 'on') {
67 $files[] = detextify($val['tmp_name']);
68 } else {
69 $files[] = $val['tmp_name'];
70 }
71 }
72 $newfile = concatpdfs($files);
73 // log
74 $logmsg = sprintf("[%s] %s creates `%s'\n", $date, $ip, $newfile);
75 file_put_contents(LOGFILE, $logmsg, FILE_APPEND);
77 return $newfile;
79 }
82 // main()
84 if (isset($_POST['submit'])) {
85 echo '<pre>';
86 $outfile = procfiles();
87 echo '</pre>';
88 echo '<hr>';
89 echo '<h2><a href="'. $outfile .'">The concatenated PDF</a></h2>';
90 echo '<hr>';
91 }
93 ?>
96 <p>
97 This webservice concatenates PDF files and optionally converts their
98 text to bitmaps.
99 </p>
100 <p>
101 The files are stored temporary on the webserver. The detextification
102 function modifies them. Use this service only of you have the
103 appropriate rights on the files.
104 </p>
106 <form action="<?php echo basename($_SERVER['SCRIPT_NAME']); ?>"
107 method="post" enctype="multipart/form-data">
108 <p>
109 <input type="file" name="pdf1" />
110 detextify? <input type="checkbox" name="pdf1detextify" />
111 <br />
112 <input type="file" name="pdf2" />
113 detextify? <input type="checkbox" name="pdf2detextify" />
114 <br />
115 <input type="file" name="pdf3" />
116 detextify? <input type="checkbox" name="pdf3detextify" />
117 <br />
118 <input type="file" name="pdf4" />
119 detextify? <input type="checkbox" name="pdf4detextify" />
120 <br />
121 <input type="file" name="pdf5" />
122 detextify? <input type="checkbox" name="pdf5detextify" />
123 </p>
124 <p>
125 (Maximum file size: <?php echo ini_get('upload_max_filesize'); ?>)
126 </p>
127 <p>
128 <input type="submit" name="submit" />
129 </p>
130 </form>
132 </body>
133 </html>