{"id":444,"date":"2015-10-11T13:07:07","date_gmt":"2015-10-11T11:07:07","guid":{"rendered":"https:\/\/olkn.myvnc.com\/?p=444"},"modified":"2015-10-11T13:12:51","modified_gmt":"2015-10-11T11:12:51","slug":"print-documents-via-web-interface","status":"publish","type":"post","link":"https:\/\/olkn.myvnc.com\/?p=444","title":{"rendered":"print documents via web interface"},"content":{"rendered":"<p>This simple php code will allow to choose one or more documents and prints them using the local cups printer. There is also a possibility to select one of the locally installed printers.<\/p>\n<p>There are some prerequesites as follows:<\/p>\n<ul>\n<li>install unoconv and openoffice\/libreoffice (import filters are required)<\/li>\n<li>install calc, writer, impress, draw as required (import formats)<\/li>\n<li>local lpr printing programm and lpstat for list of installed printers are required<\/li>\n<li>upload is putting any non-pdf document in a local folder on the webserver for conversion this folder must be writable by PHP\/apache<\/li>\n<li>copy this file to location accessible by apache<\/li>\n<\/ul>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php ############# HEAD  ############################\r\necho &quot;&lt;!DOCTYPE html PUBLIC \\&quot;-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\\&quot; \\&quot;http:\/\/www.w3.org\/TR\/html4\/loose.dtd\\&quot;&gt;\\n&quot;;\r\necho &quot;&lt;html&gt;\\n&lt;head&gt;\\n&lt;meta http-equiv=\\&quot;Content-Type\\&quot; content=\\&quot;text\/html; charset=UTF-8\\&quot;&gt;\\n&quot;;\r\n\/* Short Introduction\r\n * \r\n * - install unoconv and openoffice\/libreoffice (import filters are required)\r\n * - install calc, writer, impress, draw as required (import formats)\r\n * - local lpr printing programm and lpstat for list of installed printers are required\r\n * - upload is putting any non-pdf document in a local folder on the webserver for conversion\r\n *   this folder must be writable by PHP\/apache\r\n * - copy this file to location accessible by apache\r\n *\/\r\n############# Configuration ##########################\r\n$print_command = &quot;\/usr\/bin\/lpr -P &quot;;# local print command\r\n$conv_command = &quot;unoconv -f pdf &quot;;# conversion command\r\n$upload_directory =&quot;\/var\/www\/PDF\/&quot;;# dir where all the magic takes place\r\n$filesize = 100000;# file size limit in kB\r\n$printer_default = &quot;PDF&quot;;# default printer for first start\r\n### no changes should be necessary beneathe this line\r\n$reg_printer = &quot;\/^device for (.*): .*$\/im&quot;;# examination of lpstat to get list of installed printers\r\n$PHP_SELF = htmlspecialchars($_SERVER&#x5B;'PHP_SELF']); \r\n$uploadcounter = 1;#default value for number of upload buttons\r\necho &quot;&lt;title&gt;Print File&lt;\/title&gt;\\n&lt;\/head&gt;\\n&lt;body&gt;\\n\\n&quot;;\r\n###### debug output of POST\r\n# debugging function to show HTML variables\r\n#\r\nfunction debug_output(){\r\n\techo &quot;&lt;pre&gt;&lt;br&gt;&lt;h1&gt;Show internal variables&lt;\/h1&gt;&quot;;\r\n\tprint_r($_POST);echo &quot;&lt;br&gt;&quot;;\r\n\tprint_r($_FILES);\r\n\techo &quot;&lt;\/pre&gt;&quot;; return ;\r\n}\r\n###### get local printerrs #############\r\n# function will examine lpstat to get list of\r\n# locally installed printers\r\n#\r\nfunction get_local_printer($printer_default){\r\n\tglobal $reg_printer;\r\n\t$output = shell_exec(&quot;\/usr\/bin\/lpstat -s&quot;);\r\n\tif (preg_match_all($reg_printer, $output, $printer)) {\r\n\t\techo &quot;&lt;SELECT name='printer' size='1'&gt;\\n&quot;;\r\n\t\tforeach ($printer&#x5B;1] as $key =&gt; $value){\r\n\t\techo &quot;&lt;OPTION&quot;;if ($printer_default == $value) echo &quot; selected&quot;;echo &quot;&gt; &quot;.$value.&quot; &lt;\/OPTION&gt;\\n&quot;;\r\n\t\t}\r\n\t\techo &quot;&lt;\/SELECT&gt;&quot;;\r\n\t}\r\n\treturn;\r\n}\r\n###### file upload box with comment text\r\n# show file upload box with in-\/decrease button\r\n#\r\nfunction fileupload_box($text, $count=1){\r\n\tglobal $filesize;\r\n\tif ($text != &quot;&quot;) echo &quot;&lt;h2&gt;&quot;.$text.&quot;&lt;\/h2&gt;\\n&quot;;# show status information on top\r\n\techo '&lt;input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;'.$filesize.'&quot; &gt;';\r\n\techo &quot;&lt;table border='0'&gt;&quot;;# Table for formatting\r\n\tfor ($i=0; $i &lt; $count; $i++){\r\n\t\techo &quot;&lt;tr&gt;&lt;td&gt;&lt;input type='submit' name='addcounter' value='+'&gt;&lt;\/td&gt;&quot;;\r\n\t\techo &quot;&lt;td&gt;&lt;input type='submit' name='delcounter' value='-'&gt;&lt;\/td&gt;&quot;;\r\n\t\techo &quot;&lt;td&gt;File to print Nr. &quot;.$i.&quot; :&lt;\/td&gt;&lt;td&gt;&lt;INPUT TYPE='file' NAME='print_file&#x5B;]' size='50'&gt;&lt;\/td&gt;&lt;\/tr&gt;\\n&quot;;\r\n\t}\r\n\techo &quot;&lt;tr&gt;&lt;td colspan='4'&gt;&amp;nbsp; &lt;\/td&gt;&lt;\/tr&gt;&quot;;\r\n\techo &quot;&lt;tr&gt;&lt;td colspan='4' align='center'&gt;&lt;INPUT TYPE='SUBMIT' NAME='UPLOAD' value=' PRINT '&gt;&lt;\/td&gt;&lt;\/tr&gt;\\n&lt;\/table&gt;\\n&quot;;\r\n}\r\n############################## MAIN ##################################\r\necho &quot;&lt;form method='post' action='$PHP_SELF' enctype='multipart\/form-data'&gt;\\n&quot;;# form for file upload\r\nif (isset($_POST&#x5B;'uploadcounter'])) $uploadcounter = $_POST&#x5B;'uploadcounter'];\r\nif (isset($_POST&#x5B;'addcounter'])) $uploadcounter = $uploadcounter+1;# additional upload button please\r\nif (isset($_POST&#x5B;'delcounter'])) $uploadcounter = $uploadcounter-1;# delete one upload button please\r\nif (isset($_POST&#x5B;'printer'])) $printer_default = $_POST&#x5B;'printer'];# keep choosen printer\r\nif ($uploadcounter &lt; 1) $uploadcounter=1;# at least one entry must remain\r\necho &quot;&lt;table width='100%'&gt;&lt;tr&gt;&lt;td&gt;&lt;h1&gt;Print selected Files&lt;\/h1&gt;&lt;\/td&gt;\\n&quot;;$text = &quot;&quot;;\r\necho &quot;&lt;td&gt;Choose Printer:&lt;\/td&gt;&lt;td&gt;&quot;;get_local_printer($printer_default);\r\necho &quot;&lt;\/td&gt;&lt;\/tr&gt;&lt;\/table&gt;\\n&quot;;\r\nif (isset($_POST&#x5B;'UPLOAD'])) {# upload button pressed;the magic begins\r\n\tfor ($i=0; $i &lt; $uploadcounter; $i++){# iterate over list of files\r\n\t\t$file = $_FILES&#x5B;'print_file']&#x5B;'tmp_name']&#x5B;$i];# temporary file name\r\n\t\t$filename = $upload_directory.basename($file);# final doc name incl folder\r\n\t\tif ($file == &quot;&quot; || $_FILES&#x5B;'print_file']&#x5B;'size']&#x5B;$i] == 0 || !is_file($file)) { # no file selected \r\n\t\t\t$text = &quot;Please choose files to print!&quot;;\r\n\t\t}\r\n\t\telse {\r\n\t\t\tif (is_uploaded_file($file)) { # check for upload errors\r\n\t\t\t\tif(!move_uploaded_file($file, $filename))# copy files to dir\r\n\t\t\t\t\techo &quot;&lt;h2&gt;Moving file&quot;.$file.&quot; to destination &quot;.$upload_directory.&quot; failed!&lt;\/h2&gt;&quot;;\r\n\t\t\t\telse{\r\n\t\t\t\t\tif (strripos(mime_content_type($filename), &quot;pdf&quot;) === false){# check if file mime type is pdf\r\n\t\t\t\t\t\tshell_exec( $conv_command.&quot; &quot;.$filename.&quot; -o &quot;.$filename.&quot;.pdf &gt; \/dev\/null&quot;);# unoconv -f pdf file\r\n\t\t\t\t\t\tshell_exec( $print_command.$printer_default.&quot; &quot;.$filename.&quot;.pdf &gt; \/dev\/null&quot;);# execute print command\r\n\t\t\t\t\t\techo &quot;&lt;p style='color:#aaaaaa, font-size:50%'&gt; (&quot;.$print_command.$printer_default.&quot; &quot;.$filename.&quot;.pdf) &lt;\/p&gt;&quot;;\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse # file is pdf, so print directly\r\n\t\t\t\t\t\tshell_exec( $print_command.$printer_default.&quot; &quot;.$file.&quot; &gt; \/dev\/null&quot;);# execute print command\r\n\t\t\t\t\techo &quot;&lt;br&gt;Printouts may be found in your local printer\\n&quot;;# be polite ;-)\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\telse { # file upload went wrong\r\n\t\t\t\t$text = &quot;File Upload error&quot;;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t$uploadcounter=1;# reset counter\r\n}\r\necho &quot;&lt;input type='hidden' name='uploadcounter' value='&quot;.$uploadcounter.&quot;'&gt;&quot;;# take hidden counter for number of upload buttons\r\nfileupload_box($text, $uploadcounter);# generate upload boxes\r\n#debug_output();# show debug information\r\necho &quot;&lt;\/form&gt;\\n&lt;\/Body&gt;&lt;\/HTML&gt;\\n&quot;;# The End!\r\n########### END ###################################?&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This simple php code will allow to choose one or more documents and prints them using the local cups printer. There is also a possibility to select one of the locally installed printers. There are some prerequesites as follows: install unoconv and openoffice\/libreoffice (import filters are required) install calc, writer, impress, draw as required (import &hellip; <a href=\"https:\/\/olkn.myvnc.com\/?p=444\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">print documents via web interface<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[50,51,155,79,156,103,116,154,131],"class_list":["post-444","post","type-post","status-publish","format-standard","hentry","category-programming","tag-cups","tag-debian","tag-html","tag-linux","tag-php","tag-print","tag-server","tag-web","tag-webfrontend"],"_links":{"self":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts\/444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=444"}],"version-history":[{"count":2,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts\/444\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts\/444\/revisions\/446"}],"wp:attachment":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}