Into the (open) source


Print without driver, with file upload on Linux servers


This article was written in the 2013, there may have been some developments in the subject matter.
If in doubt please leave a comment in the bottom of the article.


In today's article I deal with printing. After I compared the various requirements of customers, I came to theorize the idea that the user (utonto) should have to have everything ready. Less software or drivers will be installed, will be more happy and will be less annoying to those who have something else to do.

The illumination came to me sifting between the printer settings that we have in the office. In one of the menus I see that you can upload the PDF to print on the printer's internal memory to print automatically. No driver (the printer is network linked), nothing special formatting, Only upload. So I thought it would not be hard to do the same thing with a printer connected via USB or serial port of a computer, not sharing (serve drivers). In fact, it was easy.

For the experiment with at least two computers, connected on the LAN, one of which is connected and configured to print locally (which we will call server). We will forward the PDF to the server via a web interface, because it is more intuitive and familiar user first. Therefore, the computer connected to the printer will have to do by print-server and web-server.

A few words on CUPS ...

Do it for granted that you are using a Linux server in order. For those who still did not know CUPS Take a tour Su Wikipedia or the official website of CUPS project (Common Unix Printing System). Suffice it to say that all print jobs in Linux (that I know) are managed by CUPS, a print server installed on your PC. Everyone. For the uninitiated, with CUPS, printers and print jobs are easily administered via a web interface. Simply plug in the url bar "http://localhost:631". From that page we can manage printers, print jobs, install or remove a printer, basically anything we want to make the printer we can do it as well as with the tools specially developed for each distribution, also from web interface. This makes it much easier to configure, for example, a print server. And it is precisely our case.

The Printer Administration window

CUPS: the window printer administration

Regarding the installation of CUPS we refer to the guida di Ubuntu, is still the only way to print with Linux, then each will have CUPS distribution within its repository. It is usually included by default in the distribution.

Now for the webserver, we will use Apache with PHP support. Even in this case the guidance of Ubuntu is very detailed, and takes into account the greater number of scenarios and problems that a user may experience when they install Apache. The installation of PHP instead I explain I.

You need to install PHP5 and a package that allows PHP to communicate with Apache. From terminal:

sudo apt-get install php5 libapache2-mod-php5

Apache must then be restarted in order for you "notice" which now has support for PHP. Always terminale:

sudo / etc/init.d/apache2 restart

Enter the password and confirm. Now we test if things are really going in the right direction. We create the file "/var/www/phpinfo.php" inside which insert the following code:

<? phpinfo(); ?>

Open a browser and write in the url bar "http://localhost/phpinfo.php". You should see a page similar to the following.

PHP up and running

PHP up and running

Well, now we also have a running web. We need to create a script that allows the upload of PDF on a server. First, create the HTML form:


<form action="upload_file.php" method="post"
enctype ="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Upload!">



The form invokes the file "upload_file.php", the content of which is as follows:

$allowedExts = array("pdf");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/pdf"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
  if ($_FILES["file"]["error"] > 0)
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      echo $_FILES["file"]["name"] . " already exists. ";
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  echo "Invalid file";

The fifth line will have noticed that I have set an upper limit on the size of the PDF. This can also be safely removed, and modified the file "php.ini" so let it load even heavier PDF. Do not forget that everything happens locally within a LAN. We also create the directory "upload", where file will be uploaded.

If the upload is successful, the script file will show us what is loaded, the file type, the size and where it is saved on the server, otherwise it returns "Invalid file".


Now that the first file is uploaded we will make sure that all the contents of the directory "upload" be printed, and then moved to a backup directory will be emptied daily (we do not want to clog up the file server, true?). I'll call the backup directory "deposit", positioned within the root directory. For all we will use the automatic Cron.

We add the following lines at the end of crontab file ("crontab-e").

# print server
* * * * * lpr / var / www / uploads / upload / * | sleep 30; mv / var / www / uploader / uploads / * / var / www / uploader / deposits /
0 4 * * * rm-Rf / var / www / uploader / deposit / *

The first command crontab every minute checks the directory "upload", print every file inside it and move all the contents in the directory "deposit". The second deletes all files in the directory "deposit" at 4 each night.


I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.