Matteo Mattei

Hello, my name is Matteo Mattei and this is my personal website. I am computer engineer with a long experience in Linux system administration and web software development.

linkedin rss twitter google+ github facebook

How to directly print from web browser

Fortunately the web browsers have been created to protect the client environment from executing client processes and accessing client resources. However sometimes this limitation is too strong and does not let us to do some simple things like direct print a document without opening an intermediate print pop-up.

I searched in Google for a long time and experimented several tests before summarizing all possibilities I found. There is no a single way to achieve it and every option has some pros and cons depending on the environment and conditions you have.

Direct printing using a system call

Requirements:

  • A web server installed (I only consider a Linux server).
  • A printer directly connected to the web server.

Pros:

  • You have the full control of the printer parameters.
  • This option allow you to use every browser.
  • You can print a file or a document without creating a web layout for it.

Cons:

  • You can use the application only on the same PC that holds the web server (and the printer).
  • At least you can install the web application in the company server with a Network printer.

Details: The idea here is to have a web server running for example Apache and PHP (but every other server-side language would be OK). On the same PC a printer is connected and configured using CUPS (on Linux). When a user clicks a button or a link in the web page, a PHP script executes a system() call to lpr that creates one ore more printer jobs depending on the number of documents passed.

The function above accepts a string of documents (space separated), sends a printer command and then polls the printer queue (the spooler) using lpd command and wait for the printer to return ready. Just for reference, the above function is a snippet of code I used to print barcodes with a Dymo LabelWrite 450 Turbo and 99012 paper labels.

Direct printing using browser options

Requirements:

  • A compatible web browser (Firefox or Chrome).
  • Direct access to the user browser.

Pros:

  • You don’t need to have a web server running on the same machine.
  • The web application can be remote.
  • You can print what you see in the browser using just HTML and CSS.

Cons:

  • You need to create a web layout for a document with HTML and CSS.
  • You don’t have access to all printer options.
  • Limited number of supported browsers.
  • You must have access to the web browser to change configuration.

Details: In this case we have to modify the preferences of the web browser and/or execute it in a particular mode (like kiosk mode for example). As you know, every browser has a different configuration… From the web side we can use the Javascript window.print() function. Use the following instructions based on the web browser you are using:

  1. Chrome You need to have Chrome version 18.0.1 or higher. From Chrome, open a new tab and type info:config then make sure the Disable Print Preview flag is NOT enabled (Print preview must be enabled for Kiosk Printing to work). Now set your application the default page to open when Chrome launches and then close the browser. Now you have to execute Chrome in kiosk mode. To do it just append --kiosk --kiosk-printing to the chrome executable. Chrome should now start in kiosk mode (full-screen) and from here you should be able to print directly from the browser.

  2. Firefox In Firefox open a new tab and type about:config in the address bar. Then right click on the white space and select NEW > BOOLEAN and in the text edit that appears type print.always_print_silent, hit OK and then select true. This procedure writes a line to prefs.js file (on Windows) or user.js file on Linux and the next time you start the browser, any Javascript print(); function will print directly to the printer using the currently configured print settings.

You can use the following test page to test the direct printing using browser options:


Backup Virtualhosts and MySQL data on Plesk server and store images in a remote FTP server

I am working for a client that needs a system to backup a CentOS server hosted by OVH that uses Plesk. I didn’t know that Plesk already implemented a way to backup all domains and MySQL data so I cerated a script to do that:

As you can see this script keeps the last 7 days of backups. However after googling a while I found that the same thing could be done directly using a Plesk utility called pleskbackup, so I developed an alterlate script that uses it:

I hope it can help you.


How to resolve the audio distortion in Skype on Ubuntu 13.04 and previous versions

If you have problems with Skype audio on Ubuntu (distortion, croak, noise…) the simple way to get it fixed is editing the file /etc/pulse/default.pa, change one line and restart the system.

From:

load-module module-udev-detect use_ucm=0

To:

load-module module-udev-detect use_ucm=0 tsched=0

For simplicity you can execute the following command that will do all the job:

sudo sed -i "{s/^load-module module-udev-detect use_ucm=0$/load-module module-udev-detect use_ucm=0 tsched=0/g}" /etc/pulse/default.pa

Restart now the system and the Skype audio should work fine!


How to calculate the number of inserted, deleted and modified lines in Subversion

If you need to calculate the number of inserted, deleted or modified lines in Subversion between two separate commits, you can use a simple script like this:


How to calculate the crc32 of a file in Python

Calculating the crc32 of a file in Python is very simple but I often forgot how to do. For this reason I put this snippet here:

You can simply call the CRC32_from_file() function passing a file with the whole path as argument.


How to log email sent from PHP through mail() function

If you have a website in a virtualhost environment that is under attack and starts sending tons of emails, is sometimes difficult to understand from where the attack is started (especially if you have several virtual hosts). However with a little PHP script you can understand from which folder the attack is coming.

Create the following file in a secure place and call it phpsendmail:

Now create the log file and set the correct permissions:

touch /var/log/mail_php.log
chmod 777 /var/log/mail_php.log
chmod 777 /path/to/phpsendmail

Now you have to edit the php.ini configuration (/etc/php5/apache2/php.ini in Debian). Search the [mail_function] section and set it in this way:

[mail function]
;SMTP = localhost
;smtp_port = 25
sendmail_path = /path/to/phpsendmail

Now you can restart Apache and look at /var/log/mail_php.log file.
Its content shoud be someting similar to this:

2013-02-03 17:50:57  To: mail1@domain1.com From: mail2@domain2.com ==> /var/www/vhosts/domain1/httpdocs
2013-02-03 17:50:59  To: mail3@domain3.com From: mail4@domain4.com ==> /var/www/vhosts/domain2/httpdocs/libraries
2013-02-03 17:51:02  To: mail5@domain5.com From: mail6@domain6.com ==> /var/www/vhosts/domain2/httpdocs/assets

Update August 2014 —————— I found a more convenient way to do it… and it saved my life with some servers that were affected by thousands of SPAM emails. You just need to create a couple of files:

Now, in the same way as above set the correct permissions and edit php.ini:

chmod +x /usr/local/bin/sendmail-wrapper
chmod +x /usr/loca/bin/env.php
[mail function]
;SMTP = localhost
;smtp_port = 25
sendmail_path = /usr/local/bin/sendmail-wrapper
auto_prepend_file = /usr/local/bin/env.php

Restart Apache and look at /var/log/mail.info. Now the content is similar to the following:

Aug 18 20:35:42 vps74403 logger: sendmail-wrapper.sh: site=www.example.com, client=77.221.130.44, script=/WP/wp-content/uploads/flags/plugin.php, pwd=/var/www/vhosts/example.com/WP/wp-content/uploads/flags, uid=, user=www-data
Aug 18 20:35:42 vps74403 logger: sendmail-wrapper.sh: site=www.example.com, client=77.221.130.44, script=/WP/wp-content/uploads/flags/plugin.php, pwd=/var/www/vhosts/example.com/WP/wp-content/uploads/flags, uid=, user=www-data
Aug 18 20:35:42 vps74403 logger: sendmail-wrapper.sh: site=www.example.com, client=77.221.130.44, script=/WP/wp-content/uploads/flags/plugin.php, pwd=/var/www/vhosts/example.com/WP/wp-content/uploads/flags, uid=, user=www-data